Skip to content

Instantly share code, notes, and snippets.

@hsuan1117
Created March 8, 2024 01:04
Show Gist options
  • Save hsuan1117/3193ad2838b7b55d1573395436a2b692 to your computer and use it in GitHub Desktop.
Save hsuan1117/3193ad2838b7b55d1573395436a2b692 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
typedef struct _Node {
char val;
struct _Node *next;
} Node;
Node *head[100005] = {};
Node *tail[100005] = {};
//Node *rev_head[100005] = {};
//Node *rev_tail[100005] = {};
char c[100005] = {};
//int rev[100005] ={};
Node* tmp;
void swap(int a, int b ) {
//swap(head_node)
tmp = head[a];
head[a] = head[b];
head[b] = tmp;
// swap(tail_node)
tmp = tail[a];
tail[a] = tail[b];
tail[a]= tmp;
//swap(rev_head)
// tmp = rev_head[a];
// rev_head[a] = rev_head[b];
// rev_head[b]=tmp;
//swap(rev_tail)
// tmp = rev_tail[a];
// rev_tail[a] = rev_tail[b];
// rev_tail[b] = tmp;
}
void append(int a, int b) {//pos ba
if (head[a] == NULL){
return;
}
if (head[b] == NULL) {
swap(a, b);
return;
}
if(tail[b])
tail[b]->next = head[a];
tail[b] = tail[a];
head[a] = NULL;
tail[a] = NULL;
// rev_tail[a]->next = rev_head[b];
// rev_head[b] = rev_head[a];
// rev_head[a] = NULL;
// rev_tail[a] = NULL;
}
void prepend(int a, int b) {//pre ab
if (head[a] == NULL){
return;
}
if (head[b] == NULL) {
swap(a, b);
return;
}
if(tail[a])
tail[a]->next = head[b];
head[b] = head[a];
head[a] = NULL;
tail[a] = NULL;
// rev_tail[b]->next = rev_head[a];
// rev_tail[b] = rev_tail[a];
// rev_head[a] = NULL;
// rev_tail[a] = NULL;
}
/*void reverse(int a) {
//printf("here\n");
if (head[a] == NULL) return;
// if(rev[a] == 0 ) rev[a] = 1 ;
// else rev[a] = 0;
// printf("here\n");
// printf("%c",head[a]->val);
Node* tmp_head , *tmp_tail ;
tmp_head = head[a];
tmp_tail = tail[a];
head[a] = rev_head[a];
tail[a] = rev_tail[a];
rev_head[a] = tmp_head ;
rev_tail[a] = tmp_tail;
//printf("%c",head[a]->val);
}*/
int main() {
int N;
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
int n;
scanf("%d", &n);
Node *tmp ;
// rev_head[i] = malloc(sizeof(Node));
head[i] = malloc(sizeof(Node));
//--
if(n!=0){
scanf("%s", c);
head[i]->val = c[0];
head[i]->next = NULL;
tmp = head[i];
for (int j = 1; j < n; j++) {
tmp->next = malloc(sizeof(Node));
tmp = tmp->next;
tmp->val = c[j];
tmp->next = NULL;
}
tail[i] = tmp;
/*
rev_head[i]->val = c[n - 1];
rev_head[i]->next = NULL;
tmp = rev_head[i];
for (int j = n-2 ; j >= 0; j--) {
tmp->next = malloc(sizeof(Node));
tmp = tmp->next;
tmp->val = c[j];
tmp->next = NULL;
}
rev_tail[i] = tmp;
*/
}else{
head[i]=NULL;
// rev_head[i]=NULL;
tail[i]=NULL;
// rev_tail[i]=NULL;
}
}
int q;
scanf("%d", &q);
for (int i = 0; i < q; i++) {
int type, a, b;
scanf("%d",&type);
if(type == 4) {
scanf("%d",&a);
}
else{
scanf("%d%d", &a, &b);
}
if (type == 1) {
prepend(a, b);
} else if (type == 2) {
append(a, b);
} else if (type == 3) {
swap(a, b);
} else {
// reverse(a);
}
for(int j=1; j<=N ; j++){
Node* output;
output = head[j] ;
printf("%d:",j);
while(output!=NULL){
printf("%c",output->val);
output = output->next;
}
printf("\n");
}
puts("");
}
for(int i=1 ; i<=N ; i++){
Node* output;
// if(!rev[i]) {
output = head[i] ;
while(output!=NULL){
printf("%c",output->val);
output = output->next;
}
printf("\n");
/*} else {
output = rev_head[i];
while(output!=NULL){
printf("%c",output->val);
output = output->next;
}
printf("\n");
}*/
}
for(int i=1;i<=N;i++) {
Node *tmp;
tmp = head[i];
while (tmp) {
Node *_ = tmp;
tmp = tmp->next;
free(_);
}
// tmp = rev_head[i];
// while(tmp) {
// Node* _ = tmp;
// tmp = tmp->next;
// free(_);
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment