zoukankan      html  css  js  c++  java
  • 数据结构实验之链表三:链表的逆置

    题目描述

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

    输入

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

    输出

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

    示例输入

    12 56 4 6 55 15 33 62 -1

    示例输出

    62 33 15 55 6 4 56 12

    提示

    不得使用数组。

    来源

    View Code
     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 struct node
     4 {
     5    int data ;
     6    struct node *next ;
     7 } ;
     8 struct node *creat()
     9 {
    10    struct node *head, *tail, *p ;
    11    int a ;
    12    head = ( struct node *)malloc(sizeof(struct node)) ;
    13    head -> next = NULL ;
    14    tail = head ;
    15  while(scanf("%d",&a),a!=-1)
    16  {
    17    p = (struct node *)malloc(sizeof(struct node)) ;
    18    p->data=a;
    19    p->next=NULL ;
    20    tail -> next=p ;
    21    tail=p;
    22  }
    23  return (head) ;
    24 }
    25 struct node  *reverse(struct node *head)
    26 {
    27   struct node *p, *q ;
    28   p = head->next ;
    29   head->next=NULL ;
    30   q = p->next ;
    31   while (p!=NULL)
    32  {
    33    p->next=head->next ;
    34    head -> next = p ;
    35    p = q ;
    36    if(q!=NULL)
    37    q = q->next ;
    38  }
    39  return (head);
    40 }
    41 int main()
    42 {
    43   struct node *head, *p ;
    44   struct node *r ;
    45   head = creat() ;
    46   r = reverse(head) ;
    47   p = head->next;
    48   while(p->next)
    49   {
    50     printf("%d ", p->data);
    51     p = p->next;
    52   }
    53     printf("%d\n", p->data);
    54    return 0;
    55 }
  • 相关阅读:
    03.移除相同的元素
    02.计算数组元素之和
    01-找出元素在数组中的位置
    node.js中Content-Type的设置
    node.js接受form表单数据
    node.js创建服务器
    mongoDB笔记
    TDK三大标签SEO(搜索引擎优化)优化
    引入网页图标
    JavaScript实现二叉搜索树
  • 原文地址:https://www.cnblogs.com/yelan/p/2871970.html
Copyright © 2011-2022 走看看