链表原地反转Demo

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

 //Convert_plug.h

 #ifndef CONVERT
#define CONVERT #define MAX 81
typedef char NmaeType;
typedef struct _name_list
{
NmaeType name[];
struct _name_list *next;
}Name_List; void convert_the_list(Name_List **);
void print_list(Name_List *const, const char *); #endif // !CONVERT
 //Convert.cpp

 #include "convert_plug.h"
#include <stdio.h>
#include <stdlib.h> int main(int argc, char *argv[])
{
FILE *fp = fopen("D:\\input.txt", "r");
Name_List *head = NULL, *tmpCell = NULL, *listPre = NULL; for (;!feof(fp);)
{
tmpCell = (Name_List *)malloc(sizeof(Name_List));
fscanf(fp, "%s", tmpCell->name); if (!listPre)
head = tmpCell;//如果是空链表则创建链表头
else
listPre->next = tmpCell;//如果不是,则上一个链表要连到当前链表上 listPre = tmpCell, listPre->next = NULL;
}
print_list(head,"反转前:");
convert_the_list(&head);
print_list(head,"反转后:"); fclose(fp);
system("pause");
return ;
} void print_list(Name_List *const listHead, const char *inform)
{
//输出所有链表的值
Name_List *tmpCell = listHead;
printf("%s", inform);
for (; tmpCell != NULL; tmpCell = tmpCell->next)
printf("%s ", tmpCell->name);
printf("\n");
} void convert_the_list(Name_List **listHead)
{
if (listHead == NULL)
return;
Name_List
*listTmpCur = *listHead
, *listTmpNext = (*listHead)->next
, *listTmpPre = NULL; for (;listTmpNext != NULL;)
{
listTmpCur->next = listTmpPre;
listTmpPre = listTmpCur;
listTmpCur = listTmpNext;
listTmpNext = listTmpNext->next;
}
listTmpCur->next = listTmpPre;
*listHead = listTmpCur; }
上一篇:Unity3d GUI弹窗


下一篇:关于使用 jBox 对话框的提交问题