Advertisement
backstreetimrul

Linked List

Jun 1st, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. struct node
  4. {
  5.     int data;
  6.     node *next;
  7. };
  8.  
  9. class Linked_List
  10. {
  11. private:
  12.     node *head, *tail;
  13. public:
  14.     Linked_List()
  15.     {
  16.         head=NULL;
  17.         tail=NULL;
  18.     }
  19.     ///Function to add data in the last of the list
  20.     void add_last(int n)
  21.     {
  22.         node *tmp = new node;   //allocates a new node
  23.         tmp->data = n;
  24.         tmp->next = NULL;
  25.  
  26.         if(head == NULL)
  27.         {
  28.             head = tmp;
  29.             tail = tmp;
  30.         }
  31.         else
  32.         {
  33.             tail->next=tmp;
  34.             tail = tail->next;
  35.         }
  36.     }
  37.  
  38.     node* gethead()
  39.     {
  40.         return head;
  41.     }
  42.  
  43.     void display()
  44.     {
  45.         node *tmp;
  46.         tmp=head;
  47.         while(tmp!=NULL)
  48.         {
  49.             cout<< tmp->data <<" ";
  50.             tmp=tmp->next;
  51.         }
  52.         cout<<endl;
  53.     }
  54.  
  55.     void add_by_index(node *a, int n,int pos)
  56.     {
  57.         //beginning
  58.         if(pos==0)
  59.         {
  60.             node *tmp = new node;
  61.             tmp -> data = n;
  62.             tmp -> next = head;
  63.             head = tmp;
  64.         }
  65.  
  66.         //Middle or last by indexing
  67.         else
  68.         {
  69.             int i=0;
  70.             node *tmp;
  71.             tmp=a;
  72.             while(i!=pos)
  73.             {
  74.                 if(i==(pos-1))
  75.                 {
  76.                     node* newNode = new node;
  77.                     newNode->data = n;
  78.                     newNode->next = tmp->next;
  79.                     tmp->next = newNode;
  80.                 }
  81.                 tmp=tmp->next;
  82.                 i++;
  83.             }
  84.         }
  85.     }
  86.  
  87.     //Deleting Node
  88.     void del (node *before_del)
  89.     {
  90.         node* temp;
  91.         temp = before_del->next;
  92.         before_del->next = temp->next;
  93.         delete temp;
  94.     }
  95.  
  96.     void delete_position(int pos)
  97.     {
  98.         pos=pos+1;
  99.         int i;
  100.         //beginning
  101.         if(pos==1)
  102.         {
  103.             node *temp=new node;
  104.             temp=head;
  105.             head=head->next;
  106.             delete temp;
  107.         }
  108.         //middle or last
  109.         else
  110.         {
  111.             node *current=new node;
  112.             node *previous=new node;
  113.             current=head;
  114.             for(i=1;i<pos;i++)
  115.             {
  116.                 previous=current;
  117.                 current=current->next;
  118.             }
  119.             previous->next=current->next;
  120.         }
  121.     }
  122.  
  123. };
  124. int main()
  125. {
  126.     Linked_List a;
  127.  
  128.     a.add_last(5);
  129.     a.add_last(12);
  130.     a.add_last(32);
  131.     a.add_last(43);
  132.     a.add_last(87);
  133.     a.add_last(85);
  134.     a.add_last(39);
  135.     a.add_last(98);
  136.  
  137.     a.display();
  138.  
  139.     a.add_by_index(a.gethead(),777,1);
  140.     a.display();
  141.  
  142.     a.add_by_index(a.gethead(),999,3);
  143.     a.display();
  144.  
  145.     a.add_by_index(a.gethead(),222,0);
  146.     a.display();
  147.  
  148.     a.add_by_index(a.gethead(),1111,11);
  149.     a.display();
  150.  
  151. //    a.add_by_index(a.gethead(),13333,13);
  152. //    a.display();
  153.  
  154.  
  155.     a.delete_position(3);
  156.     a.display();
  157.  
  158.     a.delete_position(2);
  159.     a.display();
  160.  
  161.     a.delete_position(1);
  162.     a.display();
  163.  
  164.     a.delete_position(9);
  165.     a.display();
  166.  
  167.  
  168.  
  169.     return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement