-
Notifications
You must be signed in to change notification settings - Fork 0
/
QUEUE USING LINKED-LIST.cpp
113 lines (95 loc) · 1.98 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
103
104
105
106
107
108
109
110
111
112
113
/*Queue - Linked List implementation*/
#include<iostream>
#include<stdlib.h>
using namespace std;
struct Node
{
int data;
struct Node* next;
};
// Two glboal variables to store address of front and rear nodes.
struct Node* front = NULL;
struct Node* rear = NULL;
// To Enqueue an integer
void Enqueue()
{
int x;
cout<<"Enter the element...\n";
cin>>x;
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data =x;
temp->next = NULL;
if(front == NULL && rear == NULL)
{
front = rear = temp;
}
else
{
rear->next = temp;
rear = temp;
}
cout<<"Element inserted...\n";
}
// To Dequeue an integer.
void Dequeue()
{
struct Node* temp = front;
if(front == NULL) {
printf("Queue is Empty...\n");
return;
}
else if(front == rear)
{
cout<<"---------------------------\n";
cout<<"Deleted item is : "<<front->data<<endl;
front = rear = NULL;
}
else
{
cout<<"---------------------------\n";
cout<<"Deleted item is : "<<front->data<<endl;
front = front->next;
}
free(temp);
}
void Print()
{
system("cls");
cout<<"---------------------------\n";
cout<<"Queue elements are : \n";
struct Node* temp = front;
while(temp != NULL) {
printf("%d \n",temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
int ch;
while(1)
{
cout<<"---------------------------\n";
cout<<"1. Enqueue\n";
cout<<"2. Dequeue\n";
cout<<"3. Traverse\n";
cout<<"4. Quit\n";
cout<<"Enter your choice :\n";
cin>>ch;
switch(ch)
{
case 1: Enqueue();
break;
case 2: Dequeue();
break;
case 3: Print();
break;
case 4: exit(1);
default :
system("cls");
cout<<"---------------------------\n";
cout<<" Invalid input\n" ;
}
}
return 0;
}