数据结构实验之链表三:链表的逆置

    xiaoxiao2022-07-13  172

    数据结构实验之链表三:链表的逆置

    Time Limit: 1000 ms Memory Limit: 65536 KiB

    Submit Statistic

    Problem Description

    输入多个整数,以-1作为结束标志,顺序建立一个带头结点的单链表,之后对该单链表的数据进行逆置,并输出逆置后的单链表数据。

    Input

    输入多个整数,以-1作为结束标志。

    Output

    输出逆置后的单链表数据。

    Sample Input

    12 56 4 6 55 15 33 62 -1

    Sample Output

    62 33 15 55 6 4 56 12

    Hint

    不得使用数组。

    Source

    本题与逆序建立链表相似,题目要求基本一样,就是输入不同,可以参照逆序建立链表进行稍作修改就可以将这道题AC,如果有不太懂逆序建立链表的朋友可以参照一下我的上一篇博客理解一下。这里仅给出C++语言的代码;

    https://blog.csdn.net/weixin_44015865/article/details/90487088(逆序建立链表参考)

    AC代码:

    #include<bits/stdc++.h> using namespace std; typedef struct node { int data; struct node*next; }tree[1100]; int main() { struct node*head,*tail,*p; head=new tree; head->next=NULL; int x; while(~scanf("%d",&x)&&x!=-1) { p=new tree; p->data=x; p->next=head->next; head->next=p; } p=head->next; while(p) { if(p->next==NULL) { printf("%d\n",p->data); } else { printf("%d ",p->data); } p=p->next; } return 0; }

     

    最新回复(0)