-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue using linked list.cpp
102 lines (94 loc) · 2.04 KB
/
queue using linked list.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
99
100
101
102
#include <iostream>
#include <stdlib.h>
using namespace std;
struct node{
int data;
struct node *next;
};
struct node *front=0;
struct node *rear=0;
void enQueue(int val)
{
struct node *new_node;
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=val;
new_node->next=0;
if (front==0 && rear==0)
{
front=new_node;
rear=new_node;
}
else
{
rear->next=new_node;
rear=new_node;
}
}
void peek()
{
if (front==0)
cout<<"List is empty."<<endl;
else
cout<<"Peeked value: "<<front->data<<endl;
}
void deQueue()
{
struct node *temp;
temp=front;
cout<<"Deleted item: "<<front->data<<endl;
front=front->next;
free(temp);
}
void display()
{
struct node *temp;
temp=front;
while(temp->next!=0)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<temp->data<<endl;
}
int main ()
{
int choice,value;
do{
cout<<"1 for enQueue"<<endl;
cout<<"2 for deQueue"<<endl;
cout<<"3 for peek"<<endl;
cout<<"4 for display"<<endl;
cout<<"0 to exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the data: ";
cin>>value;
enQueue(value);
break;
case 2:
if (front==0)
cout<<"List is empty."<<endl;
else
deQueue();
cout<<endl;
break;
case 3:
peek();
cout<<endl;
break;
case 4:
if (front==0)
cout<<"List is empty."<<endl;
else
display();
cout<<endl;
break;
case 0:
return 0;
}
}while (choice<5);
return 0;
}