链表的逆序 顺序建立

# include <stdio.h>
# include <malloc.h>
# include <string.h>
typedef struct node 
{
    struct node *next;
    int data;
}LINK;
int main()
{
    int i,j,n,leag=0;
    LINK *p,*head,*tail;
    head=(LINK *)malloc(sizeof(LINK));
    head->next=NULL;
    tail=head;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        p=(LINK *)malloc(sizeof(LINK));
        scanf("%d",&p->data);
        p->next=head->next;
        head->next=p;
        //tail->next=p;
        //tail=p;

    }
    p=head->next;
    for(i=0;i<n;i++)
    {
        if(i)printf(" ");
        printf("%d",p->data);
        p=p->next;
    }
    printf("\n");
    return 0;
    
}

 

posted on 2013-09-25 18:09  随风浪子的博客  阅读(103)  评论(0编辑  收藏  举报

导航