m2skills

add one ll c++

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