[c] 12680

Viewer

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int a;
  4. char w;
  5. typedef struct _Node{
  6.     int size;
  7.     int* data;
  8.     struct _Node* next;
  9. } Node;
  10.  
  11. Node* ReadOneList()
  12. {
  13.     Node *newone;
  14.     int* num;
  15.     scanf("%d %c", &a, &w);
  16.     newone = (Node*)malloc(sizeof(Node));
  17.     newone->size = a;
  18.     num = (int*) malloc(a*sizeof(int));
  19.     for (int i=1; i<=a; i++)scanf("%d", &num[i]);
  20.     newone->data = num;
  21.     newone->next = NULL;
  22.  
  23.     return newone;
  24. }
  25. void PrintList(Node* origin)
  26. {
  27.     Node* nd = origin;
  28.     while(nd != NULL){
  29.         if (nd->data != NULL){
  30.             int i=1;
  31.             for (; i<nd->size; i++)printf("%d ", nd->data[i]);
  32.             printf("%d\n", nd->data[i]);
  33.         }
  34.         nd = nd->next;
  35.     }
  36. }
  37. void Merge(Node* origin, int x, int y)
  38. {
  39.     Node *nd_x = origin, *pre_x, *nd_y = origin;
  40.     int i=x-1, j=y;
  41.     while(i--){
  42.         if(nd_x->next != NULL)nd_x = nd_x->next;
  43.     }
  44.     pre_x = nd_x;
  45.     nd_x = nd_x->next;
  46.     while(j--){
  47.         if(nd_y->next != NULL)nd_y = nd_y->next;
  48.     }
  49.     int newsize = nd_x->size + nd_y->size;
  50.     int *num = (int*)malloc(newsize*sizeof(int));
  51.     int k = 1, p = 1;
  52.     for (; k<=nd_y->size; k++) num[k] = nd_y->data[k];
  53.     for (; p<=nd_x->size; p++, k++) num[k] = nd_x->data[p];
  54.     free(nd_y->data);
  55.     free(nd_x->data);
  56.     nd_y->data = num;
  57.     nd_y->size = newsize;
  58.     //前面要先找到x的前一個。
  59.     pre_x->next = nd_x->next;
  60.     free(nd_x);
  61. }
  62. void Cut(Node* origin, int x, int y)
  63. {
  64.     Node *nd = origin;
  65.     int time = x;
  66.     while(time--){
  67.         if (nd->next != NULL) nd = nd->next;
  68.     }
  69.     Node *newone = (Node*)malloc(sizeof(Node));
  70.     newone->size = nd->size - y;
  71.     int *num = (int*)malloc(newone->size*sizeof(int));
  72.     for (int i=1, j=y+1; i<=newone->size; i++, j++) num[i] = nd->data[j];
  73.     nd->size = y;
  74.     newone->data = num;
  75.     newone->next = nd->next;
  76.     nd->next = newone;
  77. }
  78.  
  79. /*
  80. ReadOneList會接收input,該節點的大小和他的內容
  81. 看到struct裡的int* data可以知道他是一個指向陣列的指標
  82. 動態宣告一個陣列,然後記住它的位置
  83. 完成此節點,把該節點位置回傳。
  84.  
  85. 在main 裡面
  86. for(int i=0; i<N; i++){
  87.         tail->next = ReadOneList();
  88.         tail = tail->next;
  89.     }
  90. 這是一個儲存stack的linked list。
  91.  
  92. PrintList從頭開始依序走訪,要印出陣列裡的值。
  93.  
  94. Merge (x, y)
  95. 找到x的頭,把size跟data的內容都記下來,然後free掉他
  96. 擴充y的動態記憶體,然後把x的data接上去
  97. 更新y的size。
  98. 更新被移除節點的上一個節點的next
  99.  
  100. Cut (x, y)
  101. 先走到第x個stack(linked list中)
  102. 再去找data裡面array第y個位子(從1開始),我們先叫y+1為newhead
  103. 從y+1以後的東西存到另一個節點,順便更新size
  104. 然後把新的節點插在x和x+1的stack中間
  105. */
  106.  

Editor

You can edit this paste and save as new:


File Description
  • 12680
  • Paste Code
  • 28 Feb-2021
  • 2.82 Kb
You can Share it: