m2skills

kth element from end ll cpp

Jan 30th, 2018
953
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  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. public:
  48.     node *head;
  49.  
  50.     //constructor for the Linked List class
  51.     Linkedlist() {
  52.         head = NULL;
  53.     }
  54.  
  55.     //returns head node
  56.     node *getHead() {
  57.         return this->head;
  58.     }
  59.  
  60.     // method to add a node at the end
  61.     void insert(int element) {
  62.         node *tempNode = new node(element);
  63.         node *p = head;
  64.         if (head == NULL) {
  65.             head = tempNode;
  66.             return;
  67.         }
  68.         else {
  69.             while (p->getNextNode() != NULL) {
  70.                 p = p->getNextNode();
  71.             }
  72.             p->updateLink(tempNode);
  73.             return;
  74.         }
  75.     }
  76.  
  77.     //method to display all the elements of the Linked List
  78.     void display() {
  79.         cout << "\n";
  80.         node *tempNode = head;
  81.         while (tempNode != NULL) {
  82.             if (tempNode->getNextNode() != NULL)
  83.                 cout << tempNode->getElement() << " --> ";
  84.             else
  85.                 cout << tempNode->getElement();
  86.  
  87.             tempNode = tempNode->getNextNode();
  88.         }
  89.         return;
  90.     }
  91.  
  92.     // kth element from the end
  93.     int k_Element_End(int k){
  94.         node* first = this->getHead();
  95.         node* second = this->getHead();
  96.  
  97.         for(int i=0; i < k; i++){
  98.             second = second = second->getNextNode();
  99.         }
  100.  
  101.         while(second != NULL){
  102.             first = first->getNextNode();
  103.             second = second->getNextNode();
  104.         }
  105.  
  106.         return first->getElement();
  107.     }
  108. };
  109.  
  110. int main() {
  111.     cout<<"Program to check print kth element from the in a Linked List";
  112.     Linkedlist l1;
  113.     l1.insert(1);
  114.     l1.insert(3);
  115.     l1.insert(5);
  116.     l1.insert(7);
  117.     l1.insert(7);
  118.     l1.insert(8);
  119.     l1.insert(5);
  120.     l1.insert(3);
  121.     l1.insert(1);
  122.  
  123.     cout<<"\nLinked list is : ";
  124.     l1.display();
  125.     cout<<"\nThe 5th element from the end is : "<<l1.k_Element_End(5);
  126.  
  127.     return 0;
  128. }
Add Comment
Please, Sign In to add comment