Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* -------------------------- myNode.h ---------------------------*/
- #include <iostream>
- #ifndef myNode_h
- #define myNode_h
- class Node {
- friend void printN(Node &);
- private:
- char *clientName;
- double clientState;
- Node *next;
- public:
- Node();
- Node(char *, int);
- void setName(const char *);
- void setState(const double);
- void setNext(Node *);
- char* getName() const;
- double getState() const;
- Node * getNext() const;
- ~Node();
- };
- #endif
- /* -------------------------- myQueue.h ---------------------------*/
- #include <iostream>
- #ifndef myQueue_h
- #define myQueue_h
- class Queue {
- friend void printQ(Queue &);
- Node* head;
- Node* tail;
- public:
- Queue();
- void insertNode(Node &);
- void deQueue();
- Node *getLast();
- Node *peek();
- bool isEmpty();
- };
- #endif
- /* -------------------------- myNode.cpp ---------------------------*/
- #include <iostream>
- #include "myNode.h"
- #include "myQueue.h"
- #include <cstring>
- using namespace std;
- Node :: Node()
- {
- this->clientName = NULL;
- this->clientState=0;
- this->next=NULL;
- }
- Node :: ~Node()
- {
- delete []clientName;
- }
- 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;
- }
- void Node ::setNext(Node *next)
- {
- this->next=next;
- }
- char* Node :: getName() const
- {
- return (this->clientName);
- }
- double Node :: getState() const
- {
- return (this->clientState);
- }
- Node * Node :: getNext() const
- {
- return (this->next);
- }
- void printN(Node &n1)
- {
- cout << "Client Name: "<< n1.clientName;
- cout << " Client State: "<<n1.clientState << "$" << endl ;
- }
- /* -------------------------- myQueue.cpp ---------------------------*/
- #include <iostream>
- using namespace std;
- #include "myNode.h"
- #include "myQueue.h"
- using namespace std;
- Queue :: Queue()
- {
- head=NULL;
- tail=NULL;
- }
- void Queue :: insertNode(Node &n1)
- {
- if (head == NULL)
- {
- head=&n1;
- }
- else
- {
- tail->setNext(&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->getNext();
- }
- }
- 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->getNext();
- return myHead;
- }
- Node * Queue :: peek()
- {
- return this->head;
- }
- bool Queue :: isEmpty()
- {
- if (head == NULL)
- return true;
- return false;
- }
- void printQ(Queue &Q)
- {
- Node *location=Q.head;
- if (!location)
- {
- cout <<"There are no clients!"<<endl;
- }
- cout <<"The Current Queue is:"<<endl;
- while (location !=NULL)
- {
- printN(*location);
- location=location->getNext();
- }
- }
- /* ------------------------------------------ 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);
- printQ(q1);
- if(!q1.isEmpty())
- {
- peek=q1.peek();
- cout << "the first node in the queue is " << peek->getName()<<" " << peek->getState() <<endl ;
- }
- printQ(q1);
- last=q1.getLast();
- cout << "the node that was dequeued is " << last->getName()<<" " << last->getState() <<endl ;
- printQ(q1);
- q1.deQueue();
- printQ(q1);
- q1.deQueue();
- if(q1.isEmpty())
- cout<< "The Queue is empty" <<endl;
- q1.insertNode(n4);
- printQ(q1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement