[2023 King's Road Data Structure] [Linear Table—page40—11] Complete implementation of C and C++ (can be run directly)

~~~How can the writing style end in a dull way, and the story does not recognize ordinary at the beginning ✌✌✌

If you need the complete code, you can follow the public account below, and reply "code" in the background to get it. Aguang is looking forward to your visit~

Topic:
insert image description here
Let C={a1,b1,a2,b2} be a linear table, use the hc singly linked list with the head node to store, design an in-place algorithm, split it into two linear tables, so that A={a1,a2 ,an}, B={bn,b2,b1}.

Problem solving ideas:

>问题的关键就是采用何种方式构建链表
>A采用尾插保持原顺序
>B采用前插法将其逆序

Code:

#include <iostream>
using namespace std;

typedef struct LNode
{
    int data;
    struct LNode *next;
} LNode, *LinkList;

// 头插法
void HeadInsert(LinkList &L)
{
    int val = 0;
    while (cin >> val)
    {
        LNode *s = new LNode;
        s->data = val;
        s->next = L->next;
        L->next = s;

        if (cin.get() == '\n')
        {
            break;
        }
    }
}

// 尾插法
void TailInsert(LinkList &L)
{
    int val = 0;
    LNode *r = L;
    while (cin >> val)
    {
        LNode *s = new LNode;
        s->data = val;
        r->next = s;
        r = s;
        r->next = NULL;

        if (cin.get() == '\n')
        {
            break;
        }
    }
}

// 遍历输出链表元素
void Print(LinkList L)
{
    LNode *p = L->next;
    while (p)
    {
        cout << p->data << '\t';
        p = p->next;
    }
    cout << endl;
}

void BreakList(LinkList &LA, LinkList &LB)
{
    LNode *p, *q, *ra; //工作指针、保存后继指针、LA尾指针
    LB->next = NULL;   //将LB链表置空
    p = LA->next;
    ra = LA; //初始化尾指针
    while (p)
    {
        // LA尾插
        ra->next = p;
        ra = p;
        p = p->next;

        //如果p不为空,继续将其前插到B
        if (p)
        {
            q = p->next; //保存p的后继,防止断链
            p->next = LB->next;
            LB->next = p;
            p = q;
        }
    }
    ra->next = NULL; //将A的尾结点的指针域置为空
}

int main()
{
    LinkList LA = new LNode;
    LinkList LB = new LNode;
    TailInsert(LA);

    BreakList(LA, LB);

    Print(LA);
    Print(LB);
}

Guess you like

Origin blog.csdn.net/m0_47256162/article/details/124468283