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

来源:互联网 发布:中国石油大学网络认证 编辑:程序博客网 时间:2024/06/05 21:11

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

12 56 4 6 55 15 33 62 -1

Example Output

62 33 15 55 6 4 56 12

Hint
不得使用数组。
Author

#include <stdio.h>#include <stdlib.h>#include <string.h>#include<bits/stdc++.h>#include <iostream>#include <algorithm>#define maxn 10100using namespace std;typedef struct list{    int data;    struct list *next;}sqlist;int main(){    sqlist *head, *mail;    head=(sqlist *)malloc(sizeof(sqlist));    head->data=0;    head->next=NULL;    mail=head;    while(1)    {        int kk;        scanf("%d", &kk);        if(kk==-1)break;        sqlist *p;        p=(sqlist *)malloc(sizeof(sqlist));        p->next=NULL;        p->data=kk;        mail->next=p;        mail=p;    }    mail=head->next;    head->next=NULL;    while(mail!=NULL)    {        sqlist *qq=mail->next;        mail->next=head->next;        head->next=mail;        mail=qq;    }    mail=head;    int top=1;    while(mail->next!=NULL)    {        if(top)top=0;        else printf(" ");        printf("%d", mail->next->data);        mail=mail->next;    }    return 0;}
0 0
原创粉丝点击