Naxocist

LinkedList [insert_at_index]

Apr 3rd, 2022
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define endll '\n'
  3. using namespace std;
  4.  
  5.  
  6. struct node {
  7.     int val;
  8.     node *next;
  9. };
  10.  
  11. node *head = NULL;
  12.  
  13.  
  14. node *createNode(int val){
  15.     // node *newNode = (node *) malloc(sizeof(node));
  16.     node *newNode = new node;
  17.     newNode->val = val;
  18.     newNode->next = NULL;
  19.  
  20.     return newNode;
  21. }
  22.  
  23. void printNode(){
  24.  
  25.     node *curr = head;
  26.  
  27.     while(curr != NULL) {
  28.         cout << curr->val;
  29.         if(curr->next != NULL) cout << " ";
  30.         curr = curr->next;
  31.     }
  32.     cout << endll;
  33.  
  34. }
  35.  
  36. void insertNode(int pos, int val){
  37.     if(pos < 0) {
  38.         printf("[%d: %d not inserted]\n", pos, val);
  39.         return ;
  40.     }
  41.  
  42.     node *newNode = createNode(val);
  43.  
  44.     node *curr = head, *prev = NULL;
  45.     int index = 0;
  46.     while(curr != NULL && index != pos) {
  47.         prev = curr;
  48.         curr = curr->next;
  49.         index++;
  50.     }
  51.  
  52.     if(curr == NULL && index != pos){
  53.         printf("[%d: %d not inserted]\n", pos, val);
  54.         return ;
  55.     }
  56.  
  57.     if(prev == NULL && pos == 0) {
  58.         head = newNode;
  59.         newNode->next = curr;
  60.         return ;
  61.     }
  62.  
  63.     if(curr == NULL && index == pos){
  64.         prev->next = newNode;
  65.         newNode->next = NULL;
  66.         return ;
  67.     }
  68.     prev->next = newNode;
  69.     newNode->next = curr;  // pos == index
  70.  
  71. }
  72.  
  73. void deleteNode(int pos){
  74.     if(pos < 0) {
  75.         printf("[%d: not deleted]\n", pos);
  76.         return ;
  77.     }
  78.  
  79.     if(head == NULL) {
  80.         printf("[%d: not deleted]\n", pos);
  81.         return ;
  82.     }
  83.  
  84.     node *curr = head, *prev = NULL;
  85.     int index = 0;
  86.     while(curr && index != pos){
  87.         prev = curr;
  88.         curr = curr->next;
  89.         index++;
  90.     }
  91.     if(!curr){
  92.         printf("[%d: not deleted]\n", pos);
  93.         return ;
  94.     }
  95.     if(!prev){
  96.         printf("[%d: %d deleted]\n", pos, head->val);
  97.         head = head->next;
  98.         return ;
  99.     }
  100.  
  101.     printf("[%d: %d deleted]\n", pos, curr->val);
  102.     prev->next = curr->next;
  103.     free(curr);
  104.  
  105. }
  106.  
  107. void indexingNode(int pos){
  108.     if(pos < 0) {
  109.         printf("[%d: invalid index]\n", pos);
  110.         return ;
  111.     }
  112.     if(head == NULL){
  113.         printf("[%d: invalid index]\n", pos);
  114.         return ;
  115.     }
  116.  
  117.     node *curr = head;
  118.     int index=0;
  119.     while(curr && index != pos) curr = curr->next, index++;
  120.  
  121.     if(!curr){
  122.         printf("[%d: invalid index]\n", pos);
  123.         return ;
  124.     }
  125.  
  126.     printf("%d: %d\n", pos, curr->val);
  127. }
  128.  
  129.  
  130. int main() {
  131.  
  132.      int n; cin >> n;
  133.  
  134.      while(n--){
  135.          char c; cin >> c;
  136.          int index, x;
  137.          if(c == 'i'){
  138.              cin >> index >> x;
  139.              insertNode(index, x);
  140.          }
  141.  
  142.          if(c == 'd'){
  143.              cin >> index;
  144.              deleteNode(index);
  145.          }
  146.  
  147.          if(c == 'v'){
  148.             cin >> index;
  149.             indexingNode(index);
  150.          }
  151.  
  152.          if(c == 'p') printNode();
  153.      }
  154.  
  155.     return 0;
  156. }
  157.  
Advertisement
Add Comment
Please, Sign In to add comment