m2skills

stack ll cpp

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