zoukankan      html  css  js  c++  java
  • 链表原地反转Demo

      现在就是Qt开发和给师弟师妹讲下数据结构吧,感觉还挺漫长的,上个Qt帖子等我把成品做出来再更。

     1 //Convert_plug.h
     2 
     3 #ifndef CONVERT
     4 #define CONVERT
     5 
     6 #define MAX 81
     7 typedef char NmaeType; 
     8 typedef struct _name_list
     9 {
    10     NmaeType name[81];
    11     struct _name_list *next;
    12 }Name_List;
    13 
    14 void convert_the_list(Name_List **);
    15 void print_list(Name_List *const, const char *);
    16 
    17 #endif // !CONVERT
     1 //Convert.cpp
     2 
     3 #include "convert_plug.h"
     4 #include <stdio.h>
     5 #include <stdlib.h>
     6 
     7 int main(int argc, char *argv[])
     8 {
     9     FILE *fp = fopen("D:\input.txt", "r");
    10     Name_List *head = NULL, *tmpCell = NULL, *listPre = NULL;
    11 
    12     for (;!feof(fp);)
    13     {
    14         tmpCell = (Name_List *)malloc(sizeof(Name_List));
    15         fscanf(fp, "%s", tmpCell->name);
    16 
    17         if (!listPre)
    18             head = tmpCell;//如果是空链表则创建链表头
    19         else         
    20             listPre->next = tmpCell;//如果不是,则上一个链表要连到当前链表上
    21 
    22         listPre = tmpCell, listPre->next = NULL;
    23     }
    24     print_list(head,"反转前:");
    25     convert_the_list(&head);
    26     print_list(head,"反转后:");
    27 
    28     fclose(fp);
    29     system("pause");
    30     return 0;
    31 }
    32 
    33 void print_list(Name_List *const listHead, const char *inform)
    34 {
    35     //输出所有链表的值
    36     Name_List *tmpCell = listHead;
    37     printf("%s", inform);
    38     for (; tmpCell != NULL; tmpCell = tmpCell->next)
    39         printf("%s ", tmpCell->name);
    40     printf("
    ");
    41 }
    42 
    43 void convert_the_list(Name_List **listHead)
    44 {
    45     if (listHead == NULL)
    46         return;
    47     Name_List 
    48           *listTmpCur = *listHead
    49         , *listTmpNext = (*listHead)->next
    50         , *listTmpPre = NULL;
    51 
    52     for (;listTmpNext != NULL;)
    53     {
    54         listTmpCur->next = listTmpPre;
    55         listTmpPre = listTmpCur;
    56         listTmpCur = listTmpNext;
    57         listTmpNext = listTmpNext->next;
    58     }
    59     listTmpCur->next = listTmpPre;
    60     *listHead = listTmpCur;
    61 
    62 }
  • 相关阅读:
    [刘阳Java]_MyBatis_其他方式来实现多表查询的操作_第9讲
    [刘阳Java]_MyBatis_实体关系映射_第8讲
    [刘阳Java]_MyBatis_动态SQL标签用法_第7讲
    [刘阳Java]_MyBatis_常规标签的用法_第6讲
    nodejs配置nginx 以后链接mongodb数据库
    es6学习
    学生管理系统
    node exprss-session 和connect-mongo
    容错处理try
    node错误中间件处理 express类 带有路由操作
  • 原文地址:https://www.cnblogs.com/Philip-Tell-Truth/p/5558216.html
Copyright © 2011-2022 走看看