Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* -------------------------- myNode.h ---------------------------*/
- #ifndef myNode_h
- #define myNode_h
- #include <iostream>
- class Node {
- friend class Queue;
- private:
- char *clientName;
- Node *next;
- double clientState;
- public:
- Node();
- Node(char *, int);
- void setName(const char *);
- void setState(const double);
- char* getName() const;
- double getState() const;
- ~Node();
- };
- #endif
- /* -------------------------- myQueue.h ---------------------------*/
- #include <iostream>
- using namespace std;
- #ifndef myQueue_h
- #define myQueue_h
- class Queue {
- private :
- Node* head;
- Node* tail;
- public:
- Queue();
- void insertNode(Node &);
- void deQueue();
- Node *getLast();
- Node *peek();
- bool isEmpty();
- void printQ();
- };
- #endif
- /* -------------------------- myNode.cpp ---------------------------*/
- #include <iostream>
- #include "myNode.h"
- #include <cstring>
- using namespace std;
- Node :: Node()
- {
- this->clientName = NULL;
- this->clientState=0;
- this->next=NULL;
- }
- Node :: ~Node()
- {
- if(this)
- {
- this->clientName = NULL;
- this->clientState=0;
- this->next=NULL;
- }
- }
- Node::Node(char *name, int state)
- {
- this->clientName = new char[strlen(name)+1];
- strcpy(clientName,name);
- this->clientState = state;
- this->next=NULL;
- }
- void Node::setName (const char *name)
- {
- strcpy(clientName,name);
- }
- void Node ::setState(const double state)
- {
- this->clientState = state;
- }
- char* Node :: getName() const
- {
- return (this->clientName);
- }
- double Node :: getState() const
- {
- return (this->clientState);
- }
- /* -------------------------- myQueue.cpp ---------------------------*/
- #include <iostream>
- using namespace std;
- #include "myNode.h"
- #include "myQueue.h"
- Queue :: Queue()
- {
- head=NULL;
- tail=NULL;
- }
- void Queue :: insertNode(Node &n1)
- {
- if (head == NULL)
- {
- head=&n1;
- }
- else
- {
- tail->next=&n1;
- }
- tail=&n1;
- }
- void Queue :: deQueue()
- {
- Node *myHead=head;
- if(!head)
- {
- cout<< "The Queue is empty" <<endl;
- }
- else
- {
- if(myHead == tail) //Special case if the queue had only 1 client
- {
- tail=NULL;
- head=NULL;
- }
- else
- head = head->next;
- }
- }
- Node * Queue :: getLast()
- {
- Node *myHead=head;
- if(!head)
- {
- cout<< "The Queue is empty" <<endl;
- return NULL;
- }
- if(myHead == tail) //Special case if the queue had only 1 client
- {
- tail=NULL;
- head=NULL;
- }
- else
- head = head->next;
- return myHead;
- }
- Node * Queue :: peek()
- {
- return this->head;
- }
- bool Queue :: isEmpty()
- {
- if (head == NULL)
- return true;
- return false;
- }
- void Queue :: printQ()
- {
- Node *location=head;
- if (!location)
- {
- cout <<"There are no clients!"<<endl;
- }
- cout <<"The Current Queue is:"<<endl;
- while (location !=NULL)
- {
- cout << "Client Name: "<< location->clientName;
- cout << " Client State: "<<location->clientState << "$" << endl ;
- location=location->next;
- }
- }
- /* ------------------------------------------ main.cpp --------------------------------------------------*/
- #include <iostream>
- #include "myNode.h"
- #include "myQueue.h"
- using namespace std;
- void main ()
- {
- Queue q1;
- Node n1("Jack",210),n2("Austin",15),n3("Steve",32),n4("Sean",312);
- Node * last, *peek;
- q1.insertNode(n1);
- q1.insertNode(n2);
- q1.insertNode(n3);
- q1.printQ();
- if(!q1.isEmpty())
- {
- peek=q1.peek();
- cout << "the first node in the queue is " << peek->getName()<<" " << peek->getState() <<endl ;
- }
- q1.printQ();
- last=q1.getLast();
- cout << "the node that was dequeued is " << last->getName()<<" " << last->getState() <<endl ;
- q1.printQ();
- q1.deQueue();
- q1.printQ();
- q1.deQueue();
- if(q1.isEmpty())
- cout<< "The Queue is empty" <<endl;
- q1.insertNode(n4);
- q1.printQ();
- }
Advertisement
Add Comment
Please, Sign In to add comment