Queue
- 일단 오늘은 queue까지 구현.
- 인적성부터 cs관련 공부까지 해야할 것이 너무많다
- 최선을 다하자
#include <iostream>
using namespace std;
class Node
{
Node * nextN;
int key;
public:
Node(){}
Node(int key): key(key) {}
void setNext(Node * n){
nextN = n;
}
Node* getNext(){
return nextN;
}
int getKey(){
return key;
}
};
class Queue
{
private:
Node* front;
Node* back;
Node* end;
public:
Queue(){
front = new Node();
back = new Node();
front->setNext(back);
back->setNext(back);
end = front;
}
~Queue(){
clear();
delete front;
delete back;
}
void push(int key){ // in
Node* temp = new Node(key);
end->setNext(temp);
temp->setNext(back);
end = temp;
}
int pop(){ // out
Node* temp = front->getNext();
front->setNext(temp->getNext());
int res = temp->getKey();
delete temp;
return res;
}
void showList(){ // 출력
Node* temp = front->getNext();
while(temp != back){
cout<<temp->getKey()<<" ";
temp = temp->getNext();
}
cout<<endl;
}
void clear(){ // 전체 pop
Node* temp = front->getNext();
while(temp != back){
Node* del = temp;
temp = temp->getNext();
delete del;
}
front->setNext(back);
}
};
int main(){
cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
Queue q = Queue();
q.push(10);
q.push(20);
q.showList();
q.push(22);
q.showList();
q.pop();
q.showList();
q.clear();
q.showList();
return 0;
}