-
Notifications
You must be signed in to change notification settings - Fork 16
/
queue_using_linkedlist.cpp
98 lines (84 loc) · 1.84 KB
/
queue_using_linkedlist.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
90
91
92
93
94
95
96
97
98
/*
Write a program to implement Queue using linkedlist.
*/
#include<iostream>
struct linkedlist{
int data;
linkedlist *next;
};
class stack_linkedList{
public:
linkedlist *front;
linkedlist *rear;
stack_linkedList(){
front=rear=NULL;
}
void enqueue(int);
int dequeue();
void display();
};
void stack_linkedList::enqueue(int ele){
linkedlist *temp=new linkedlist();
temp->data=ele;
temp->next=NULL;
if(front==NULL)
front=rear=temp;
else{
rear->next=temp;
rear=temp;
}
}
int stack_linkedList::dequeue(){
linkedlist *temp;
int ele;
if(front==NULL)
std::cout<<"\nStack is empty";
else{
temp=front;
ele=temp->data;
if(front==rear) //if length of queue is 1;
rear=rear->next;
front=front->next;
delete(temp);
}
return ele;
}
void stack_linkedList::display(){
if(front==NULL)
std::cout<<"\nStack is empty";
else {
linkedlist *temp;
temp=front;
while(temp!=NULL){
std::cout<<temp->data<<" ";
temp=temp->next;
}
}
}
int main(){
int op,data;
stack_linkedList ob;
std::cout<<"\n1. enqueue(Insertion) ";
std::cout<<"\n2. dequeue(Deletion)";
std::cout<<"\n3. Display";
std::cout<<"\n4. Exit";
while(1){
std::cout<<"\nEnter your choice ";
std::cin>>op;
if(op==1)
{
std::cout<<"Enter data ";
std::cin>>data;
ob.enqueue(data);
}
else if(op==2)
data=ob.dequeue();
else if(op==3)
ob.display();
else if(op==4)
exit(0);
else
std::cout<<"\nWrong choice ";
}
return 0;
}