1. 程式人生 > >資料結構實驗之連結串列一:順序建立連結串列

資料結構實驗之連結串列一:順序建立連結串列

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
int main()
{
    int i,n;
    struct node *head,*p,*tail;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        p=(struct node*)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    p=head;
    while(p->next!=NULL)
    {
        printf("%d ",p->next->data);
        p=p->next;
    }
    return 0;
}