-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLinkedQueue.cpp
More file actions
87 lines (86 loc) · 1.8 KB
/
LinkedQueue.cpp
File metadata and controls
87 lines (86 loc) · 1.8 KB
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
#include<iostream>
using namespace std;
struct node{
int data;
struct node* next;
};
struct node* front;
struct node* rear;
void enqueue(int value){
struct node* new_node;
new_node= new node();
if(rear==NULL){
rear=new_node;
rear->data=value;
rear->next=NULL;
front=rear;
}
else{
rear->next=new_node;
new_node->data=value;
new_node->next=NULL;
rear=new_node;
}
}
void dequeue(){
struct node* temp;
temp=front;
if(rear==NULL&&front==NULL){
cout<<"Queue is empty, Deletion is not possible !!\n";
}
else if(front->next==NULL){
cout<<"The deleted element is: \n"<<front->data;
free(front);
front=NULL;
rear=NULL;
}
else{
temp=temp->next;
cout<<"The deleted element is:\n"<<front->data;
free(front);
front=temp;
}
}
void display(){
struct node* temp;
temp=front;
if(front==NULL&&rear==NULL){
cout<<"Queue is empty\n";
}
else{
cout<<"Elements in the queue are: ";
while(temp!=NULL){
cout<<"->"<<temp->data;
temp=temp->next;
}
cout<<"\n";
}
}
int main(){
int v,choice;
do{
cout<<"Enter the choice:\n1.Enqueue\n2.Dequeue\n3.Dispaly\n4.Exit\n";
cin>>choice;
switch(choice){
case 1:
printf("Enter the value: ");
cin>>v;
enqueue(v);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
cout<<"Exit Program\n";
break;
default:
cout<<"Invalid choice";
break;
}
}
while(choice!=4);
return 0;
}