m2skills

QUEUELL CPP

May 31st, 2017
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.90 KB | None | 0 0
  1. // PROGRAM TO IMPLEMENT QUEUE USING LINKED LIST
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class node{
  7.  
  8. protected:
  9.     int element;
  10.     node* link;
  11.  
  12. public:
  13.     //constructor that accepts only element
  14.     node(int element) {
  15.         this->element = element;
  16.         this->link = NULL;
  17.     }
  18.  
  19.     //constructor that accepts both link and element
  20.     node(int element, node* link){
  21.         this->element = element;
  22.         this->link = link;
  23.     }
  24.  
  25.     //method to update the element
  26.     void updateData(int element){
  27.         this->element = element;
  28.     }
  29.  
  30.     //method to update or setup link
  31.     void updateLink(node* link){
  32.         this->link = link;
  33.     }
  34.  
  35.     //method to get the element from the node
  36.     int getElement(){
  37.         return this->element;
  38.     }
  39.  
  40.     //method to get the next node
  41.     node* getNextNode(){
  42.         return this->link;
  43.     }
  44. };
  45.  
  46. class Linkedlist{
  47. protected:
  48.     node* head;
  49. public:
  50.  
  51.     //constructor for the Linked List class
  52.     Linkedlist(){
  53.         head = NULL;
  54.     }
  55.  
  56.     //method returns true if the list is empty
  57.     bool isEmpty(){
  58.         return head == NULL;
  59.     }
  60.  
  61.     //returns head node
  62.     node* getHead(){
  63.         return this->head;
  64.     }
  65.  
  66.     //method to add a node at starting
  67.     void addToStart(int element){
  68.         node* tempNode = new node(element);
  69.         if(head == NULL){
  70.             head = tempNode;
  71.         }
  72.         else{
  73.             tempNode->updateLink(head);
  74.             head = tempNode;
  75.         }
  76.     }
  77.  
  78.     // method to add a node at the end
  79.     void addToEnd(int element){
  80.         node* tempNode = new node(element);
  81.         node* p = head;
  82.         if(head == NULL){
  83.             head = tempNode;
  84.             return;
  85.         }
  86.         else{
  87.             while(p->getNextNode()!= NULL){
  88.                 p = p->getNextNode();
  89.             }
  90.             p->updateLink(tempNode);
  91.             return;
  92.         }
  93.     }
  94.  
  95.     //method to display all the elements of the Linked List
  96.     void display(){
  97.         cout<<"\n";
  98.         node* tempNode = head;
  99.         while(tempNode != NULL){
  100.             if(tempNode->getNextNode() != NULL)
  101.                 cout<<tempNode->getElement()<<" --> ";
  102.             else
  103.                 cout<<tempNode->getElement();
  104.  
  105.             tempNode = tempNode->getNextNode();
  106.         }
  107.         return;
  108.     }
  109.  
  110.     //method to delete a element at a given position
  111.     void deletePosition(int position){
  112.         node* tempNode = head;
  113.         node* prevNode = head;
  114.         if(position == 1){
  115.             head = tempNode->getNextNode();
  116.             return;
  117.         }
  118.         else{
  119.             int pos = 1;
  120.             while(pos != position){
  121.                 prevNode = tempNode;
  122.                 tempNode = tempNode->getNextNode();
  123.                 pos += 1;
  124.             }
  125.             prevNode->updateLink(tempNode->getNextNode());
  126.         }
  127.     }
  128.  
  129.     bool enqueue(int element){
  130.         if(this->isEmpty()){
  131.             this->addToStart(element);
  132.         }else{
  133.             this->addToEnd(element);
  134.         }
  135.     }
  136.  
  137.     int dequeue(){
  138.         if(this->isEmpty()){
  139.             cout<<"Queue Empty.";
  140.             return -99999;
  141.         }else{
  142.             int temp = this->head->getElement();
  143.             this->deletePosition(1);
  144.             return temp;
  145.         }
  146.     }
  147.  
  148. };
  149.  
  150. int main() {
  151.     Linkedlist l1;
  152.     l1.enqueue(1);
  153.     l1.enqueue(2);
  154.     l1.enqueue(3);
  155.     l1.enqueue(4);
  156.     l1.enqueue(5);
  157.     l1.enqueue(6);
  158.  
  159.     cout<<"The Contents of the Queue are : ";
  160.     l1.display();
  161.  
  162.     cout<<"\n\nDequeue 3 elements from the Queue";
  163.     l1.dequeue();
  164.     l1.dequeue();
  165.     l1.dequeue();
  166.  
  167.     cout<<"\n\nThe Contents of the Queue are : ";
  168.     l1.display();
  169.  
  170.     return 0;
  171. }
  172.  
  173. /*
  174.  
  175. The Contents of the Queue are :
  176. 1 --> 2 --> 3 --> 4 --> 5 --> 6
  177.  
  178. Dequeue 3 elements from the Queue
  179.  
  180. The Contents of the Queue are :
  181. 4 --> 5 --> 6
  182.  
  183.  
  184.  */
Add Comment
Please, Sign In to add comment