问题 F 实验11_15_拆分链表

问题 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;
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
这道题目是要求我们实现链表的基本操作,包括创建链表、插入节点、删除节点、遍历链表等。以下是一个简单的链表实现代码示例: ```c++ #include <iostream> using namespace std; // 定义链表节点结构体 struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; // 创建链表 ListNode* createList(int nums[], int n) { if (n == 0) return NULL; ListNode *head = new ListNode(nums[0]); ListNode *cur = head; for (int i = 1; i < n; i++) { cur->next = new ListNode(nums[i]); cur = cur->next; } return head; } // 插入节点 void insertNode(ListNode *&head, int pos, int val) { if (pos < 0) return; if (pos == 0) { ListNode *newNode = new ListNode(val); newNode->next = head; head = newNode; return; } ListNode *cur = head; while (cur && pos > 1) { cur = cur->next; pos--; } if (cur) { ListNode *newNode = new ListNode(val); newNode->next = cur->next; cur->next = newNode; } } // 删除节点 void deleteNode(ListNode *&head, int pos) { if (pos < 0) return; if (pos == 0) { ListNode *delNode = head; head = head->next; delete delNode; return; } ListNode *cur = head; while (cur && pos > 1) { cur = cur->next; pos--; } if (cur && cur->next) { ListNode *delNode = cur->next; cur->next = delNode->next; delete delNode; } } // 遍历链表 void traverseList(ListNode *head) { while (head) { cout << head->val << " "; head = head->next; } cout << endl; } int main() { int nums[] = {1, 2, 3, 4, 5}; int n = sizeof(nums) / sizeof(int); ListNode *head = createList(nums, n); traverseList(head); insertNode(head, 2, 6); traverseList(head); deleteNode(head, 3); traverseList(head); return 0; } ``` 在上面的代码中,我们定义了一个 `ListNode` 结构体作为链表节点,包括节点值 `val` 和指向下一个节点的指针 `next`。同时,我们实现了创建链表、插入节点、删除节点和遍历链表等基本操作。在使用链表时,我们可以先通过 `createList` 函数创建一个链表,然后对链表进行插入、删除和遍历操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值