您的位置:首页 > 其它

链表

2016-07-11 17:00 197 查看
链表的创建

用head,p1,p2三个指针,分别指向头结点,新创建的结点,最后的结点。

#include<iostream>

#include<cstdio>

#include<cstring>

#include<string>

#include<cstdlib>

#include<algorithm>

using namespace std;

struct node

{

    int a;

    struct node *next;

};

int main()

{

    int n;

    cin>>n;

    n--;

    node *head,*p1=NULL,*p2=NULL;

    p1=(struct node *)malloc(sizeof(struct node));

    head=p1;

    cin>>p1->a;

    p2=p1;

    while(n--)

    {

        p1=(struct node *)malloc(sizeof(struct node));

        cin>>p1->a;

        p2->next=p1;

        p2=p1;

    }

    p2->next=NULL;

    p1=head;

    while(p1->next!=NULL)

    {

        cout<<p1->a<<endl;

        p1=p1->next;

    }

    cout<<p1->a;

    free(p1);

    p1=NULL;

    return 0;

}

链表的比较好的博客文章http://blog.csdn.net/hackbuteer1/article/details/6591486/:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: