Advertisement
ruhan008

Untitled

Oct 2nd, 2023 (edited)
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. //Q2.3) WAP to implement a linked list, insert data at any position, delete data at any position and then print the linked list.
  2.  
  3. #include <iostream>
  4. #include<stdlib.h>
  5. using namespace std;
  6.  
  7. struct node{
  8.     int data;
  9.     node* next;
  10.     void setData(int d){
  11.         data=d;
  12.         next=NULL;
  13.     }
  14. };
  15.  
  16. void InsertAtHead(node* &head,int data){
  17.     node* newNode=(node*)malloc(sizeof(node));
  18.     newNode->setData(data);
  19.     newNode->next=head;
  20.     head=newNode;
  21. }
  22.  
  23. void InsertAtTail(node* &tail,int data){
  24.     node* newNode=(node*)malloc(sizeof(node));
  25.     newNode->setData(data);
  26.     tail->next=newNode;
  27.     tail=newNode;
  28. }
  29.  
  30. void InsertAtPosition(node* &head,node* &tail,int position,int data){
  31.     if(position==1){
  32.         InsertAtHead(head,data);
  33.         return ;
  34.     }
  35.     node* temp=head;
  36.     int nodeNumber=1;
  37.     while(nodeNumber<position-1){
  38.         temp=temp->next;
  39.         nodeNumber++;
  40.     }
  41.  
  42.     if(temp->next==NULL){
  43.         InsertAtTail(tail,data);
  44.         return ;
  45.     }
  46.     node* newNode=(node*)malloc(sizeof(node));
  47.     newNode->setData(data);
  48.     newNode->next=temp->next;
  49.     temp->next=newNode;
  50.    
  51. }
  52. void DeleteAtPosition(node* &head,node* &tail,int position){
  53.     node* temp=head;
  54.     int nodeNumber=1;
  55.     if(position==1){
  56.         head=temp->next;
  57.         temp->next=NULL;
  58.         free(temp);
  59.         temp=NULL;
  60.         return ;
  61.     }
  62.     while(nodeNumber<position-1){
  63.         temp=temp->next;
  64.         nodeNumber++;
  65.     }
  66.     node* toDelete=temp->next;
  67.  
  68.     if(toDelete==tail){
  69.         tail=temp;
  70.         tail->next=NULL;
  71.         free(toDelete);
  72.         toDelete=NULL;
  73.         return ;
  74.     }
  75.  
  76.     temp->next=toDelete->next;
  77.     toDelete->next=NULL;
  78.     free(toDelete);
  79.     toDelete=NULL;
  80. }
  81. void PrintLinkedList(node* &head){
  82.     node* temp=head;
  83.     while(temp!=NULL){
  84.         cout<<temp->data<<" ";
  85.         temp=temp->next;
  86.     }
  87.     cout<<endl;
  88. }
  89.  
  90. int main(){
  91.  
  92.     node* head=(node*)malloc(sizeof(node));
  93.     head->setData(1);
  94.     node* tail=head;
  95.    
  96.     InsertAtHead(head,2);
  97.     InsertAtTail(tail,4);
  98.     InsertAtTail(tail,5);
  99.  
  100.     cout<<"Before Insertion: ";
  101.     PrintLinkedList(head);
  102.    
  103.     InsertAtPosition(head,tail,5,7);
  104.     InsertAtPosition(head,tail,3,6);
  105.     InsertAtPosition(head,tail,1,3);
  106.    
  107.     cout<<"After Insertion: ";
  108.     PrintLinkedList(head);
  109.     cout<<endl;
  110.     cout<<"Before Deletion: ";
  111.     PrintLinkedList(head);
  112.  
  113.     DeleteAtPosition(head,tail,7);
  114.     DeleteAtPosition(head,tail,4);
  115.     DeleteAtPosition(head,tail,1);
  116.  
  117.     cout<<"After Deletion: ";
  118.     PrintLinkedList(head);
  119.  
  120.  
  121.     return 0;
  122. }
  123. /*
  124.     OUTPUT:
  125.  
  126.     Before Insertion: 2 1 4 5
  127.     After Insertion: 3 2 1 6 4 5 7
  128.  
  129.     Before Deletion: 3 2 1 6 4 5 7
  130.     After Deletion: 2 1 4 5
  131.  
  132. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement