155 lines
2.8 KiB
C
155 lines
2.8 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
typedef struct Node {
|
||
|
int data;
|
||
|
struct Node *next;
|
||
|
} Node;
|
||
|
|
||
|
Node *constructLinkedList();
|
||
|
Node *appendLinkedList(Node *node, int data);
|
||
|
void deleteLinkedList(Node *head);
|
||
|
Node *segmentLinkedList(Node *head, Node **nextHead, int segLen);
|
||
|
// returns the head of segmented Linked list, writes the head of next linked
|
||
|
// list to *nextHead
|
||
|
void connectLinkedList(Node *head1, Node *head2);
|
||
|
// appends list2 to the end of list1
|
||
|
void printLinkedList(Node *head);
|
||
|
|
||
|
int main(void) {
|
||
|
Node *head = constructLinkedList();
|
||
|
/* printLinkedList(head); */
|
||
|
|
||
|
int s1, t1, s2, t2;
|
||
|
scanf("%d%d%d%d", &s1, &t1, &s2, &t2);
|
||
|
|
||
|
Node *tmp;
|
||
|
Node *ptr = head;
|
||
|
// start to iterate to s1
|
||
|
for (int i = 1; i < s1; i++) {
|
||
|
if (i == s1 - 1) {
|
||
|
tmp = ptr->next;
|
||
|
ptr->next = NULL;
|
||
|
ptr = tmp;
|
||
|
} else {
|
||
|
ptr = ptr->next;
|
||
|
}
|
||
|
}
|
||
|
Node *seg1, *seg2 = NULL, *seg3, *seg4 = NULL;
|
||
|
// get the first segment
|
||
|
seg1 = segmentLinkedList(ptr, &seg2, t1 - s1);
|
||
|
ptr = seg2;
|
||
|
|
||
|
for (int i = 0; i < s2 - t1 - 1; i++) {
|
||
|
if (i == s2 - t1 - 2) {
|
||
|
tmp = (Node*)(ptr->next);
|
||
|
ptr->next = NULL;
|
||
|
ptr = tmp;
|
||
|
} else {
|
||
|
ptr = ptr->next;
|
||
|
}
|
||
|
}
|
||
|
seg3 = segmentLinkedList(ptr, &seg4, t2 - s2);
|
||
|
|
||
|
// special cases:
|
||
|
// head == seg1
|
||
|
// seg2 == seg3
|
||
|
// seg4 == NULL
|
||
|
|
||
|
Node *newHead = head;
|
||
|
|
||
|
if (head != seg1) {
|
||
|
connectLinkedList(head, seg3);
|
||
|
} else {
|
||
|
newHead = seg3;
|
||
|
}
|
||
|
if (seg2 != seg3) {
|
||
|
connectLinkedList(seg3, seg2);
|
||
|
}
|
||
|
connectLinkedList(seg2, seg1);
|
||
|
if (seg4 != NULL) {
|
||
|
connectLinkedList(seg1, seg4);
|
||
|
}
|
||
|
|
||
|
printf("The new list is:");
|
||
|
printLinkedList(newHead);
|
||
|
|
||
|
deleteLinkedList(newHead);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
Node *constructLinkedList() {
|
||
|
int buf;
|
||
|
Node *head = malloc(sizeof(Node));
|
||
|
scanf("%d", &buf);
|
||
|
head->data = buf;
|
||
|
head->next = NULL;
|
||
|
|
||
|
Node *ptr = head;
|
||
|
|
||
|
scanf("%d", &buf);
|
||
|
while (buf != -1) {
|
||
|
ptr = appendLinkedList(ptr, buf);
|
||
|
scanf("%d", &buf);
|
||
|
}
|
||
|
|
||
|
return head;
|
||
|
}
|
||
|
|
||
|
Node *appendLinkedList(Node *node, int data) {
|
||
|
Node *new = malloc(sizeof(Node));
|
||
|
|
||
|
new->next = NULL;
|
||
|
new->data = data;
|
||
|
|
||
|
node->next = new;
|
||
|
|
||
|
return new;
|
||
|
}
|
||
|
|
||
|
void deleteLinkedList(Node *head) {
|
||
|
if (head == NULL) {
|
||
|
return;
|
||
|
} else {
|
||
|
deleteLinkedList(head->next);
|
||
|
free(head);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Node *segmentLinkedList(Node *head, Node **nextHead, int segLen) {
|
||
|
Node *ptr = head;
|
||
|
for (int i = 0; i < segLen; i++) {
|
||
|
ptr = ptr->next;
|
||
|
}
|
||
|
|
||
|
if (ptr->next != NULL) {
|
||
|
*nextHead = ptr->next;
|
||
|
}
|
||
|
ptr->next = NULL;
|
||
|
|
||
|
return head;
|
||
|
}
|
||
|
|
||
|
void connectLinkedList(Node *head1, Node *head2) {
|
||
|
Node *ptr = head1;
|
||
|
|
||
|
while (ptr->next != NULL) {
|
||
|
ptr = ptr->next;
|
||
|
}
|
||
|
|
||
|
ptr->next = head2;
|
||
|
}
|
||
|
|
||
|
void printLinkedList(Node *head) {
|
||
|
if (head != NULL)
|
||
|
printf("%d", head->data);
|
||
|
|
||
|
Node *ptr = head;
|
||
|
ptr = head->next;
|
||
|
|
||
|
while (ptr != NULL) {
|
||
|
printf(" %d", ptr->data);
|
||
|
ptr = ptr->next;
|
||
|
}
|
||
|
}
|