terminator891

Queueimpl

Oct 15th, 2016
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.60 KB | None | 0 0
  1. #include<iostream>
  2. #include "Queue.h"
  3.  
  4. using namespace std;
  5.  
  6. template <class KeyType>
  7. DListNode<KeyType>::DListNode(KeyType element)
  8. {
  9.     next=0;
  10.     previous=0;
  11.     data=element;
  12. }
  13.  
  14. template <class KeyType>
  15. void DListNode<KeyType>::setData(KeyType pVal)
  16. {
  17.     data=pVal;
  18. }
  19.  
  20. template <class KeyType>
  21. KeyType DListNode<KeyType>:: getData()
  22. {
  23.     return data;
  24. }
  25.  
  26. template <class KeyType>
  27. DListNode<KeyType>*DListNode<KeyType>:: GetNext()
  28. {
  29.     return next;
  30. }
  31.  
  32. template <class KeyType>
  33. void DListNode<KeyType>::SetNext(DListNode<KeyType> *x)
  34. {
  35.     next=x;
  36. }
  37.  
  38. template <class KeyType>
  39. DListNode<KeyType>*DListNode<KeyType>:: GetPrevious()
  40. {
  41.     return previous;
  42. }
  43.  
  44. template <class KeyType>
  45. void DListNode<KeyType>::SetPrevious(DListNode<KeyType> *x)
  46. {
  47.     previous=x;
  48. }
  49.  
  50. //***********************************    NODES     ***********************************
  51. //*********************************** DECLARATIONS ***********************************
  52. //***********************************  Ends Here   ***********************************
  53.  
  54. template <class KeyType>
  55. DList<KeyType>::DList()
  56. {
  57.     first=0;
  58. }
  59.  
  60. template <class KeyType>
  61. DListNode<KeyType>*DList<KeyType>:: GetFirst()
  62. {
  63.     return first;
  64. }
  65.  
  66. // if the list is empty, it makes pNew the first node of the list
  67. template <class KeyType>
  68. void DList<KeyType>::Insert(DListNode<KeyType>* pBefore, DListNode<KeyType>* pNew)
  69. {
  70.     if(first==0)
  71.     {
  72.         first = pNew;
  73.         //cout<<"i came here"<<endl;
  74.         return;
  75.        
  76.     }
  77.     //cout<<"kaptaan"<<endl;
  78.     DListNode<KeyType>*shift;
  79.     shift=pBefore->GetNext(); //pointing to next node of pbefore
  80.     if(shift==0)    //it means we are inserting at last node
  81.     {
  82.         pBefore->SetNext(pNew);
  83.         pNew->SetPrevious(pBefore);
  84.     }
  85.     else
  86.     {
  87.     pNew->SetNext(shift);       //new node to pbefore next node
  88.     pBefore->SetNext(pNew);     //point pbefore to ne node
  89.     shift->SetPrevious(pNew);   //pointing the right node to pnew
  90.     pNew->SetPrevious(pBefore); //poiting pnew to the previous node
  91.     }
  92.  
  93.    
  94. }
  95.  
  96. //Deletes the node pToBeDeleted (5 marks)
  97. template <class KeyType>
  98. void DList<KeyType>::Delete(DListNode<KeyType>* pToBeDeleted)
  99. {
  100.     int check=0;
  101.     if(pToBeDeleted == first )// if we are delete first node
  102.       { //cout<< " in the if "<<endl;
  103.         first = first->GetNext();
  104.         check=1;
  105.       }
  106.     else if(pToBeDeleted->GetNext()==0)// if we're deleting last node
  107.     {
  108.         DListNode<KeyType>*temp;
  109.         temp=pToBeDeleted->GetPrevious();
  110.         temp->SetNext(0);
  111.         check=1;
  112.     }
  113.     if(check==0)  //if we're deleting in between node
  114.     {
  115.     DListNode<KeyType>*current_next;
  116.     DListNode<KeyType>*current_prev;
  117.     current_next=pToBeDeleted->GetNext();
  118.     current_prev=pToBeDeleted->GetPrevious();
  119.     current_prev->SetNext(current_next);
  120.     current_next->SetPrevious(current_prev);
  121.     }
  122.     delete pToBeDeleted;
  123.  
  124. }
  125.  
  126. template <class KeyType>
  127. void DList<KeyType>::printList(DListNode<KeyType>*pFirst)
  128. {
  129.     cout<<"**********************************"<<endl;
  130.       DListNode<KeyType>*z ;
  131.      
  132.       z=pFirst;
  133.       int i=0;
  134.  
  135.     while(z)
  136.     {
  137.         KeyType test2 = z->getData();
  138.         cout<<"Value in Node # "<< i+1<<" is "<<test2<<endl;
  139.         z=z->GetNext();
  140.         i++;
  141.     }
  142.  
  143. }
  144.  
  145. //***********************************    DLLIST    ***********************************
  146. //*********************************** DECLARATIONS ***********************************
  147. //***********************************  Ends Here   ***********************************
  148.  
  149.  
  150. template <class KeyType>
  151. Queue<KeyType>::Queue(int maxsize)
  152. {
  153.     size=maxsize;
  154.     count=0;
  155.     l1=new DList<KeyType>();
  156.     front=l1->GetFirst();
  157.     //front=0;
  158.     rear=l1->GetFirst();
  159.     //rear=0;
  160. }
  161.  
  162. // returns true if queue is full, otherwise return false (3 marks)
  163. template <class KeyType>
  164. bool Queue<KeyType>::IsFull()
  165. {
  166.     if(count==size)
  167.         return true;
  168.     else
  169.         return false;
  170. }
  171.  
  172. //If number of elements in the queue is zero return true, otherwise return false
  173. template <class KeyType>
  174. bool Queue<KeyType>::IsEmpty()
  175. {
  176.     if(count==0)
  177.         return true;
  178.     else
  179.         return false;
  180.  
  181. }
  182.  
  183.  
  184.  
  185. template <class KeyType>
  186. void Queue<KeyType>::Put(const KeyType item)
  187. {
  188.     if(IsFull()==0)
  189.     {
  190.         DListNode<KeyType>*newnode;
  191.         newnode=new DListNode<KeyType>(item);
  192.         l1->Insert(rear,newnode);
  193.         rear=newnode;
  194.         front=l1->GetFirst();
  195.         count++;
  196.     }
  197.     else
  198.     {
  199.         cout<<"**********************************"<<endl;
  200.         cout<<"Queue is full.First Deque then put"<<endl;
  201.         cout<<"**********************************"<<endl;
  202.     }
  203. }
  204.  
  205. template <class KeyType>
  206. KeyType Queue<KeyType>::Get()
  207. {
  208.     if(IsEmpty()==0)
  209.     {
  210.     KeyType temp;
  211.     front=l1->GetFirst(); //pointing front to new first value
  212.     temp=front->getData();
  213.     l1->Delete(front);
  214.     count--;
  215.     return temp;
  216.     }
  217.     else
  218.     {
  219.         return 0;
  220.     }
  221. }
Add Comment
Please, Sign In to add comment