9.26 there is a problem! Do do another day

Reverse how to write a linked list also wrong. . . Good irritability ah ah ah ah, spent a lot of time feeling to collapse

#include<iostream>
using namespace std;
typedef struct LNode *List;
struct LNode {
    int data;
    List next;
};
List reverse(List &L)
{
    List p, r, s;
    p = L->next;
    L->next = NULL;
    r = p->next;
    p->next = NULL;
    while (r)
    {
        s = r;
        r->next = p;
        p = r;
        r = s->next;
    }
    L->next = p;
    return L;
}
void InitList(List &L)
{
    L = new LNode;
    L->next = NULL;
}
void CreatList(List &L, int n)
{
    List r = L;
    for (int i = 0; i < n; i++)
    {
        List q = new LNode;
        cin >> q->data;
        q->next = NULL;
        r->next = q;
        r = q;
    }
}
void Print(List &L, int n)
{
    List p = L->next;
    for (int i = 0; i < n; i++)
    {
        cout << p->data;
        p = p->next;
    }

}
int main()
{
    List L;
    InitList(L);
    CreatList(L, 5);
    Print(L, 5);
    List p = reverse(L);
    Print(p, 5);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/h694879357/p/11594070.html