Advertisement
DASBD72

12303

Oct 2nd, 2020
1,297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.60 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. typedef struct _Node node;
  5. struct _Node{
  6.     int num;
  7.     node *next;
  8. };
  9. node *head;
  10. void insert(int val1, int val2){
  11.     while(val2--){
  12.         node *inserting = (node*)malloc(sizeof(node));
  13.         inserting->num = val1;
  14.         inserting->next = head->next;
  15.         head->next = inserting;
  16.     }
  17.     return;
  18. }
  19. void erase(int val){
  20.     while(val--){
  21.         node *todelete = head->next;
  22.         head->next = head->next->next;
  23.         free(todelete);
  24.     }
  25.     return;
  26. }
  27. void move(int value){
  28.     while(value--) head = head->next;
  29.     return;
  30. }
  31. void show(){
  32.     node *current = head;
  33.     do{
  34.         printf("%d", current->num);
  35.         current = current->next;
  36.         if(current == head) printf("\n");
  37.         else printf(" ");
  38.     }while(current != head);
  39.     return;
  40. }
  41. int main(){
  42.     int x, n;
  43.     char buf[10];
  44.     scanf("%d %d", &x, &n);
  45.  
  46.     //initialize
  47.     head = (node*)malloc(sizeof(node));
  48.     head->next = head;
  49.     head->num = x;
  50.    
  51.     //start reading command
  52.     while(n--){
  53.         scanf("%s", buf);
  54.         if(!strcmp(buf, "insert")){
  55.             int val1, val2;
  56.             scanf("%d %d", &val1, &val2);
  57.             insert(val1, val2);
  58.         }
  59.         else if(!strcmp(buf, "erase")){
  60.             int val;
  61.             scanf("%d", &val);
  62.             erase(val);
  63.         }
  64.         else if(!strcmp(buf, "move")){
  65.             int value;
  66.             scanf("%d", &value);
  67.             move(value);
  68.         }
  69.         else if(!strcmp(buf, "show")){
  70.             show();
  71.         }
  72.     }
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement