您的位置:首页 > 理论基础 > 数据结构算法

数据结构实验之链表三:链表的逆置

2014-01-15 20:38 274 查看
#include <stdio.h>

#include <malloc.h>

struct node

{

    int data;

    struct node *next;

};

int main()

{

    struct node *head,*p,*tail,*q,*r;

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

    head->next=NULL;

    tail=head;

    while(1)

    {

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

        scanf("%d",&p->data);

        if(p->data==-1)break;

        p->next=NULL;

        tail->next=p;

        tail=p;

    }

    p=head->next;

    head->next=NULL;

    q=p->next;

    while(p)

    {

       p->next=head->next;

       head->next=p;

       p=q;

       if(q)

        q=q->next;

    }

    r=head;

    while(r->next!=NULL)

    {

        printf("%d ",r->next->data);

        r=r->next;

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: