Skip to content
Snippets Groups Projects
Commit eff3b245 authored by Peter Bui's avatar Peter Bui
Browse files

Add lecture01

parents
No related branches found
No related tags found
No related merge requests found
*.bak
*.o
*.swp
*~
Examples
========
Example source code for CSE 30331 / 34331 Data Structures (Fall 2016)
CXX= g++
CXXFLAGS= -g -gdwarf-2 -std=gnu++11 -Wall
C_SOURCE= $(wildcard *.c)
CXX_SOURCE= $(wildcard *.cpp)
PROGRAMS= $(C_SOURCE:.c=) $(CXX_SOURCE:.cpp=)
all: $(PROGRAMS)
%: %.c
$(CC) $(CFLAGS) -o $@ $^
%: %.cpp
$(CXX) $(CXXFLAGS) -o $@ $^
clean:
rm -f $(PROGRAMS)
.PHONY: all clean
// duplicates_linear.cpp
#include <algorithm>
#include <climits>
#include <iostream>
#include <random>
#include <vector>
using namespace std;
const int N = 1<<15;
int main(int argc, char *argv[]) {
random_device rd;
default_random_engine g(rd());
uniform_int_distribution<> d(1, INT_MAX);
vector<int> v;
for (int i = 0; i < N; i++) {
v.push_back(d(g));
}
for (auto it = v.begin(); it != v.end(); it++) {
if (find(it + 1, v.end(), *it) != v.end()) {
cout << *it << " is duplicated" << endl;
break;
}
}
return 0;
}
// duplicates_set.cpp
#include <algorithm>
#include <climits>
#include <iostream>
#include <random>
#include <set>
using namespace std;
const int N = 1<<15;
int main(int argc, char *argv[]) {
random_device rd;
default_random_engine g(rd());
uniform_int_distribution<> d(1, INT_MAX);
set<int> s;
for (int i = 0; i < N; i++) {
int n = d(g);
if (s.find(n) != s.end()) {
cout << n << " is duplicated" << endl;
break;
}
s.insert(n);
}
return 0;
}
// duplicates_linear.cpp
#include <algorithm>
#include <climits>
#include <iostream>
#include <random>
#include <vector>
using namespace std;
const int N = 1<<15;
int main(int argc, char *argv[]) {
random_device rd;
default_random_engine g(rd());
uniform_int_distribution<> d(1, INT_MAX);
vector<int> v;
for (int i = 0; i < N; i++) {
v.push_back(d(g));
}
sort(v.begin(), v.end());
for (auto it = v.begin(); it != v.end(); it++) {
if (*it == *(it + 1)) {
cout << *it << " is duplicated" << endl;
}
}
return 0;
}
// duplicates_unordered_set.cpp
#include <algorithm>
#include <climits>
#include <iostream>
#include <random>
#include <unordered_set>
using namespace std;
const int N = 1<<15;
int main(int argc, char *argv[]) {
random_device rd;
default_random_engine g(rd());
uniform_int_distribution<> d(1, INT_MAX);
unordered_set<int> s;
for (int i = 0; i < N; i++) {
int n = d(g);
if (s.find(n) != s.end()) {
cout << n << " is duplicated" << endl;
break;
}
s.insert(n);
}
return 0;
}
// stack_default.cpp
#include <cassert>
#include <stack>
using namespace std;
const int N = 1<<28;
int main(int argc, char *argv[]) {
stack<int> s;
// Fill stack
for (int i = 0; i < N; i++) {
s.push(i);
}
// Empty stack
int i = N - 1;
while (!s.empty()) {
assert(s.top() == i);
s.pop();
i--;
}
return 0;
}
// stack_default.cpp
#include <stack>
using namespace std;
const int N = 1<<28;
int main(int argc, char *argv[]) {
stack<int> s;
// Fill stack
for (int i = 0; i < N; i++) {
s.push(i);
}
// Empty stack
while (!s.empty()) {
s.pop();
}
return 0;
}
// stack_deque.cpp
#include <stack>
#include <deque>
using namespace std;
const int N = 1<<28;
int main(int argc, char *argv[]) {
stack<int, deque<int>> s;
// Fill stack
for (int i = 0; i < N; i++) {
s.push(i);
}
// Empty stack
while (!s.empty()) {
s.pop();
}
return 0;
}
// stack_list.cpp
#include <stack>
#include <list>
using namespace std;
const int N = 1<<28;
int main(int argc, char *argv[]) {
stack<int, list<int>> s;
// Fill stack
for (int i = 0; i < N; i++) {
s.push(i);
}
// Empty stack
while (!s.empty()) {
s.pop();
}
return 0;
}
// stack_vector.cpp
#include <stack>
#include <vector>
using namespace std;
const int N = 1<<28;
int main(int argc, char *argv[]) {
stack<int, vector<int>> s;
// Fill stack
for (int i = 0; i < N; i++) {
s.push(i);
}
// Empty stack
while (!s.empty()) {
s.pop();
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment