Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- struct node
- {
- node*next;
- int val;
- };
- bool isEmpty(node *head)
- {
- if (head)return true;
- else false;
- }
- void enqueue(node *&head, node *&tail, int v)
- {
- node * e = new node;
- e->val =v;
- e->next = nullptr;
- if (tail) tail->next = e;
- else head = e;
- tail = e;
- }
- int dequeue(node *&head, node *&tail)
- {
- if (head)
- {
- node*e = head;
- int tmp = head->val;
- head = head->next;
- delete e;
- if (!head)tail = nullptr;
- cout << "element popped: " << tmp << endl;
- return tmp;
- }
- }
- void showHT(node *head, node *tail)
- {
- if (head)
- {
- cout << "head: [" << head->val << "] " << "tail: [" << tail->val << "]" << endl;
- }
- else cout << "head & tail : NULL" << endl;
- }
- int main()
- {
- node * head = nullptr;
- node * tail = nullptr;
- dequeue(head,tail);
- showHT(head, tail);
- dequeue(head,tail);
- showHT(head, tail);
- enqueue(head,tail,9);
- showHT(head, tail);
- enqueue(head,tail,2);
- showHT(head, tail);
- enqueue(head,tail,5);
- showHT(head, tail);
- dequeue(head,tail);
- showHT(head, tail);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement