forked from manishaparuthi/manisha_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueuesll.cpp
182 lines (175 loc) · 3.7 KB
/
queuesll.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include<iostream>
using namespace std;
#include<cstring>
class SNode
{
/*
objective: Create a class for a Node for Single Linked list
input parameters: none
output value: none
description: SNode class defines the node structure
approach: Class defines data item is names element with datatype string
and link is named next pf snode type
*/
/*public:
SNode(string v)
{
elem = v;
next = NULL;
}*/
private:
int elem;
SNode* next;
friend class Stack; // provides SLinkedList access to SNode
};
class Queue
{
/*
objective: Create a Queue class
input parameters: none
output value: none
side effects: Class Queue defines Queue class
approach: Class definition
*/
public:
Queue()
{
head=NULL;
} // empty list constructor
~Queue()
{
delete head;
} // destructor
bool empty() const; // is list empty?
const int& front() const; // get front element
void push(const int& e); // add to front of list
//void addBack(const string& e); // add to back of list
void pop(); // remove from front
//void removeEnd(); // remove from end
void print(); // prints the SLL
SNode* newNode(const int& e)
{
SNode* temp=new SNode();
temp->next=NULL;
temp->elem=e;
return temp;
}
private:
SNode* head; // pointer to the head of list
};
bool Queue::empty() const
{
return head==NULL;
}
const int& Queue::front() const
{
return head->elem;
}
/*void Stack::push(const int& e)
{
SNode* node =newNode(e);
if(head==NULL)
{
head=node;
}
else
{
node->next=head;
head=node;
} //SNode* node =new SNode(e);
}
*/
void Queue::push(const int& e)
{
SNode* node =newNode(e);
if (head==NULL) {
head=node;
}
else {
SNode *temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = node;
}
}
void Queue::pop()
{
if(head->next==NULL)
{
head=NULL;
}
else if(!empty())
{
head=head->next;
}
}
/*void SLinkedList::removeEnd()
{
SNode *temp = head;
if(head->next==NULL)
{
head=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
}
delete temp;
}
*/
void Queue::print()
{
SNode* temp=head;
cout<<"\n Stack is: ";
while(temp!=NULL)
{
cout<<temp->elem<<" ";
temp=temp->next;
}
}
int main()
{
Queue obj;
while(1) {
cout << "\n1.Enqueue\n2.Dequeue\n3.Front element\n4.Display\n5.Exit\n";
int choice;
cout << "\nEnter Your Choice\t:\t";
cin >> choice ;
int temp;
switch(choice) {
case 1 : cout << "\nEnter New Element\t:\t";
cin>>temp;
obj.push(temp);
break;
case 2 : obj.pop();
break;
case 3 : if(obj.empty())
{
cout << "\nQueue is Empty.\n";
}
else
{
temp=obj.front();
cout<<"Element at the top is: "<<temp;
}
break;
case 4 : if(obj.empty())
{
cout << "\nQueue is Empty.\n";
}
else
{
obj.print();
}
break;
case 5 : exit(0);
break;
default: cout << "\nInvalid Input\n";
}
}
return 0;
}