问题 F 实验11_15_拆分链表
题目描述
已知有一个乱序的字符序列L,序列中的字符可能是英文字母、数字字符或其它字符,字符的个数未知,每个字符之间用空格分开。字符序列用“-1”作为输入结束标志,这里你要把-1当做一个字符串对待,并且不算作字符序列中的元素。如下即为一个合法的字符序列:“a c 3 b a d 6 , & j m 8 7 2 V -1”。你的任务是将这个字符序列拆分为三个独立的序列A、B和C,其中序列A存放序列L中的字母,序列B存放序列L中的数字,序列C存放序列L中的其他字符,然后,将序列A、B和C分别按照ASCII码的大小关系进行升序排序。最终序列L将变为空序列。
要求:
建立四个单链表,分别存储序列L、A、B、C中的元素。字符序列的输入用“-1”作为结束标志。建立链表L时,建议使用scanf(“%s”,s);来读取字符序列中的字符,即把单独的字符看做一个字符串读取。当L建立后,你要按照问题描述中所述,将L拆分为A、B、C三个链表,然后对每个链表都进行排序,这部分的操作都应该是对指针进行修改,而不是删除节点与建立新节点。在程序结束前要释放链表A、B、C中的所有节点。
输入
一个乱序的字符序列,序列元素的个数未知,以输入“-1”结束,输入“-1”前可能没有其它元素,每个字符序列占一行。
输出
链表A中的元素,占一行;然后是链表B中的元素,占一行。最后是链表C中的元素,占一行。每行的每个元素后有一个空格,注意最后一个元素后只有换行符,如果某个链表为空则,则输出“There is no item in X list.”
数据最多的测试用例节点数在100这个数量级。
请注意输入输出格式。
样例输入 Copy
Sample 1:
a c 3 b a d 6 , & j m 8 7 2 V -1
Sample 2:
z m v 1 a K 2 m p 9 a 0 a d -1
样例输出 Copy
Sample 1:
The list A is: V a a b c d j m
The list B is: 2 3 6 7 8
The list C is: & ,
Sample 2:
The list A is: K a a a d m m p v z
The list B is: 0 1 2 9
There is no item in C list.
/* creat the linked list and add l to a,b,c and sort it
@author: CangCheng
@date: 18/3/2021
*/
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
char data[3];
int *nextPtr;
};
struct node *creatLinkedList(struct node *headPtr);
struct node *creatNullLinkedList(struct node *headPtr);
void delLinkedList(struct node *headPtr);
void add_L_tO_ABC(struct node *headPtrL, struct node *headPtrA, struct node *headPtrB, struct node *headPtrC);
void ifSortAndSort(struct node *headPtrA, struct node *headPtrB, struct node *headPtrC);
void sortLinkedList(struct node *headPtr);
int getNodeNum(struct node *headPtr);
void printABC(struct node *headPtrA, struct node *headPtrB, struct node *headPtrC);
void printList(struct node *headPtr);
int main() {
struct node *headPtrL = NULL, *headPtrA = NULL, *headPtrB = NULL, *headPtrC = NULL;
headPtrL = creatLinkedList(headPtrL);
headPtrA = creatNullLinkedList(headPtrA);
headPtrB = creatNullLinkedList(headPtrB);
headPtrC = creatNullLinkedList(headPtrC);
add_L_tO_ABC(headPtrL, headPtrA, headPtrB, headPtrC);
ifSortAndSort(headPtrA, headPtrB, headPtrC);
printABC(headPtrA, headPtrB, headPtrC);
delLinkedList(headPtrL);
delLinkedList(headPtrA);
delLinkedList(headPtrB);
delLinkedList(headPtrC);
return 0;
}
//创建有空节点的链表
struct node *creatLinkedList(struct node *headPtr) {
struct node *newPtr, *currentPtr;
char num[3];
newPtr = malloc(sizeof(struct node));
newPtr->nextPtr = NULL;
headPtr = newPtr;
scanf("%s", num);
while (num[0] != '-' || num[1] != '1') {
newPtr = malloc(sizeof(struct node));
strcpy(newPtr->data, num);
newPtr->nextPtr = NULL;
if (headPtr->nextPtr == NULL) {
currentPtr = newPtr;
headPtr->nextPtr = currentPtr;
} else {
currentPtr->nextPtr = newPtr;
currentPtr = newPtr;
}
scanf("%s", num);
}
return headPtr;
}
//创建空的空链表
struct node *creatNullLinkedList(struct node *headPtr) {
struct node *newPtr;
newPtr = malloc(sizeof(struct node));
newPtr->nextPtr = NULL;
headPtr = newPtr;
return headPtr;
}
//删除链表
void delLinkedList(struct node *headPtr) {
struct node *thisPtr, *thatPtr;
thisPtr = headPtr;
while (thisPtr != NULL) {
thatPtr = thisPtr->nextPtr;
free(thisPtr);
thisPtr = thatPtr;
}
}
//将L移至ABC
void add_L_tO_ABC(struct node *headPtrL, struct node *headPtrA, struct node *headPtrB, struct node *headPtrC) {
struct node *thisPtrL, *thisPtrA, *thisPtrB, *thisPtrC;
thisPtrL = headPtrL->nextPtr;
thisPtrA = headPtrA;
thisPtrB = headPtrB;
thisPtrC = headPtrC;
while (thisPtrL != NULL) {
if ((thisPtrL->data[0] >= 'a' && thisPtrL->data[0] <= 'z') ||
(thisPtrL->data[0] >= 'A' && thisPtrL->data[0] <= 'Z')) {
headPtrL->nextPtr = thisPtrL->nextPtr;
thisPtrA->nextPtr = thisPtrL;
thisPtrA = thisPtrA->nextPtr;
thisPtrA->nextPtr = NULL;
thisPtrL = headPtrL->nextPtr;
continue;
} else if (thisPtrL->data[0] >= '0' && thisPtrL->data[0] <= '9') {
headPtrL->nextPtr = thisPtrL->nextPtr;
thisPtrB->nextPtr = thisPtrL;
thisPtrB = thisPtrB->nextPtr;
thisPtrB->nextPtr = NULL;
thisPtrL = headPtrL->nextPtr;
continue;
} else {
headPtrL->nextPtr = thisPtrL->nextPtr;
thisPtrC->nextPtr = thisPtrL;
thisPtrC = thisPtrC->nextPtr;
thisPtrC->nextPtr = NULL;
thisPtrL = headPtrL->nextPtr;
continue;
}
}
}
//升序排序
void sortLinkedList(struct node *headPtr) {
struct node *thisPtr, *thatPtr, *lastPtr;
int nodeNum, flag;
nodeNum = getNodeNum(headPtr);
for (int i = 1; i <= nodeNum - 2; i++) {
lastPtr = headPtr;
thisPtr = lastPtr->nextPtr;
thatPtr = thisPtr->nextPtr;
for (int j = 1; j <= nodeNum - 2; j++) {
if (thisPtr->data[0] > thatPtr->data[0]) {
thisPtr->nextPtr = thatPtr->nextPtr;
thatPtr->nextPtr = thisPtr;
lastPtr->nextPtr = thatPtr;
}
lastPtr = lastPtr->nextPtr;
thisPtr = lastPtr->nextPtr;
thatPtr = thisPtr->nextPtr;
}
}
}
//得到节点数
int getNodeNum(struct node *headPtr) {
int num = 0;
struct node *thisPtr;
thisPtr = headPtr;
while (thisPtr != NULL) {
thisPtr = thisPtr->nextPtr;
num++;
}
return num;
}
//将ABC分别排序
void ifSortAndSort(struct node *headPtrA, struct node *headPtrB, struct node *headPtrC) {
if (headPtrA->nextPtr != NULL) {
sortLinkedList(headPtrA);
}
if (headPtrB->nextPtr != NULL) {
sortLinkedList(headPtrB);
}
if (headPtrC->nextPtr != NULL) {
sortLinkedList(headPtrC);
}
}
//打印输出
void printABC(struct node *headPtrA, struct node *headPtrB, struct node *headPtrC) {
if (headPtrA->nextPtr == NULL) {
printf("There is no item in A list.\n");
} else {
printf("The list A is: ");
printList(headPtrA);
}
if (headPtrB->nextPtr == NULL) {
printf("There is no item in B list.\n");
} else {
printf("The list B is: ");
printList(headPtrB);
}
if (headPtrC->nextPtr == NULL) {
printf("There is no item in C list.\n");
} else {
printf("The list C is: ");
printList(headPtrC);
}
}
//打印链表
void printList(struct node *headPtr) {
struct node *thisPtr;
thisPtr = headPtr->nextPtr;
while (thisPtr != NULL) {
if (thisPtr->nextPtr != NULL) {
printf("%c ", thisPtr->data[0]);
} else if (thisPtr->nextPtr == NULL) {
printf("%c\n", thisPtr->data[0]);
}
thisPtr = thisPtr->nextPtr;
}
}