This repository has been archived by the owner on Oct 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathLinkedListQueue.cpp
77 lines (71 loc) · 1.55 KB
/
LinkedListQueue.cpp
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
75
76
77
#include <bits/stdc++.h>
using namespace std;
struct node {
int val;
node* link;
node(int num)
{
val = num;
link = NULL;
}
};
struct Queue{
node *front, *rear;
Queue()
{
front=NULL;
rear=NULL;
}
void enqueue(int x)
{
node* temp = new node(x);
if (rear == NULL) {
front = temp;
rear = temp; // adding first element
return;
}
rear->link = temp; //linking to next node and assigning the valye temp
rear = temp;
}
void dequeue()
{
if (front == NULL){
rear = NULL;
return; //no elements to delete
}
node* temp = front;
front = front->link; //removing element 1 by linking the head to the next node
}
void Display() {
node* temp;
temp = front;
if ((front == NULL) && (rear == NULL)) { // if front and rear ==null then no elements in queue
cout<<"Empty Queue\n";
return;
}
cout<<"Elements in the current Queue are : ";
while (temp != NULL) {
cout<<temp->val<<" "; //printing from front to rear
temp = temp->link;
}
}
};
int main()
{
Queue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.dequeue();
q.enqueue(40);
q.Display();
cout<<"\n";
q.enqueue(50);
q.enqueue(60);
q.enqueue(70);
q.dequeue();
q.enqueue(80);
q.dequeue();
q.Display();
cout<<"\n";
}