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

来源:互联网 发布:nba主帅数据排名 编辑:程序博客网 时间:2024/06/01 10:05

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

数据结构里的链表基本操作和以前一样,只不过更加规范化了。

#include <stdlib.h>#include <stdio.h>typedef int elemtype;typedef struct lnode{  elemtype data;  struct lnode *next;}sqlist,*linklist;linklist creat(){    lnode *head,*p,*tail;    head=new lnode;    head->next=NULL;    tail=head;    int x;    while(~scanf("%d",&x)&&x!=-1)    {        p=new lnode;        p->data=x;        p->next=NULL;        tail->next=p;        tail=p;    }    return head;}linklist change(linklist head){    lnode *p,*q;    p=head->next;    q=p->next;    head->next=NULL;    while(p)    {        p->next=head->next;        head->next=p;        p=q;        if(q!=NULL)            q=q->next;    }    return head;}void output(linklist head){    lnode *p;    p=head->next;    printf("%d",p->data);    p=p->next;    while(p)    {        printf(" %d",p->data);        p=p->next;    }    printf("\n");}int main(){    linklist head;    head=creat();    head=change(head);    output(head);}
原创粉丝点击