Advertisement
AnindyaBiswas

Multi Insert Variants

Aug 24th, 2024
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. using namespace std;
  2. #include<iostream>
  3.  
  4. struct Node{
  5.     int data;
  6.     struct Node *next;
  7. };
  8.  
  9. Node* head = NULL;
  10. int Size = 0;
  11.  
  12. void Traversal(){
  13.     struct Node* temp = head;
  14.     cout<<"\tThe Linked List is : \t";
  15.     while(temp != NULL){
  16.         cout<<temp->data<<" |-> ";
  17.         temp = temp->next;
  18.     }
  19.     cout<<"\tSize - "<<Size<<endl;
  20.     cout<<"\n------------------------------------------------------------------------\n";
  21. }
  22. void InsertBeg(int x){
  23.     cout<<"Inserting "<<x<<" at the beginning"<<endl;
  24.     struct Node* node = new Node;
  25.     node->data = x;
  26.     node->next = head;
  27.     head = node;
  28.     Size++;
  29.     Traversal();
  30. }
  31.  
  32. void InsertEnd(int x){
  33.     cout<<"Inserting "<<x<<" at the end"<<endl;
  34.     Node* node = new Node;
  35.     node->data = x;
  36.     node->next = NULL;
  37.  
  38.     if (head == NULL){
  39.         head = node;
  40.     }
  41.     else{
  42.         Node *temp = head;
  43.         while(temp->next != NULL){
  44.             temp = temp->next;
  45.         }  
  46.     temp->next = node;
  47.     }
  48.  
  49.     Size++;
  50.     Traversal();
  51. }
  52.  
  53.  
  54. void InsertPos(int x, int pos){
  55.     if (pos > Size){
  56.         cout<<"Invalid Position"<<endl;
  57.         return;
  58.     }
  59.     if (pos == 0){
  60.         InsertBeg(x);
  61.         return;
  62.     }
  63.     cout<<"Inserting "<<x<<" at position "<<pos<<endl;
  64.     Node* node = new Node;
  65.     node->data = x;
  66.     node->next = NULL;
  67.     Node* temp = head;
  68.     while(pos--){
  69.         if (pos == 0){
  70.             node->next = temp->next;
  71.             temp->next = node;
  72.         }
  73.         else{
  74.             temp = temp->next;
  75.         }
  76.     }
  77.     Size++;
  78.    Traversal();
  79. }
  80.  
  81.  
  82. void MultiInsertM(int i){
  83.     Node* temp = head;
  84.     for(int j=1;j<=Size;j++){
  85.         if(j%i == 0){
  86.             InsertPos(i, j-1);
  87.             temp = temp->next;
  88.         }
  89.     }
  90. }
  91.  
  92. int P[] = {2, 3, 5, 7, 11, 13, 17, 19};
  93.  
  94. void MultiInsertP(){
  95.     Node* temp = head;
  96.     int k = 0;
  97.     for(int j=1;j<=Size;j++){
  98.         if(j == P[k]){
  99.             InsertPos(P[k], j-1);
  100.             temp = temp->next;
  101.             k++;
  102.         }
  103.     }
  104. }
  105.  
  106.  
  107.  
  108.  
  109. int main(){
  110.     InsertEnd(23);
  111.     InsertEnd(15);
  112.     InsertEnd(35);
  113.     InsertEnd(49);
  114.     MultiInsertM(2);
  115.     // MultiInsertP();
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement