Queue Insert Delete Implementation and Operations in Data Structures (C plus plus)
In this tutorial, we will learn about Queue Insert Delete Implementation and Operations in Data Structures (C plus plus).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
/*C++ Program to Implement Queue using Linked List*/ #include<iostream> using namespace std; struct node { int info; node *next; } *front = NULL,*rear = NULL,*p = NULL,*np = NULL; void push(int x) { np = new node; np->info = x; np->next = NULL; if(front == NULL) { front = rear = np; rear->next = NULL; } else { rear->next = np; rear = np; rear->next = NULL; } } int del() { int x; if(front == NULL) { cout<<"The queue is empty \n"; } else { p = front; x = p->info; front = front->next; delete(p); return(x); } } int main() { int num, z = 0 , val; cout<<"Please enter the number of values to be entered into queue: \n"; cin>>num; while (z < num) { cout<<"Please enter the values to be entered into queue: \n"; cin>>val; push(val); z++; } cout<<"\n\nRemoved Values are following: \n\n"; while(true) { if (front != NULL) cout<<del()<<endl; else break; } } |
Output:
