1. 程式人生 > >資料結構實驗之連結串列三:連結串列的逆置(SDUT 2118)

資料結構實驗之連結串列三:連結串列的逆置(SDUT 2118)

題目連結

#include <bits/stdc++.h>

using namespace std;

struct node
{
    int data;
    struct node *next;
};
int main()
{
    int n;
    struct node *head,*p;
    head = new node;
    head -> next = NULL;
    while(~scanf("%d", &n))
    {
        if(n == -1)
            break;
        p = new node;
        p -> next = NULL;
        p -> data = n;
        p -> next = head -> next;
        head -> next = p;
    }
    for(p = head -> next ; p != NULL; p = p -> next)
    {
        if(p == head -> next)
            printf("%d",p->data);
        else
            printf(" %d",p->data);
    }
    printf("\n");
    return 0;
}