-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Queue_all_operations_using_linkedlist.cpp
114 lines (107 loc) · 2.61 KB
/
Queue_all_operations_using_linkedlist.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
114
/*Queue is a data structure which works on FIFO principle i.e First In First Out. Element inserted first will also get deleted first.Insertion will always take place at front and Deletion from Rear. You can understand Queue with the example of a Queue at Ticket counter. The people keep on adding in Queue from End and Exist from Beginning. */
#include<bits/stdc++.h>//Header file for all standard library
using namespace std;
struct Node //Structure of node of linked list
{
public:
int info;
Node *next;
} * ptr, *save, *rear, *front, *NEW;
Node *CN(int y) // For creating node
{
ptr = new Node;
ptr->info = y;
ptr->next = NULL;
return ptr;
}
void insert(Node *NEWPTR)// For inserting node
{
if (front == NULL)
{
front = rear = NEWPTR;
}
else
{
rear->next = NEWPTR;
rear = NEWPTR;
}
}
void Delete() //for deleting a node
{
if (front == NULL)
cout << "\nUnderflow!!";
else
{
ptr = front;
front = front->next;
delete ptr;
cout << "\nValue got deleted";
}
}
/*Time complexity:- O(n)
Space Complexity:- O(n)*/
void Display(Node *D) //for displaying the queue
{
if (D != NULL)
{
while (D != NULL)
{
cout << D->info << "-->";
D = D->next;
}
}
else
cout << "Empty!:(";
}
int main()
{
int a, x;
char ch = 'y';
do
{
cout << "\nWhat do u want to do with Queue\n1. Insert a node \n2. Delete a node \n3. Display\n Enter your choice:";
cin >> a;
switch (a)
{
case 1:
{
cout << "\nEnter the data of new node :";
cin >> x;
NEW = CN(x);
insert(NEW);
cout << "\nNew node with value " << x << " is inserted in Queue.";
}
break;
case 2:
{
Delete();
cout << "\nFront node is Deleted Successfully!!";
}
break;
case 3:
{
cout << "\nYour queue is:";
Display(front);
}
break;
default:
cout << "\nWANT TO DO NOTHING! IT's OK!";
}
cout << "\nWant to insert or delete an another node?(Y/N):";
cin >> ch;
} while (ch == 'y' || ch == 'Y');
return 0;
}
/* Time complexity for both insertion and deletion is:- O(1)
Space Complexity:- O(n)*/
/* Test case
Sample Input:
What do u want to do with Queue
1. Insert a node
2. Delete a node
3. Display
Enter your choice: 1
Enter the data of new node : 23
Sample Output:
New node with value 23 is inserted in queue.
*/