-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_ll.cpp
More file actions
137 lines (111 loc) · 1.82 KB
/
Copy pathqueue_ll.cpp
File metadata and controls
137 lines (111 loc) · 1.82 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
using namespace std;
int SIZE;
void setSize(int sz)
{
SIZE = sz;
}
struct Item
{
int val;
Item* next;
};
int size(Item* front)
{
int count = 0;
while(front != NULL)
{
count++;
front = front->next;
}
return count;
}
bool full(Item* front)
{
int temp = size(front);
return(temp == SIZE);
}
bool empty(Item* front, Item* rear)
{
return (front == NULL && rear == NULL);
}
void appendEnd(Item*& front, int va, Item*& rear)
{
if(full(front))
{
cout << "Queue is full" << endl;
return;
}
Item* newptr = new Item;
newptr->val = va;
newptr->next = NULL;
if(empty(front, rear))
{
front = newptr;
rear = newptr;
}
else
{
rear->next = newptr;
rear = newptr;
}
}
void deleteBegin(Item*& front, Item*& rear)
{
if(empty(front,rear))
{
cout << "Queue is empty" << endl;
}
else if(front == rear)
{
front = NULL;
rear = NULL;
free(front);
}
else
{
Item* temp = front;
front = temp->next;
free(temp);
}
}
int frontValue(Item* ptr)
{
return ptr->val;
}
void displayQueue(Item* ptr)
{
if(ptr == NULL)
{
return;
}
else
{
cout << ptr->val << " ";
displayQueue(ptr->next);
}
}
int main()
{
setSize(5);
Item* front = NULL;
Item* rear = NULL;
deleteBegin(front, rear);
appendEnd(front,6, rear);
appendEnd(front,7, rear);
appendEnd(front,8, rear);
appendEnd(front,9, rear);
appendEnd(front,10, rear);
displayQueue(front);
cout << endl;
appendEnd(front,10, rear);
displayQueue(front);
cout << endl;
cout << frontValue(front) << endl;
deleteBegin(front, rear);
deleteBegin(front, rear);
displayQueue(front);
cout << endl;
cout << frontValue(front) << endl;
return 0;
}