Untitled
unknown
c_cpp
3 years ago
3.1 kB
12
Indexable
#include<stdio.h>
#include<stdlib.h>
typedef struct _Node{
int size;
int* data;
struct _Node* next;
} Node;
Node* ReadOneList(){
int index, num;
char tmp;
Node* node = (Node*)malloc(sizeof(Node));
scanf("%d%c", &node->size, &tmp);
node->data = (int*)malloc(sizeof(int)*(node->size));
for(int i = 0; i < node->size; i++){
scanf("%d", &node->data[i]);
}
return node;
}
void PrintList(Node* head){
head = head->next;
while(head != NULL){
for(int i = 0; i < head->size; i++){
if(i == head->size-1) printf("%d\n", head->data[i]);
else printf("%d ", head->data[i]);
}
head = head->next;
}
return;
}
void Merge(Node* node, int x, int y){
Node* target = node;//要被合併的node
Node* tmp = node;
Node* pre = node;//被合併的node的上一個node
for(int i = 0; i < x; i++) {
if(i == x-1) pre = target;
target = target->next;
}
for(int i = 0; i < y; i++){
tmp = tmp->next;
}
pre->next = target->next;
int* data_tmp = (int*)malloc(sizeof(int)*(tmp->size));
for(int i = 0; i < tmp->size; i++){
data_tmp[i] = tmp->data[i];
}
free(tmp->data);
tmp->data = (int*)malloc(sizeof(int)*(target->size + tmp->size));
for(int i = 0; i < tmp->size; i++){
tmp->data[i] = data_tmp[i];
}
free(data_tmp);
for(int i = 0; i < target->size; i++){
tmp->data[(tmp->size)+i] = target->data[i];
}
tmp->size += target->size;
free(target->data);
free(target);
return;
}
void Cut(Node* node, int x, int a){
Node* cutted = node; //要被切的node
Node* new = (Node*)malloc(sizeof(Node)*1); //被切出來的node
for(int i = 0; i < x; i++){
cutted = cutted->next;
}
int* data_tmp = (int*)malloc(sizeof(int)*(cutted->size));
for(int i = 0; i < cutted->size; i++){
data_tmp[i] = cutted->data[i];
}
free(cutted->data);
cutted->data = (int*)malloc(sizeof(int)*(a));
new->data = (int*)malloc(sizeof(int)*(cutted->size-a));
for(int i = 0; i < a; i++){
cutted->data[i] = data_tmp[i];
}
for(int i = a; i < cutted->size; i++){
new->data[i-a] = data_tmp[i];
}
new->next = cutted->next;
cutted->next = new;
new->size = cutted->size - a;
cutted->size = a;
return;
}
int main(){
Node* dummy_head;
Node* tail;
dummy_head = (Node*) malloc( sizeof(Node) );
dummy_head->data = NULL;
dummy_head->size = -1;
tail = dummy_head;
int N, M;
scanf("%d %d", &N, &M);
for(int i=0; i<N; i++){
tail->next = ReadOneList();
tail = tail->next;
}
char act[10];
int x, y;
for(int i=0; i<M; i++){
scanf("%s %d %d", act, &x, &y);
if( act[0] == 'M' ){
Merge(dummy_head, x, y);
}
else{
Cut(dummy_head, x, y);
}
}
PrintList(dummy_head);
return 0;
}Editor is loading...