forked from arnab2001/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplementing-Queue-Using-Linked-List.cpp
113 lines (113 loc) · 2.57 KB
/
Implementing-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
#include <bits/stdc++.h>
using namespace std;
typedef struct node
{
int x;
node *next;
}node;
node **enqueue(node *front, node *temp, node *rear, node *newnode, node *arr[2])
{
rear = arr[0];
front = arr[1];
newnode = (node *) malloc (sizeof (node));
cout << "Enter the value to insert : " << endl;
cin >> newnode -> x;
newnode -> next = NULL;
if (front == NULL)
{
rear = newnode;
front = newnode;
temp = newnode;
}
else
{
rear -> next = newnode;
rear = newnode;
}
arr[0] = rear;
arr[1] = front;
return arr;
}
node **dequeue (node *front, node *temp, node *rear, node *newnode, node *arr[2])
{
rear = arr[0];
front = arr[1];
if (front == NULL)
{
cout << "Underflow" << endl;
}
else
{
temp = front;
front = front -> next;
free (temp);
cout << "**************The element at front is deleted***************" << endl;
}
arr[0] = rear;
arr[1] = front;
return arr;
}
node **display(node *front, node *temp, node *rear, node *newnode, node *arr[2])
{
rear = arr[0];
front = arr[1];
temp = front;
if (rear == NULL || front == NULL)
{
cout << "The queue is empty !!" << endl;
}
else
{
cout << "***************The queue is displaying***********************" << endl;
while (temp != NULL)
{
cout << temp -> x << "->" << " ";
temp = temp -> next;
}
cout << "\n";
}
return arr;
}
int main()
{
int choice = 1;
node *front = NULL;
node *temp = NULL;
node *rear = NULL;
node *newnode = NULL;
node* arr[2];
node **p;
arr[0] = rear;
//cout << arr[0];
arr[1] = front;
while (choice)
{
cout << "Press 1 for enqueue operation" << endl;
cout << "Press 2 for dequeue operation" << endl;
cout << "Press 3 for display operation" << endl;
cout << "Press 0 for the end of the program" << endl;
cout << "Enter your choice : " << endl;
cin >> choice;
if (choice == 1)
{
p = enqueue(front, temp, rear, newnode, arr);
}
else if (choice == 2)
{
p = dequeue(front, temp, rear, newnode, arr);
}
else if (choice == 3)
{
p = display(front, temp, rear, newnode, arr);
}
else if (choice == 0)
{
cout << "The program is ended" << endl;
}
else
{
cout << "Wrong Choice" << endl;
}
}
return 0;
}