倒序单链表

#include<stdio.h>
#include<stdlib.h>
struct link 
{
    int data;
    struct link *next;
};
struct link *invent(void);
void outp(struct link *head);
int main()
{
    struct link *head,*p;
    head=invent();
    p=head;
    outp(p);
    return 0;
}
struct link *invent (void)
{
    struct link *head=NULL,*tail,*new;
    int icount=0;
    while(1)
    {
        printf("请输入第%d个结点:",icount+1);
        new=(struct link *)malloc(sizeof(struct link));
        scanf("%d",&new->data );
        if(new->data ==-1)
        {
            free(new);
            return head;
        }
        else
        {
            icount++;
            if(icount  == 1)
            {
                head=tail=new;
                tail->next =NULL;
            }
            else
            {
                new->next=head;
                head=new;
            }
        }
    }
}
void outp(struct link *head)
{
    struct link *p=head;
    while(p)
    {
        printf("%d\n",p->data);
        p=p->next;
    } 
}

 

posted @ 2017-04-21 23:35  T.X  阅读(217)  评论(0编辑  收藏  举报