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

来源:互联网 发布:mac相簿批量删除照片 编辑:程序博客网 时间:2024/04/30 01:54
#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;
}
0 0
原创粉丝点击