View difference between Paste ID: etg3s0Cj and wKdhKVU2
SHOW: | | - or go back to the newest paste.
1
/* -------------------------- myNode.h  ---------------------------*/
2
#ifndef myNode_h
3
#define myNode_h
4
#include <iostream>
5
6
7
class Node {
8
 friend class Queue;
9
private:
10
	char *clientName;
11
	Node *next;
12
	double clientState;
13
	
14
public:
15
	Node();
16
	Node(char *, int);
17
	void setName(const char *);
18
	void setState(const double);
19
	char* getName() const;
20
	double getState() const;
21
	~Node();
22
};
23
24
#endif
25
26
27
28
29
30
31
32
/* -------------------------- myQueue.h  ---------------------------*/
33
#include <iostream>
34
using namespace std;
35
36
#ifndef myQueue_h
37
#define myQueue_h
38
39
class Queue {
40-
friend void printQ();
40+
41
private :
42
43
	Node* head;
44
	Node* tail;
45
46
public:
47
48
	Queue();
49
	void insertNode(Node &);
50-
	void insertNode(Node);
50+
	void deQueue();
51
	Node *getLast();
52
	Node *peek();
53
	bool isEmpty();
54
	void printQ();
55
};
56
57
#endif 
58-
void printQ(Queue &Q) 
58+
59
60-
	Node *location=	Q.head;
60+
61
62-
	if (!location) { cout <<"There are no clients!"<<endl; }
62+
63-
	while (location)
63+
64
#include "myNode.h"
65-
	//	cout << "Client Name: "<< location->clientName ;
65+
66-
	//	cout << " Client State: "<<location->clientState << endl ;
66+
67-
	//	location=location->next;
67+
68
69
Node :: Node()
70
{
71
	this->clientName = NULL;
72
	this->clientState=0;
73
	this->next=NULL;
74
}
75
76
Node :: ~Node()
77
{
78
  if(this)
79
  {
80
	  this->clientName = NULL;
81
	this->clientState=0;
82
	this->next=NULL;
83
  }
84
}
85
Node::Node(char *name, int state)
86
{
87
	this->clientName = new char[strlen(name)+1];
88
	strcpy(clientName,name);
89
	this->clientState = state;
90
	this->next=NULL;
91
}
92
93
94
void  Node::setName (const char *name)
95
{
96
	strcpy(clientName,name);
97
}
98
99
100
void  Node ::setState(const double state)
101
{
102
	this->clientState = state;
103
}
104
105
106
107
char* Node :: getName() const
108
{
109
	return (this->clientName);
110
}
111
112
double Node :: getState() const
113
{
114
	return (this->clientState);
115
}
116
117
118
119
120
121
122
/* -------------------------- myQueue.cpp  ---------------------------*/
123
124-
	delete []clientName;
124+
125
using namespace std;
126
#include "myNode.h"
127
#include "myQueue.h"
128
129
Queue :: Queue()
130
{
131
    head=NULL;
132
	tail=NULL;
133
}
134
135
void Queue :: insertNode(Node &n1)
136
{
137
	if (head == NULL)
138
	{
139
       head=&n1; 
140-
	this->head=NULL;
140+
141-
	this->tail=NULL;
141+
	else 
142
	{
143
		tail->next=&n1;
144
	}
145-
void Queue :: insertNode(Node n1)
145+
	tail=&n1;
146
}
147-
	if (this->head == NULL)
147+
148-
		this->tail = this->head = &n1;
148+
void Queue :: deQueue()
149
{
150-
		this->tail->next=&n1;
150+
	Node *myHead=head;
151
	if(!head)
152
	{
153
		cout<< "The Queue is empty" <<endl;
154
	}
155
	else
156-
	Node *myHead=this->head;
156+
157-
	this->head = this->head->next;
157+
	if(myHead == tail) //Special case if the queue had only 1 client
158
	{
159
		tail=NULL;
160
		head=NULL;
161
	}
162
	else
163
	head = head->next;
164
	}
165
}
166
167
Node * Queue :: getLast()
168
{
169
	Node *myHead=head;
170-
	if (this->head == NULL)
170+
	if(!head)
171
	{
172
		cout<< "The Queue is empty" <<endl;
173
    return NULL;
174
	}
175
	if(myHead == tail) //Special case if the queue had only 1 client
176
	{
177
		tail=NULL;
178
		head=NULL;
179
	}
180
	else
181
	head = head->next;
182
	return myHead;
183
}
184
185
186
Node * Queue :: peek()
187
{
188
	return this->head;
189
}
190
191
192
bool Queue ::  isEmpty()
193
{
194
	if (head == NULL)
195
		return true;
196
	return false;
197
}
198
199
void Queue :: printQ() 
200
{
201
	Node *location=head;
202
203
	if (!location) 
204
	{ 
205
		cout <<"There are no clients!"<<endl; 
206
	}
207
	cout <<"The Current Queue is:"<<endl;
208
	while (location !=NULL)
209
	{
210
		cout << "Client Name: "<< location->clientName;
211
		cout << " Client State: "<<location->clientState << "$" << endl ;
212
		location=location->next;
213
	}
214
}
215
216
217
218
219
220
221
/* ------------------------------------------ main.cpp --------------------------------------------------*/
222
#include <iostream>
223
#include "myNode.h"
224
#include "myQueue.h"
225
226
using namespace std;
227
228
void main ()
229
{
230
	Queue q1;
231
 Node n1("Jack",210),n2("Austin",15),n3("Steve",32),n4("Sean",312);
232
 Node * last, *peek;
233
 q1.insertNode(n1);
234
 q1.insertNode(n2);
235
 q1.insertNode(n3);
236
 q1.printQ();
237
 if(!q1.isEmpty())
238
 {
239
 peek=q1.peek();
240
 cout << "the first node in the queue is " << peek->getName()<<" " << peek->getState() <<endl ;
241
 }
242
 q1.printQ();
243
 last=q1.getLast();
244
 cout << "the node that was dequeued is " << last->getName()<<" " << last->getState() <<endl ;
245
 q1.printQ();
246
 q1.deQueue();
247
 q1.printQ();
248
 q1.deQueue();
249
 if(q1.isEmpty())
250
	 cout<< "The Queue is empty" <<endl;
251
 q1.insertNode(n4);
252
 q1.printQ();
253
}