-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData Structure - Linear (Queue).cpp
154 lines (139 loc) · 3 KB
/
Data Structure - Linear (Queue).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
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
144
145
146
147
148
149
150
151
152
153
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include "stdafx.h"
using namespace std;
class Node{
public:
int data;
Node* next;
Node(){
data = 0;
next = NULL;
};
};
class Queue{
public:
Node*front;
Node*tail;
Queue(){
front = tail = NULL;
};
//Queue Operation
//1- isEmpty()
bool isEmpty(){
if (front == NULL &&tail == NULL)
return true;
else
return false;
}
//2- Enqueue() - add new items to queue
void enqueue(int newVal){
Node* newNode = new Node();
if (isEmpty()){
//if queue is empty so that head & tail are equal the new node
newNode->data = newVal;
front = tail = newNode;
}else{
newNode->data = newVal;
tail->next = newNode;
tail = newNode;
}
};
//3- Display() - Traversing on Queue
void display(){
if (isEmpty()){
cout << "Queue is Empty! \n";
}else{
Node*temp;
temp = front;
while (temp != NULL){
cout << temp->data << " ";
temp = temp->next;
};
cout << endl;
};
};
//4- Dequeue(); - Remove element from Queue
int dequeue(){
int saveDeletedValue = -1;
if (isEmpty()){
//if queue has no items
cout << "Queue is Empty! \n";
}else if (front == tail){
//if queue has the last item
delete front;
front = tail = NULL;
}else{
//if queue has more than one item
Node*delPtr;
delPtr = front;
front = front->next;
//to store removed value before delete it from memory
saveDeletedValue = delPtr->data;
delete delPtr;
};
return saveDeletedValue;
};
//5- getFront() - return the first item in queue
int getFront(){
return front->data;
};
//6- getTail() - return the last item in queue
int getTail(){
return tail->data;
};
//7- counter() - return the number of items in queue
int counter(){
int count = 0;
Node*temp = front;
while (temp!=NULL){
count++;
temp = temp->next;
};
return count;
};
//8- search() - check if item is found in queue
bool search(int val){
bool found = false;
Node*temp = front;
while (temp!=NULL){
if (temp->data == val)
found = true;
temp = temp->next;
};
return found;
};
//9- clear() - remove all queue items
void clear(){
while (!isEmpty()){
dequeue();
};
};
};
int _tmain()
{
///////////////////////////////////////////////////////////////////
Queue LinkedQueue;
for (int i = 0; i < 4; i++){
int val;
cout << "enter item number " << i+1 <<" to enqueue: \n";
cin >> val;
LinkedQueue.enqueue(val);
};
LinkedQueue.display();
cout << "Queue has " << LinkedQueue.counter() << " items\n";
///////////////////////////////////////////////////////////////////
for (int i = 0; i < 2; i++){
cout << LinkedQueue.dequeue() << " removed from queue! \n";
};
cout << "After Dequeue \n";
LinkedQueue.display();
///////////////////////////////////////////////////////////////////
cout << "Clear all Queue Items \n";
LinkedQueue.clear();
LinkedQueue.display();
////////////////////////**Program End**////////////////////////////
system("pause");
return 0;
}