zoukankan      html  css  js  c++  java
  • 链表的基本操作,逆置——C语言

    #include<stdlib.h>

    #include<stdio.h>
    typedef struct node {
      int data;
      struct node *next;
    }Node;

    Node *CreatList( int n )    /*n用于指定链表长度*/
    {
      int val, i;
      Node *phead, *p;
      phead = ( Node * )malloc( sizeof( Node ) );    /*建立一个带头结点的链表*/
      phead->next = NULL;
      printf( "输入链表节点数据:\n" );
      for( i = 0; i < n; i++ )
      {
        scanf( "%d", &val );
        p = ( Node * )malloc( sizeof( Node ) );
        p->data = val;
        p->next = phead->next;    /*倒插*/
        phead->next = p;
      }
      return phead;
    }

    void ReverseList( Node *phead )
    {
      Node *p, *q, *r;
      p = phead->next;
      q = NULL;    
      r = NULL;
      while( p->next != NULL )    /*r, q分别用于标记p的前后结点*/
      {
        q = p->next;
        p->next = r;
        r = p;
        p = q;
      }
      p->next = r;
      phead->next = p;
    }

    void PrintList( Node *phead )
    {
      Node *p;
      p = phead->next;
      while( p != NULL )
      {
        printf( "%d ", p->data );
        p = p->next;
      }
      printf( "\n" );
    }

    main()
    {
      Node *phead;
      phead = CreatList( 10 );
      PrintList( phead );
      ReverseList( phead );
      PrintList( phead );
    }

  • 相关阅读:
    ajax 中文乱码问题 主要是IE浏览器
    js 带省略号的分页源码及应用实例
    js iframe onload &line-height浏览器兼容问题
    Js 正则表达式特殊字符含义
    js prototype
    js call apply caller callee bind
    怎么查看一个网站是用什么语言编写的?
    ASP.NET之AreaRegistration
    产品经理
    程序猿全面充电10本书
  • 原文地址:https://www.cnblogs.com/liangchao/p/2697460.html
Copyright © 2011-2022 走看看