• 链表的基本操作,逆置——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 );
    }

  • 相关阅读:
    python之timeit模块
    python isinstance函数
    继承
    冒泡排序&&选择排序
    监听器
    被钝化的代码
    Ajax验证用户名
    原生ajax访问服务器所展现的现象
    今天
    随机点名
  • 原文地址:https://www.cnblogs.com/liangchao/p/2697460.html
Copyright © 2020-2023  润新知