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

2117資料結構實驗之連結串列二:逆序建立連結串列

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