forked from projectX-india/madlab-hack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.cpp
89 lines (73 loc) · 1.65 KB
/
queue.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
78
79
80
81
82
83
84
85
86
87
88
89
#include<iostream>
using namespace std;
struct node{
int data;
struct node *next;
node(int d)
{
data=d;
next=NULL;
}
};
struct queue{
struct node *front,*rear;
queue(){
front=rear=NULL;
}
void enqueue(int d)
{
node* temp=new node(d);
if(front==NULL)
{
rear=front=temp;
cout<<"\nElement enqueued is : "<<rear->data;
}
else{
rear->next=temp;
rear=temp;
cout<<"\nElement enqueued is : "<<rear->data;
}
}
void dequeue(){
struct node* temp;
if(front==NULL)
{
cout<<"\nQueue is empty (underflow)";
}
else{
temp=front;
front=front->next;
if(front==NULL)
rear==NULL;
temp->next=NULL;
cout<<"\nElement dequeued is : "<<temp->data;
free(temp);
}
}
void display(){
struct node* temp;
if(rear==NULL && front==NULL)
{
cout<<"\nQueue is empty";
}
else{
cout<<"\nElements in the queue are : ";
struct node *temp=front;
while (temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
}
};
int main(){
queue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
q.dequeue();
q.display();
}