forked from deepanshumishra/JavaAndCPP_programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueLL.cpp
More file actions
113 lines (104 loc) · 1.4 KB
/
Copy pathQueueLL.cpp
File metadata and controls
113 lines (104 loc) · 1.4 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include<iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
typedef struct node * NODE;
NODE front = NULL, rear = NULL;
void enqueue()
{
NODE temp;
int x;
temp = (NODE)malloc(sizeof(NODE));
cout<<"Enter data : ";
cin>>x;
temp->data = x;
if(rear == NULL)
{
rear = temp;
temp->next = NULL;
front = temp;
}
else
{
rear->next = temp;
rear = temp;
temp->next = NULL;
}
}
void dequeue()
{
NODE ptr;
if(front==NULL && rear==NULL)
cout<<"Queue is empty.";
else if (front==rear)
{
ptr = front;
cout<<"Deleted item : "<<ptr->data;
ptr->next = NULL;
free(ptr);
front = NULL;
rear = NULL;
}
else
{
ptr = front;
cout<<"Deleted item : "<<ptr->data;
front = ptr->next;
ptr->next = NULL;
free(ptr);
}
}
void display()
{
if (front==NULL && rear==NULL)
cout<<"Queue is empty.";
else
{
NODE ptr;
ptr = front;
while(ptr!=rear)
{
cout<<ptr->data<<" --> ";
ptr = ptr->next;
}
cout<<ptr->data<<" --> ";
cout<<"NULL.";
}
}
int main()
{
int choice;
while(1)
{
cout<<"ENTER CHOICE"<<endl<<"1. ENQUEUE"<<endl<<"2. DEQUEUE"<<endl<<"3. TRAVERSE"<<endl<<"4. EXIT"<<endl;
cin>>choice;
switch(choice)
{
case 1:
{
enqueue();
break;
}
case 2:
{
dequeue();
break;
}
case 3:
{
display();
break;
}
case 4:
{
exit(1);
break;
}
}
cout<<endl;
}
return 0;
}