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