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 );
    }

  • 相关阅读:
    setCookie
    EF getCookie
    EF
    Dapper修改
    Dapper显示
    Dapper上传图片
    Dapper存储过程分页
    Azure Function(.Net Cor框架)读取配置文件
    .Net Core3.1中出现AssemblyInfo特性重复
    YAML配置文件 基础学习
  • 原文地址:https://www.cnblogs.com/liangchao/p/2697460.html
Copyright © 2011-2022 走看看