Queue Implementation Using Linked List C++
Animated picture is here. So need to wait for some seconds to load it completely.
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 69 70 71 72 73 74 |
#include<iostream> using namespace std; struct Node { int data; Node *next ; }; class Queue { Node *front, *rear; public: Queue() { front = rear = NULL; } void Enqueue(int elem) // insertion values from rear side { Node *newnode; newnode = new Node; newnode->data = elem; newnode->next = NULL; if(front == NULL) front = rear = newnode; else { rear->next = newnode; rear = newnode; } } void Dequeue() // delete values from front side { Node *temp; if(front == NULL) cout<<"Queue is Empty"; else { temp= front; front = front->next; delete temp; } } void show() { Node *temp; temp= front; while(temp!=NULL) { cout<<temp->data<<"\t"; temp = temp->next; } cout<<endl; } }; int main() { Queue T4TUTORIALS; T4TUTORIALS.Enqueue(10); cout<<"Queue after inserting the 1st values is :"; T4TUTORIALS.show(); T4TUTORIALS.Enqueue(4); cout<<"Queue after inserting the 2nd value is :"; T4TUTORIALS.show(); T4TUTORIALS.Dequeue(); cout<<"Queue after deleting a value from the queue:"; T4TUTORIALS.show(); } |