Linked List

Data Structure 2019. 2. 13. 23:00


#include <stdio.h>
#include <algorithm>
#include <list>
#include "Windows.h"
using namespace std;

struct NODE {
	int prev;
	int next;
	int val;
};

const int NODE_SIZE = 30000;

//TEST CMD
const int PUSH_BACK = 0;
const int PUSH_FRONT = 1;
const int INSERT = 2;
const int POP_BACK = 3;
const int POP_FRONT = 4;
const int ERASE = 5;

int test_cmd[NODE_SIZE][3];

struct MY_LIST {
	int HEAD = NODE_SIZE;
	int TAIL = NODE_SIZE + 1;
	int pos;
	NODE node[NODE_SIZE + 2];

	MY_LIST() {
		pos = 0;
		node[HEAD].next = TAIL;
		node[TAIL].prev = HEAD;
	}

	void push_back(int data) {
		int prev = node[TAIL].prev;
		int next = node[prev].next;	// TAIL;

		node[pos].val = data;

		node[pos].prev = prev;
		node[prev].next = pos;

		node[pos].next = next;
		node[next].prev = pos;
		++pos;
	}

	void push_front(int data) {
		int next = node[HEAD].next;
		int prev = node[next].prev;	// HEAD
		
		node[pos].val = data;

		node[pos].prev = prev;
		node[prev].next = pos;

		node[pos].next = next;
		node[next].prev = pos;
		++pos;
	}

	void insert(int p, int data) {
		int next = node[HEAD].next;
		for(int i = 0; i < p; ++i) {
			next = node[next].next;
		}
		int prev = node[next].prev;
	
		node[pos].val = data;

		node[pos].prev = prev;
		node[prev].next = pos;

		node[pos].next = next;
		node[next].prev = pos;
		++pos;
	}

	void pop_back() {
		int target = node[TAIL].prev;

		int prev = node[target].prev;
		int next = node[target].next;

		node[prev].next = next;
		node[next].prev = prev;
	}

	void pop_front() {
		int target = node[HEAD].next;

		int prev = node[target].prev;
		int next = node[target].next;

		node[prev].next = next;
		node[next].prev = prev;
	}

	void erase(int p) {
		int target = node[HEAD].next;
		for (int i = 0; i < p; ++i) {
			target = node[target].next;
		}
		int prev = node[target].prev;
		int next = node[target].next;

		node[prev].next = next;
		node[next].prev = prev;
	}
};

MY_LIST my_list;
list<int> stl_list;

int main()
{
	// make test case..
	int cur_size = 0;
	for (int i = 0; i < NODE_SIZE; ++i) {
		if (i < NODE_SIZE / 3) {
			test_cmd[i][0] = rand() % 2;
		}
		else {
			test_cmd[i][0] = rand() % 6;
		}

		switch (test_cmd[i][0]) {
		case PUSH_BACK:
		case PUSH_FRONT: {
			test_cmd[i][1] = rand();
			++cur_size;
			break;
		}
		case INSERT: {
			test_cmd[i][1] = rand() % cur_size;
			test_cmd[i][2] = rand();
			++cur_size;
			break;
		}
		case POP_BACK:
		case POP_FRONT: {
			--cur_size;
			break;
		}
		case ERASE: {
			test_cmd[i][1] = rand() % cur_size;
			--cur_size;
			break;
		}
		}
	}

	// test my list
	int my_list_begin = GetTickCount();
	for (int i = 0; i < NODE_SIZE; ++i) {
		switch (test_cmd[i][0]) {
		case PUSH_BACK: {
			my_list.push_back(test_cmd[i][1]);
			break;
		}
		case PUSH_FRONT: {
			my_list.push_front(test_cmd[i][1]);
			break;
		}
		case INSERT: {
			my_list.insert(test_cmd[i][1], test_cmd[i][2]);
			break;
		}

		case POP_BACK: {
			my_list.pop_back();
			break;
		}
		case POP_FRONT: {
			my_list.pop_front();
			break;
		}
		case ERASE: {
			my_list.erase(test_cmd[i][1]);
			break;
		}
		}
	}
	int my_list_end = GetTickCount();

	// test stl list
	int stl_list_begin = GetTickCount();
	for (int i = 0; i < NODE_SIZE; ++i) {
		switch (test_cmd[i][0]) {
		case PUSH_BACK: {
			stl_list.push_back(test_cmd[i][1]);
			break;
		}
		case PUSH_FRONT: {
			stl_list.push_front(test_cmd[i][1]);
			break;
		}
		case INSERT: {
			list<int>::iterator it = stl_list.begin();
			for (int k = 0; k < test_cmd[i][1]; ++k) {
				++it;
			}
			stl_list.insert(it, test_cmd[i][2]);
			break;
		}

		case POP_BACK: {
			stl_list.pop_back();
			break;
		}
		case POP_FRONT: {
			stl_list.pop_front();
			break;
		}
		case ERASE: {
			list<int>::iterator it = stl_list.begin();
			for (int k = 0; k < test_cmd[i][1]; ++k) {
				++it;
			}
			stl_list.erase(it);
			break;
		}
		}
	}
	int stl_list_end = GetTickCount();

	//time compare
	printf("my list : %d\n", (my_list_end - my_list_begin));
	printf("stl list : %d\n", (stl_list_end - stl_list_begin));

	//result test
	list<int>::iterator it = stl_list.begin();
	int cur = my_list.node[my_list.HEAD].next;
	while (it != stl_list.end()) {
		if (*it != my_list.node[cur].val) {
			printf("Error\n");
		}
		++it;
		cur = my_list.node[cur].next;
	}

	return 0;
}


설정

트랙백

댓글

Merge Sort

Data Structure 2019. 2. 10. 23:43



#include <stdio.h>
#include "Windows.h"
#include <algorithm>
using namespace std;

const int MAX_SIZE = 500000;

int arr_size;
int ms[MAX_SIZE], qs[MAX_SIZE], stls[MAX_SIZE], buf[MAX_SIZE];

void merge_sort(int* p, int len) {
	if (len < 2)	return;
	int i, j, k, mid;
	mid = (len / 2);
	i = 0, j = mid, k = 0;

	merge_sort(p, mid);
	merge_sort((p + mid), (len - mid));

	while (i < mid && j < len) {
		if (p[i] < p[j]) {
			buf[k++] = p[i++];
		}
		else {
			buf[k++] = p[j++];
		}
	}

	while (i < mid) {
		buf[k++] = p[i++];
	}

	while (j < len) {
		buf[k++] = p[j++];
	}

	for (int i = 0; i < len; ++i) {
		p[i] = buf[i];
	}
}

void qsort(int* p, int left, int right) {
	if (left >= right)	return;
	int l = left - 1;
	int r = right + 1;
	int mid = p[(l + r) / 2];
	while (1) {
		while (p[++l] < mid);
		while (p[--r] > mid);
		if (l >= r)	break;
		int temp = p[l];
		p[l] = p[r];
		p[r] = temp;
	}
	qsort(p, left, l - 1);
	qsort(p, r + 1, right);
}


int main()
{
	arr_size = MAX_SIZE;

	for (int i = 0; i < arr_size; ++i) {
		ms[i] = rand();
		qs[i] = stls[i] = ms[i];
	}

	int quick_sort_begin = GetTickCount();
	qsort(qs, 0, arr_size - 1);
	int quick_sort_end = GetTickCount();

	int merge_sort_begin = GetTickCount();
	merge_sort(ms, arr_size);
	int merge_sort_end = GetTickCount();

	int stl_sort_begin = GetTickCount();
	sort(stls, stls + arr_size);
	int stl_sort_end = GetTickCount();

	printf("my quick sort : %d\n", (quick_sort_end - quick_sort_begin));
	printf("my merge sort : %d\n", (merge_sort_end - merge_sort_begin));
	printf("stl sort : %d\n", (stl_sort_end - stl_sort_begin));
	printf("=====================================\n");

	quick_sort_begin = GetTickCount();
	qsort(qs, 0, arr_size - 1);
	quick_sort_end = GetTickCount();

	merge_sort_begin = GetTickCount();
	merge_sort(ms, arr_size);
	merge_sort_end = GetTickCount();

	stl_sort_begin = GetTickCount();
	sort(stls, stls + arr_size);
	stl_sort_end = GetTickCount();

	printf("my quick sort : %d\n", (quick_sort_end - quick_sort_begin));
	printf("my merge sort : %d\n", (merge_sort_end - merge_sort_begin));
	printf("stl sort : %d\n", (stl_sort_end - stl_sort_begin));
	printf("=====================================\n");

	for (int i = 0; i < arr_size; ++i) {
		if (qs[i] != stls[i] || ms[i] != stls[i]) {
			printf("Error\n");
		}
	}

	return 0;
}


설정

트랙백

댓글

Hash

Data Structure 2019. 1. 22. 22:59



#include <stdio.h>
#include <map>
#include <algorithm>
#include "Windows.h"
using namespace std;

const int PN = 23;
const int HASH_SIZE = 10000;

int table[HASH_SIZE][50];
int hash_size = 0;
char hash_raw[30000][100];

char input[30000][100];
map<char*, int> test;

unsigned int get_key(char str[]) {
	unsigned int key = 0, p = 1;

	for (int i = 0; str[i] != 0; ++i) {
		key += (str[i] * p);
		p *= PN;
	}

	return (key % HASH_SIZE);
}

int my_strcamp(char a[], char b[]) {
	int i = 0, j = 0;
	while (a[i]) {
		if (a[i++] != b[j++]) {
			--i, --j;
			break;
		}
	}
	return (a[i] - b[j]);
}

int contain(char str[]) {
	unsigned int key = get_key(str);
	int size = table[key][0];
	for (int i = 1; i <= size; ++i) {
		int pos = table[key][i];
		if (my_strcamp(hash_raw[pos], str) == 0) {
			return pos;
		}
	}
	return -1;
}

void add(char str[]) {
	int len = 0;
	for (len = 0; str[len] != 0; ++len) {
		hash_raw[hash_size][len] = str[len];
	}
	hash_raw[hash_size][len] = 0;

	unsigned int key = get_key(str);
	int& size = table[key][0];
	table[key][++size] = hash_size;

	++hash_size;
}

bool remove(char str[]) {
	unsigned int key = get_key(str);
	int& size = table[key][0];
	for (int i = 1; i <= size; ++i) {
		int pos = table[key][i];
		if (my_strcamp(hash_raw[pos], str) == 0) {
			for (int j = i + 1; j <= size; ++j) {
				table[key][j - 1] = table[key][j];
			}
			--size;
			return true;
		}
	}
	return false;
}

int make_int(int min, int max) {
	return (rand() % (max - min)) + min;
}

char make_char() {
	int val = rand() % 52;
	if (val < 26) {
		return static_cast<char>('a' + val);
	}
	return static_cast<char>('A' + val - 26);
}

int main()
{
	for (int i = 0; i < 30000; ++i) {
		int len = make_int(10, 100);
		for (int j = 0; j < len; ++j) {
			input[i][j] = make_char();
		}
		input[i][len] = 0;

		if (contain(input[i]) == -1) {
			add(input[i]);
		}
		test[input[i]] = i;


		if (i > 20000) {
			int cmd = make_int(0, 5);
			int index = make_int(0, i);
			if (cmd == 0) {
				if (contain(input[index]) != -1) {
					remove(input[index]);
				}

				test.erase(input[index]);
			}
			if (cmd == 1) {
				int my_pos = contain(input[index]);
				map<char*, int>::iterator iter = test.find(input[index]);
				int stl_pos = -1;
				if (iter != test.end()) {
					stl_pos = iter->second;
				}

				if (my_pos != stl_pos) {
					printf("find error!!!\n");
				}
			}
		}
	}

	int my_hash_size = 0;
	for (int i = 0; i < HASH_SIZE; ++i) {
		my_hash_size += table[i][0];
	}

	if (my_hash_size != test.size()) {
		printf("remove error!!!\n");
	}

	return 0;
}


설정

트랙백

댓글

Heap

Data Structure 2019. 1. 11. 22:36





#include <stdio.h>
#include <algorithm>
#include "Windows.h"
using namespace std;

int heap_size;
int heap[10000];

void push(int data) {
	int target = heap_size + 1;
	while (target != 1 && heap[target / 2] < data) {
		heap[target] = heap[target / 2];
		target /= 2;
	}
	heap[target] = data;
	++heap_size;
}

void pop() {
	int parent = 1, child = 2;
	int temp = heap[heap_size];
	while (child < heap_size) {
		if (child + 1 < heap_size && heap[child] < heap[child + 1]) {
			++child;
		}
		if (temp >= heap[child]) {
			break;
		}
		heap[parent] = heap[child];
		parent = child;
		child *= 2;
	}
	heap[parent] = temp;
	--heap_size;
}

bool comp(int a, int b) {
	return (a > b);
}

int main()
{
	int a[10000], b[10000];
	for (int i = 0; i < 9999; ++i) {
		a[i] = rand() % 10000;
		b[i] = a[i];
	}
	sort(a, a + 9999, comp);

	for (int i = 0; i < 9999; ++i) {
		push(b[i]);
	}

	for (int i = 0; i < 9999; ++i) {
		if (a[i] != heap[1]) {
			printf("not heap!!!\n");
		}
		pop();
	}

	return 0;
}


설정

트랙백

댓글