Advertisement
Guest User

Ex4 finished v2

a guest
Nov 22nd, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* -------------------------- myNode.h  ---------------------------*/
  2. #include <iostream>
  3. #ifndef myNode_h
  4. #define myNode_h
  5.  
  6. class Node {
  7.     friend void printN(Node &);
  8. private:
  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.  
  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. };
  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. Node::Node(char *name, int state)
  79. {
  80.     this->clientName = new char[strlen(name)+1];
  81.     strcpy(clientName,name);
  82.     this->clientState = state;
  83.     this->next=NULL;
  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.     {
  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.     {
  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.     {
  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.     if (head == NULL)
  200.         return true;
  201.     return false;
  202. }
  203.  
  204. void printQ(Queue &Q)
  205. {
  206.     Node *location=Q.head;
  207.  
  208.     if (!location)
  209.     {
  210.         cout <<"There are no clients!"<<endl;
  211.     }
  212.     cout <<"The Current Queue is:"<<endl;
  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.insertNode(n2);
  237.  q1.insertNode(n3);
  238.  printQ(q1);
  239.  if(!q1.isEmpty())
  240.  {
  241.  peek=q1.peek();
  242.  cout << "the first node in the queue is " << peek->getName()<<" " << peek->getState() <<endl ;
  243.  }
  244.  printQ(q1);
  245.  last=q1.getLast();
  246.  cout << "the node that was dequeued is " << last->getName()<<" " << last->getState() <<endl ;
  247.  printQ(q1);
  248.  q1.deQueue();
  249.  printQ(q1);
  250.  q1.deQueue();
  251.  if(q1.isEmpty())
  252.      cout<< "The Queue is empty" <<endl;
  253.  q1.insertNode(n4);
  254.  printQ(q1);
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement