链表前插法

#include <iostream>

using namespace std;

struct LinkNode

{

    int data;

    LinkNode *next;

};

void init(LinkNode *&head)    //初始化头结点

{

    head=new LinkNode;

    head->next=NULL;

}

void create(LinkNode *&head)    //创建链表

{

    LinkNode *p;

    while(1)

    {

        p=new LinkNode;

        cin>>p->data;

        if(p->data < 0)

            break;

        p->next=head->next;

        head->next=p;

    }

}

void show(LinkNode *p)

{

    cout<<endl;

    while(p)

    {

    cout<<p->data<<" ";

     p=p->next;

    }

    cour<<endl;

}

int main ()

{

    LinkNode *head;

    init(head);

    create(head);

    show(head->next);

    return 0;

}

猜你喜欢

转载自blog.csdn.net/wrc_nb/article/details/79983353