m2skills

simple LL cpp

Apr 18th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 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()<<" --> ";
  101.             else
  102.                 cout<<tempNode->getElement();
  103.  
  104.             tempNode = tempNode->getNextNode();
  105.         }
  106.         return;
  107.     }
  108.  
  109.     //method that returns the length of the Linked List
  110.     int length(){
  111.         int count = 0;
  112.         if(this->isEmpty()){
  113.             return -1;
  114.         }
  115.         node* tempNode = head;
  116.         while(tempNode != NULL){
  117.             count += 1;
  118.             tempNode = tempNode->getNextNode();
  119.         }
  120.         return count;
  121.     }
  122.  
  123.     //method to delete a data element
  124.     void deleteData(int element){
  125.         if(isEmpty()){
  126.             cout<<"The List is Empty";
  127.             return;
  128.         }
  129.         else{
  130.             node* tempNode = head;
  131.             node* prevNode = head;
  132.             while(tempNode->getElement() != element){
  133.                 prevNode = tempNode;
  134.                 tempNode = tempNode->getNextNode();
  135.             }
  136.             prevNode->updateLink(tempNode->getNextNode());
  137.             return;
  138.         }
  139.     }
  140.  
  141. };
  142.  
  143. int main() {
  144.     Linkedlist l1;
  145.     l1.addToStart(25);
  146.     l1.addToStart(25);
  147.     l1.addToStart(25);
  148.     l1.addToStart(26);
  149.     l1.addToStart(27);
  150.     l1.addToStart(28);
  151.     l1.display();
  152.  
  153.     l1.addToEnd(24);
  154.     l1.addToEnd(23);
  155.     cout<<"\n";
  156.     l1.display();
  157.  
  158.     l1.deleteData(26);
  159.     l1.display();
  160.     return 0;
  161. }
Add Comment
Please, Sign In to add comment