-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueue.cpp
More file actions
146 lines (124 loc) · 3.18 KB
/
queue.cpp
File metadata and controls
146 lines (124 loc) · 3.18 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
138
139
140
141
142
143
#include "queue.h"
#include "stack.h"
#include <iostream>
#include <string>
using namespace std;
#define RESET "\033[0m"
#define RED "\033[31m"
attend::attend()
{
front = rear = NULL;
count = 0;
}
// Check if the queue is empty
bool attend::isEmpty()
{
return (count == 0);
}
void attend::addAttendee(string name)
{
NodeQueue* newNode = new NodeQueue{name, NULL};
if (isEmpty())
{
front = rear = newNode;
}
else
{
rear->next = newNode;
rear = newNode;
rear-> next = NULL;
}
count++;
}
/*
* @brief Delete the front attendee from the queue
* @details Removes the front node from the linked list and adjusts the front pointer.
*/
void attend::delAttendee(string val, stack& undo_attendee)
{
if (isEmpty())
{
cout << RED <<"Queue is Empty...!! \t You Can't delete one...!!" << RESET << endl;
return;
}
NodeQueue* temp = front;
NodeQueue* prev = NULL;
bool found = false;
while (temp != NULL)
{
if (temp->data == val)
{
found = true;
undo_attendee.push_attendee(val); // revise
break;
}
prev = temp;
temp = temp->next;
}
if (!found)
{
cout << RED << "\nElement not found in Our System..!!\n" << RESET;
return;
}
if(temp == front)
{
front = front->next;
if(front == NULL)
{
rear == NULL;
}
}
else{
prev->next = temp->next;
if(temp == rear)
rear = prev;
}
delete temp;
temp = NULL;
count--;
cout << "attendee " << val << " Deleted Successfully!" << endl;
}
/*
* @brief Display the list of attendees
* @details Iterates over the linked list and prints each element.
*/
void attend::disAttendees()
{
if (isEmpty())
{
cout << RED <<"\nQueue is Empty...!! \t Can't display anything...!!" << RESET << endl;
return;
}
NodeQueue* current = new NodeQueue;
current = front;
int k = 1;
while (current != NULL)
{
cout << k << " - " << current->data << endl;
current = current->next;
k++;
}
}
/*
* @brief Search for an attendee in the queue
* @return Position (0-based index), or -1 if not found
*/
int attend::attendSearch(string attendee)
{
if (isEmpty())
{
cout << RED << "\nThe queue is empty...!! \t There is nothing to search for...!!" << RESET <<endl;
return -1;
}
NodeQueue* current = new NodeQueue;
current = front;
int i = 0;
while (current != NULL)
{
if (current->data == attendee)
return i;
current = current->next;
i++;
}
return -1; // Not found
}