c++单链表拆分

1.功能描述:

假设有一个带头结点的单链表L,每个结点值由单个数字、小写字母和大写字母构成。设计一个算法将其拆分成3个带头结点的单链表L1、L2和L3,L1包含L中的所有数字结点,L2包含L中的所有小写字母结点,L3包含L中的所有大写字母结点。

2.关键源码:

头文件 ListNode.h

#include<iostream>
using namespace std;

typedef struct ListNode
{
    char data;
    struct ListNode *next;
};
void InitList(ListNode*& L);
void function(ListNode* L, ListNode* L1, ListNode* L2, ListNode* L3);
void CreatListR(ListNode*& L, char c[], int n);
void display(ListNode* L);

function.cpp

#include "ListNode.h"

void InitList(ListNode*& L)//建立一个新的头节点
{
    L = (ListNode*)malloc(sizeof(ListNode));
    L->next = NULL;
}


void CreatListR(ListNode*& L, char c[], int n)//尾插法初始化
{
    ListNode* s, * r;
    L = (ListNode*)malloc(sizeof(ListNode));
    r = L;
    for (int i = 0; i < n; i++)
    {
        s = (ListNode*)malloc(sizeof(ListNode));
        s->data = c[i];
        r->next = s;
        r = s;
    }
    r->next = NULL;
}

void display(ListNode* L)
{
    ListNode* p = L->next;
    while (p!= NULL)
    {
        printf("%c", p->data);
        p = p->next;
    }
    printf("\n");
}

void function(ListNode *L,ListNode * L1,ListNode * L2,ListNode * L3)
{
    ListNode* p = L->next; 

    ListNode* s1, * r1;
    r1 = L1;//指向L1表尾

    ListNode* s2, * r2;
    r2 = L2;//指向L2表尾

    ListNode* s3, * r3;
    r3 = L3;//指向L3表尾

    while (p!= NULL)
    {
        if (p->data >= 48 && p->data <= 57)//数字
        {
            s1 = (ListNode*)malloc(sizeof(ListNode));
            s1->data =p->data;
            r1->next = s1;
            r1 = s1;
        }
            
        else if (p->data >= 97 && p->data <= 122)//小写
        {
            s2 = (ListNode*)malloc(sizeof(ListNode));
            s2->data = p->data;
            r2->next = s2;
            r2 = s2;
        }
        else if(p->data>=65 &&p->data<=90)//大写
        {
            s3 = (ListNode*)malloc(sizeof(ListNode));
            s3->data = p->data;
            r3->next = s3;
            r3 = s3;
        }
        p = p->next;
    }
    r1->next = NULL;
    r2->next = NULL;
    r3->next = NULL;
}

主函数

#include "ListNode.h"

int main()
{
    char a[] = "123ADCsdf";
    ListNode* L = new ListNode();
    ListNode* L1 = new ListNode();
    ListNode* L2 = new ListNode();
    ListNode* L3 = new ListNode();
    
    int n = sizeof(a)/sizeof(a[0]);
    CreatListR(L, a, n);
    InitList(L1);
    InitList(L2);
    InitList(L3);
    function(L, L1, L2, L3);
    display(L);
    display(L1);
    display(L2);
    display(L3);  
}

3.成果展示

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你讲解一下如何使用C++单链表来存储火车信息。 首先需要定义一个火车信息的结构体,例如: ```c++ struct Train { string name; // 火车名 string from; // 起点站 string to; // 终点站 string time; // 发车时间 }; ``` 接下来,定义一个单链表节点的结构体,其中包含一个火车信息的结构体和一个指向下一个节点的指针,例如: ```c++ struct Node { Train train; // 火车信息 Node* next; // 指向下一个节点的指针 }; ``` 定义一个单链表的类,其中包含头节点指针和尾节点指针,以及一些操作方法,例如: ```c++ class TrainList { public: TrainList() { head = new Node(); tail = head; } ~TrainList() { Node* current = head; while (current != nullptr) { Node* tmp = current; current = current->next; delete tmp; } } void insert(const Train& train) { Node* node = new Node(); node->train = train; tail->next = node; tail = node; } void remove(const Train& train) { Node* current = head->next; Node* prev = head; while (current != nullptr) { if (current->train.name == train.name && current->train.from == train.from && current->train.to == train.to && current->train.time == train.time) { prev->next = current->next; if (current == tail) { tail = prev; } delete current; break; } prev = current; current = current->next; } } void print() const { Node* current = head->next; while (current != nullptr) { cout << "火车名:" << current->train.name << endl; cout << "起点站:" << current->train.from << endl; cout << "终点站:" << current->train.to << endl; cout << "发车时间:" << current->train.time << endl; cout << endl; current = current->next; } } private: Node* head; Node* tail; }; ``` 然后在主函数中,可以创建一个TrainList对象,调用insert方法添加火车信息,调用remove方法删除火车信息,调用print方法打印所有火车信息,例如: ```c++ int main() { TrainList trainList; Train train1 = {"G1", "北京", "上海", "08:00"}; Train train2 = {"G2", "上海", "北京", "14:00"}; Train train3 = {"D1", "北京", "天津", "09:00"}; trainList.insert(train1); trainList.insert(train2); trainList.insert(train3); trainList.print(); trainList.remove(train1); trainList.print(); return 0; } ``` 以上就是使用C++单链表存储火车信息的示例,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值