栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

Description

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

Input

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

Output

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

Sample

Input 

12 56 4 6 55 15 33 62 -1

Output 

62 33 15 55 6 4 56 12
Hint

不得使用数组。

#include
#include
#include
struct node{
    int data;
    struct node *next;
};
int main(){
    struct node *head;
    head = (struct node *)malloc(sizeof(struct node));
    head -> next = NULL;
    struct node *p;
    int n;
    while(~scanf("%d", &n) && n != -1){
        p = (struct node *)malloc(sizeof(struct node));
        p -> data = n;
        p -> next = NULL;
        p -> next = head -> next;
        head -> next = p;
    }
    p = head -> next;
    while(p != NULL){
        if(p -> next != NULL){
            printf("%d ", p -> data);
        }
        else{
            printf("%dn", p -> data);
        }
        p = p -> next;
    }
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/879046.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号