Advertisement
illeas

stack and queue using linked list

Sep 4th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Node{
  6.     int data;
  7.     Node *link;
  8. };
  9.  
  10. Node *top = NULL;
  11. Node *fronts = NULL;
  12. Node *rear = NULL;
  13.  
  14. bool isEmpty(){
  15.     if(top == NULL){
  16.         return true;
  17.     }else{
  18.         return false;
  19.     }
  20. }
  21.  
  22. bool qisEmpty(){
  23.     if(fronts == NULL && rear == NULL){
  24.         return true;
  25.     }else{
  26.         return false;
  27.     }
  28. }
  29.  
  30. void enqueue(int value){
  31.     Node *p = new Node();
  32.     p->data = value;
  33.     p->link = NULL;
  34.  
  35.     if(fronts == NULL){
  36.         fronts = p;
  37.         rear = p;
  38.     }else{
  39.         rear->link = p;
  40.         rear = p;
  41.     }
  42. }
  43.  
  44. void dequeue(){
  45.     if(qisEmpty()){
  46.         cout << "Queue is Empty!";
  47.     }else{
  48.         if(fronts == rear){
  49.             free(fronts);
  50.             fronts = rear = NULL;
  51.         }else{
  52.             Node *p = fronts;
  53.             fronts = fronts->link;
  54.             free(p);
  55.         }
  56.     }
  57. }
  58.  
  59. void showfront(){
  60.     if(qisEmpty()){
  61.         cout << "Queue is Empty!";
  62.     }else{
  63.         cout << fronts->data;
  64.     }
  65.  
  66. }
  67.  
  68. void displayQueue(){
  69.     if(qisEmpty()){
  70.         cout << "Queue is Empty!";
  71.     }else{
  72.         Node *p = fronts;
  73.         while(p != NULL){
  74.             cout << p->data << " ";
  75.             p = p->link;
  76.         }
  77.     }
  78. }
  79.  
  80. void push(int value){
  81.     Node *p = new Node();
  82.     p->data = value;
  83.     p->link =top;
  84.     top = p;
  85. }
  86.  
  87. void pop(){
  88.     if(isEmpty()){
  89.         cout << "Stack is Empty!";
  90.     }else{
  91.         Node *p = top;
  92.         top = top->link;
  93.         delete(p);
  94.     }
  95. }
  96.  
  97. void showTop(){
  98.     if(isEmpty()){
  99.         cout << "Stack is Empty!";
  100.     }else{
  101.         cout << "Element at top is: " << top->data;
  102.     }
  103. }
  104.  
  105. int main(){
  106.     //Stack
  107.     push(1);
  108.     push(2);
  109.     pop();
  110.     push(5);
  111.     showTop();
  112.  
  113.     //Queue
  114.     int choice, flag=1, value;
  115.     while(flag==1){
  116.         cout<<"\n1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit\n";
  117.         cin>>choice;
  118.         switch (choice){
  119.             case 1: cout<<"Enter Value:\n";
  120.               cin>>value;
  121.               enqueue(value);
  122.               break;
  123.             case 2: dequeue();
  124.               break;
  125.             case 3: showfront();
  126.               break;
  127.             case 4: displayQueue();
  128.               break;
  129.             case 5: flag = 0;
  130.               break;
  131.         }
  132.     }
  133.  
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement