数据结构实验之链表三:链表的逆置(SDUT 2118)

版权声明:本文为原创文章QWQ,如果有错误欢迎指正,转载请注明作者且附带网址 https://blog.csdn.net/Mercury_Lc/article/details/82927816

题目链接

#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;
}

猜你喜欢

转载自blog.csdn.net/Mercury_Lc/article/details/82927816