document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  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.     // deleting node with only that node pointer
  93.     void deleteNode(node* nodePtr){
  94.         if(nodePtr == NULL){
  95.             return;
  96.         }else{
  97.             // make a pointer to next node
  98.             node* nextNode = nodePtr->getNextNode();
  99.  
  100.             // copy deteils of next node to current node
  101.             nodePtr->updateData(nextNode->getElement());
  102.             nodePtr->updateLink(nextNode->getNextNode());
  103.  
  104.             // delete next node
  105.             free(nextNode);
  106.         }
  107.     }
  108.  
  109.     // deleting kth element from the end
  110.     int delete_kth_Element_End(int k){
  111.         node* first = this->getHead();
  112.         node* second = this->getHead();
  113.  
  114.         for(int i=0; i < k; i++){
  115.             second = second = second->getNextNode();
  116.         }
  117.  
  118.         while(second != NULL){
  119.             first = first->getNextNode();
  120.             second = second->getNextNode();
  121.         }
  122.  
  123.         deleteNode(first);
  124.     }
  125.  
  126. };
  127.  
  128. int main() {
  129.     cout<<"Program to delete kth element from the in a Linked List";
  130.     Linkedlist l1;
  131.     l1.insert(1);
  132.     l1.insert(3);
  133.     l1.insert(5);
  134.     l1.insert(7);
  135.     l1.insert(7);
  136.     l1.insert(8);
  137.     l1.insert(5);
  138.     l1.insert(3);
  139.     l1.insert(1);
  140.  
  141.     cout<<"\\nLinked list is : ";
  142.     l1.display();
  143.     cout<<"\\nLinked List after deleting 5th element from the end is : ";
  144.     l1.delete_kth_Element_End(5);
  145.     l1.display();
  146.  
  147.     return 0;
  148. }
');