[c] 12680
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- #include <stdio.h>
- #include <stdlib.h>
- int a;
- char w;
- typedef struct _Node{
- int size;
- int* data;
- struct _Node* next;
- } Node;
- Node* ReadOneList()
- {
- Node *newone;
- int* num;
- scanf("%d %c", &a, &w);
- newone = (Node*)malloc(sizeof(Node));
- newone->size = a;
- num = (int*) malloc((a+1)*sizeof(int));
- for (int i=1; i<=a; i++)scanf("%d", &num[i]);
- newone->data = num;
- newone->next = NULL;
- return newone;
- }
- void PrintList(Node* origin)
- {
- Node* nd = origin;
- while(nd != NULL){
- if (nd->data != NULL){
- int i=1;
- for (; i<nd->size; i++)printf("%d ", nd->data[i]);
- printf("%d\n", nd->data[i]);
- }
- nd = nd->next;
- }
- }
- void Merge(Node* origin, int x, int y)
- {
- Node *nd_x = origin, *pre_x, *nd_y = origin;
- int i=x-1, j=y;
- while(i--){
- if(nd_x->next != NULL)nd_x = nd_x->next;
- }
- pre_x = nd_x;
- nd_x = nd_x->next;
- while(j--){
- if(nd_y->next != NULL)nd_y = nd_y->next;
- }
- int newsize = nd_x->size + nd_y->size;
- int *num = (int*)malloc((newsize+1)*sizeof(int));
- int k = 1, p = 1;
- for (; k<=nd_y->size; k++) num[k] = nd_y->data[k];
- for (; p<=nd_x->size; p++, k++) num[k] = nd_x->data[p];
- free(nd_y->data);
- free(nd_x->data);
- nd_y->data = num;
- nd_y->size = newsize;
- //前面要先找到x的前一個。
- pre_x->next = nd_x->next;
- free(nd_x);
- }
- void Cut(Node* origin, int x, int y)
- {
- Node *nd = origin;
- int time = x;
- while(time--){
- if (nd->next != NULL) nd = nd->next;
- }
- if (y>=nd->size) return;
- Node *newone = (Node*)malloc(sizeof(Node));
- newone->size = nd->size - y;
- int *num = (int*)malloc((newone->size+1)*sizeof(int));
- for (int i=1, j=y+1; i<=newone->size; i++, j++) num[i] = nd->data[j];
- nd->size = y;
- newone->data = num;
- newone->next = nd->next;
- nd->next = newone;
- }
- /*
- ReadOneList會接收input,該節點的大小和他的內容
- 看到struct裡的int* data可以知道他是一個指向陣列的指標
- 動態宣告一個陣列,然後記住它的位置
- 完成此節點,把該節點位置回傳。
- 在main 裡面
- for(int i=0; i<N; i++){
- tail->next = ReadOneList();
- tail = tail->next;
- }
- 這是一個儲存stack的linked list。
- PrintList從頭開始依序走訪,要印出陣列裡的值。
- Merge (x, y)
- 找到x的頭,把size跟data的內容都記下來,然後free掉他
- 擴充y的動態記憶體,然後把x的data接上去
- 更新y的size。
- 更新被移除節點的上一個節點的next
- Cut (x, y)
- 先走到第x個stack(linked list中)
- 再去找data裡面array第y個位子(從1開始),我們先叫y+1為newhead
- 從y+1以後的東西存到另一個節點,順便更新size
- 然後把新的節點插在x和x+1的stack中間
- */
Editor
You can edit this paste and save as new: