m2skills

Linked List cpp

May 31st, 2017
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.04 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 insert element at a given position in the Linked List
  124.     void insert(int element, int position){
  125.         node* tempNode = head;
  126.         node* prevNode = head;
  127.         if(position == 1){
  128.             this->addToStart(element);
  129.         }
  130.         else{
  131.             int pos = 1;
  132.             while(pos != position){
  133.                 prevNode = tempNode;
  134.                 tempNode = tempNode->getNextNode();
  135.                 pos += 1;
  136.             }
  137.             node* p1 = new node(element,prevNode->getNextNode());
  138.             prevNode->updateLink(p1);
  139.         }
  140.         return;
  141.     }
  142.  
  143.     //method to delete a element at a given position
  144.     void deletePosition(int position){
  145.         node* tempNode = head;
  146.         node* prevNode = head;
  147.         if(position == 1){
  148.             head = tempNode->getNextNode();
  149.             return;
  150.         }
  151.         else{
  152.             int pos = 1;
  153.             while(pos != position){
  154.                 prevNode = tempNode;
  155.                 tempNode = tempNode->getNextNode();
  156.                 pos += 1;
  157.             }
  158.             prevNode->updateLink(tempNode->getNextNode());
  159.         }
  160.     }
  161.  
  162.     //method to delete a data element
  163.     void deleteData(int element){
  164.         if(isEmpty()){
  165.             cout<<"The List is Empty";
  166.             return;
  167.         }
  168.         else{
  169.             node* tempNode = head;
  170.             node* prevNode = head;
  171.             while(tempNode->getElement() != element){
  172.                 prevNode = tempNode;
  173.                 tempNode = tempNode->getNextNode();
  174.             }
  175.             prevNode->updateLink(tempNode->getNextNode());
  176.             return;
  177.         }
  178.     }
  179.  
  180.     //method that returns reversed linked list
  181.     Linkedlist reverse(){
  182.         Linkedlist temp;
  183.         node* tempNode = head;
  184.         while(tempNode != NULL){
  185.             temp.addToStart(tempNode->getElement());
  186.             tempNode = tempNode->getNextNode();
  187.         }
  188.         return temp;
  189.  
  190.     }
  191.  
  192.     //method that recursively reverses the Linked List
  193.     void recursiveReverse(node* tempNode , node* prevNode){
  194.         if(tempNode->getNextNode() != NULL) {
  195.             prevNode = tempNode;
  196.             tempNode = tempNode->getNextNode();
  197.             recursiveReverse(tempNode, prevNode);
  198.         }
  199.         else{
  200.             head = tempNode;
  201.         }
  202.         tempNode->updateLink(prevNode);
  203.         prevNode->updateLink(NULL);
  204.         return;
  205.     }
  206.  
  207.     //method that returns index of a particular element
  208.     int indexOf(int element){
  209.         if(this->isEmpty()){
  210.             return -1;
  211.         }
  212.  
  213.         node* tempNode = head;
  214.         int position = 1;
  215.         bool found = false;
  216.         while(tempNode != NULL){
  217.             if(tempNode->getElement() == element){
  218.                 found = true;
  219.                 return position;
  220.             }
  221.             else {
  222.                 tempNode = tempNode->getNextNode();
  223.                 position += 1;
  224.             }
  225.         }
  226.         if(not found){
  227.             return -1;
  228.         }
  229.     }
  230.  
  231.     //method that returns element at a particular position
  232.     int atIndex(int position){
  233.         if(isEmpty()){
  234.             return -1;
  235.         }
  236.         else if(position > this->length()){
  237.             cout<<"Postion is greater than the size of Linked List.";
  238.             return -99999;
  239.         }
  240.         else{
  241.             int tempPosition = 1;
  242.             node* tempNode = head;
  243.             while(tempPosition != position){
  244.                 tempNode = tempNode->getNextNode();
  245.                 tempPosition += 1;
  246.             }
  247.             return tempNode->getElement();
  248.         }
  249.     }
  250.  
  251.     //method that returns the max element
  252.     int findMax(){
  253.         int largest = -999999; //considering this element never occurs in the list
  254.         node* tempNode = head;
  255.         while(tempNode != NULL){
  256.             if(tempNode->getElement() > largest){
  257.                 largest = tempNode->getElement();
  258.             }
  259.             tempNode = tempNode->getNextNode();
  260.         }
  261.         return largest;
  262.     }
  263.  
  264.     //method that returns the min element
  265.     int findMin(){
  266.         int smallest = 999999; //considering this element never occurs in the list
  267.         node* tempNode = head;
  268.         while(tempNode != NULL){
  269.             if(tempNode->getElement() < smallest){
  270.                 smallest = tempNode->getElement();
  271.             }
  272.             tempNode = tempNode->getNextNode();
  273.         }
  274.         return smallest;
  275.     }
  276.  
  277.     //method that returns the occurences of an element
  278.     int countOccurences(int element){
  279.         if(isEmpty()){
  280.             return 0;
  281.         }
  282.         else{
  283.             int count = 0;
  284.             node* tempNode = head;
  285.             while(tempNode != NULL){
  286.                 if(tempNode->getElement() == element){
  287.                     count += 1;
  288.                 }
  289.                 tempNode = tempNode->getNextNode();
  290.             }
  291.             return count;
  292.         }
  293.     }
  294.  
  295.     //pop method removes last element of the Linked List
  296.     int pop(){
  297.         int position = this->length();
  298.         this->deletePosition(position);
  299.     }
  300.  
  301.     //method that clears the Linked List
  302.     void clear(){
  303.         this->head = NULL;
  304.         cout<<"The Linked List has been cleared.";
  305.     }
  306.  
  307.     //method that returns a string of all elements of the String
  308.     string toString(char seperator){
  309.         if(isEmpty()){
  310.             cout<<"Empty List";
  311.             return "";
  312.         }
  313.         else{
  314.             string finalString = "";
  315.             node* tempNode = head;
  316.             while(tempNode != NULL){
  317.  
  318.                 finalString  = finalString + to_string(tempNode->getElement());
  319.                 if(tempNode->getNextNode()!=NULL){
  320.                     finalString += seperator;
  321.                 }
  322.                 tempNode = tempNode->getNextNode();
  323.             }
  324.             return finalString;
  325.         }
  326.  
  327.     }
  328.  
  329.     //method that returns the copy of the list
  330.     Linkedlist copy(){
  331.         Linkedlist temp;
  332.         node* tempNode = head;
  333.         while(tempNode != NULL){
  334.             temp.addToEnd(tempNode->getElement());
  335.             tempNode = tempNode->getNextNode();
  336.         }
  337.         return temp;
  338.     }
  339.  
  340. };
  341.  
  342. int main() {
  343.     Linkedlist l1;
  344.     l1.addToStart(25);
  345.     l1.addToStart(25);
  346.     l1.addToStart(25);
  347.     l1.addToStart(26);
  348.     l1.addToStart(27);
  349.     l1.addToStart(28);
  350.     l1.display();
  351.  
  352.     l1.addToEnd(24);
  353.     l1.addToEnd(23);
  354.     cout<<"\n";
  355.     l1.display();
  356.  
  357.     cout<<"\n\nLength of the Linked List is : "<<l1.length();
  358.     cout<<"\nLargest element of the Linked List is : "<<l1.findMax();
  359.     cout<<"\nSmallest element of the Linked List is : "<<l1.findMin();
  360.  
  361.     cout<<"\n\nThe Element at index 3 is : "<<l1.atIndex(3);
  362.  
  363.     cout<<"\nThe index of element 27 is : "<<l1.indexOf(27);
  364.     cout<<"\nThe index of element 31 is : "<<l1.indexOf(31);
  365.  
  366.     cout<<"\nNumber of occurences of 25 in the Linked List are : "<<l1.countOccurences(25);
  367.     cout<<"\nNumber of occurences of 37 in the Linked List are : "<<l1.countOccurences(37);
  368.  
  369.     Linkedlist l2 = l1.copy();
  370.     cout<<"\nThe copied List is : ";
  371.     l2.display();
  372.  
  373.     l2.insert(99,4);
  374.     cout<<"\n";
  375.     l2.display();
  376.  
  377.     l2.deletePosition(4);
  378.     l2.display();
  379.  
  380.     l2.deleteData(23);
  381.     l2.display();
  382.  
  383.     cout<<"\n"<<l2.toString('-');
  384.     Linkedlist l3 = l2.reverse();
  385.     l3.display();
  386.  
  387.     l3.recursiveReverse(l3.getHead(),NULL);
  388.     l3.display();
  389.     return 0;
  390. }
Add Comment
Please, Sign In to add comment