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

来源:互联网 发布:网络实名制利大于弊 编辑:程序博客网 时间:2024/05/16 07:35

题目描述

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

输入

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

输出

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

示例输入

12 56 4 6 55 15 33 62 -1

示例输出

62 33 15 55 6 4 56 12
  1. #include<stdio.h>   
  2. #include<stdlib.h>   
  3. struct node   
  4. {   
  5.     int date;   
  6.     struct node *next;   
  7. };   
  8. struct node *head,*p,*q;   
  9. int main()   
  10. {   
  11.     head=(struct node*)malloc(sizeof(struct node));    
  12.     head->next=NULL;   
  13.     while(1)   
  14.     {   
  15.         p=(struct node*)malloc(sizeof(struct node)*1);   
  16.         scanf("%d",&p->date);   
  17.         if(p->date==-1)   
  18.             break;   
  19.         p->next=head->next;   
  20.         head->next=p;   
  21.        
  22.     }   
  23.     q=head->next;   
  24.     while(q!=NULL)   
  25.     {   
  26.         printf("%d",q->date);   
  27.         if(q->next!=NULL)   
  28.             printf(" ");   
  29.             q=q->next;   
  30.     }   
  31.     printf("\n");   
  32.     return 0;   
  33. }    
  34.   
0 0
原创粉丝点击