Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include "Queue.h"
- using namespace std;
- template <class KeyType>
- DListNode<KeyType>::DListNode(KeyType element)
- {
- next=0;
- previous=0;
- data=element;
- }
- template <class KeyType>
- void DListNode<KeyType>::setData(KeyType pVal)
- {
- data=pVal;
- }
- template <class KeyType>
- KeyType DListNode<KeyType>:: getData()
- {
- return data;
- }
- template <class KeyType>
- DListNode<KeyType>*DListNode<KeyType>:: GetNext()
- {
- return next;
- }
- template <class KeyType>
- void DListNode<KeyType>::SetNext(DListNode<KeyType> *x)
- {
- next=x;
- }
- template <class KeyType>
- DListNode<KeyType>*DListNode<KeyType>:: GetPrevious()
- {
- return previous;
- }
- template <class KeyType>
- void DListNode<KeyType>::SetPrevious(DListNode<KeyType> *x)
- {
- previous=x;
- }
- //*********************************** NODES ***********************************
- //*********************************** DECLARATIONS ***********************************
- //*********************************** Ends Here ***********************************
- template <class KeyType>
- DList<KeyType>::DList()
- {
- first=0;
- }
- template <class KeyType>
- DListNode<KeyType>*DList<KeyType>:: GetFirst()
- {
- return first;
- }
- // if the list is empty, it makes pNew the first node of the list
- template <class KeyType>
- void DList<KeyType>::Insert(DListNode<KeyType>* pBefore, DListNode<KeyType>* pNew)
- {
- if(first==0)
- {
- first = pNew;
- //cout<<"i came here"<<endl;
- return;
- }
- //cout<<"kaptaan"<<endl;
- DListNode<KeyType>*shift;
- shift=pBefore->GetNext(); //pointing to next node of pbefore
- if(shift==0) //it means we are inserting at last node
- {
- pBefore->SetNext(pNew);
- pNew->SetPrevious(pBefore);
- }
- else
- {
- pNew->SetNext(shift); //new node to pbefore next node
- pBefore->SetNext(pNew); //point pbefore to ne node
- shift->SetPrevious(pNew); //pointing the right node to pnew
- pNew->SetPrevious(pBefore); //poiting pnew to the previous node
- }
- }
- //Deletes the node pToBeDeleted (5 marks)
- template <class KeyType>
- void DList<KeyType>::Delete(DListNode<KeyType>* pToBeDeleted)
- {
- int check=0;
- if(pToBeDeleted == first )// if we are delete first node
- { //cout<< " in the if "<<endl;
- first = first->GetNext();
- check=1;
- }
- else if(pToBeDeleted->GetNext()==0)// if we're deleting last node
- {
- DListNode<KeyType>*temp;
- temp=pToBeDeleted->GetPrevious();
- temp->SetNext(0);
- check=1;
- }
- if(check==0) //if we're deleting in between node
- {
- DListNode<KeyType>*current_next;
- DListNode<KeyType>*current_prev;
- current_next=pToBeDeleted->GetNext();
- current_prev=pToBeDeleted->GetPrevious();
- current_prev->SetNext(current_next);
- current_next->SetPrevious(current_prev);
- }
- delete pToBeDeleted;
- }
- template <class KeyType>
- void DList<KeyType>::printList(DListNode<KeyType>*pFirst)
- {
- cout<<"**********************************"<<endl;
- DListNode<KeyType>*z ;
- z=pFirst;
- int i=0;
- while(z)
- {
- KeyType test2 = z->getData();
- cout<<"Value in Node # "<< i+1<<" is "<<test2<<endl;
- z=z->GetNext();
- i++;
- }
- }
- //*********************************** DLLIST ***********************************
- //*********************************** DECLARATIONS ***********************************
- //*********************************** Ends Here ***********************************
- template <class KeyType>
- Queue<KeyType>::Queue(int maxsize)
- {
- size=maxsize;
- count=0;
- l1=new DList<KeyType>();
- front=l1->GetFirst();
- //front=0;
- rear=l1->GetFirst();
- //rear=0;
- }
- // returns true if queue is full, otherwise return false (3 marks)
- template <class KeyType>
- bool Queue<KeyType>::IsFull()
- {
- if(count==size)
- return true;
- else
- return false;
- }
- //If number of elements in the queue is zero return true, otherwise return false
- template <class KeyType>
- bool Queue<KeyType>::IsEmpty()
- {
- if(count==0)
- return true;
- else
- return false;
- }
- template <class KeyType>
- void Queue<KeyType>::Put(const KeyType item)
- {
- if(IsFull()==0)
- {
- DListNode<KeyType>*newnode;
- newnode=new DListNode<KeyType>(item);
- l1->Insert(rear,newnode);
- rear=newnode;
- front=l1->GetFirst();
- count++;
- }
- else
- {
- cout<<"**********************************"<<endl;
- cout<<"Queue is full.First Deque then put"<<endl;
- cout<<"**********************************"<<endl;
- }
- }
- template <class KeyType>
- KeyType Queue<KeyType>::Get()
- {
- if(IsEmpty()==0)
- {
- KeyType temp;
- front=l1->GetFirst(); //pointing front to new first value
- temp=front->getData();
- l1->Delete(front);
- count--;
- return temp;
- }
- else
- {
- return 0;
- }
- }
Add Comment
Please, Sign In to add comment