내 세상

[Algorithms] Datastructure Queue 구현-Linked List 본문

Coding/Algorithms

[Algorithms] Datastructure Queue 구현-Linked List

sga8 2021. 3. 10. 20:14
728x90
반응형
#ifndef NULL
#define NULL 0
#endif
const int SIZE = 100010;
struct Node{
	int num;
	Node* next;
	Node() : num(0), next(NULL) {}
	Node(int n, Node* np){
		num = n, next = np;
	}
	~Node() {
		delete next;
	}
	void erase(Node*& ref){
		ref = next;
		next = NULL;
		delete this;
	}
}buf[SIZE];
int bcnt;

struct Queue{
	Node*head, *tail;
	int cnt;
	Queue(){
		cnt = 0;
		head = tail = new Node();
	}
	~Queue(){
		cnt = 0;
		delete head;
		head = tail = NULL;
	}
}que;

Queue* newQueue(){
	return new Queue();
}

void delQueue(Queue*que){
	delete que;
}

bool empty(Queue*que){
	return que->cnt == 0;
}

int size(Queue*que){
	return que -> cnt;
}

int front(Queue*que){
	return que->head->num;
}

int back(Queue*que){
	return que->tail->num;
}

void push(Queue*que, int num){
	que->cnt++;
	que->tail->next = new Node(num, NULL);
	que->tail = que->tail->next;
}

void pop(Queue*que){
	if (empty(que)) return;
	que->cnt--;
	que->head->erase(que->head);
}
728x90
반응형

'Coding > Algorithms' 카테고리의 다른 글

[Algorithms] Hashtable / Probing / Hash Chaining / DJB2  (0) 2021.03.12
[Algorithms] Double Linked List 구현  (0) 2021.03.10
[Algorithms] Disjoint sets  (0) 2021.02.26
[Algorithms] Merge Sort  (0) 2021.02.19
내꺼비공개  (0) 2020.09.16