首页 > 代码库 > C经典之14-双向链表存储1-10---ShinePans
C经典之14-双向链表存储1-10---ShinePans
#include <stdio.h> #include <conio.h> #include <stdlib.h> //system(); 这个指令需要用到此头文件 #include <ctype.h> //toupper要用到 #include <malloc.h> //在内存管理时用到的头文件 void main() { int i; struct ListEntry{ int number; //数据域 struct ListEntry *next; //指向 下一个ListEntry节点的指针 struct ListEntry *previous; //指向上一个 ListEntry节点的指针 }start,*node, *newOne; start.next = NULL; //置空链表 start.previous = NULL; node = &start; //指向链表的开始 for (i = 1; i < 10; i += 2) { node->next = (struct ListEntry*)malloc(sizeof(struct ListEntry)); node->next->previous = node; node = node->next; node->number = i; node->next = NULL; } for (i = 2; i <= 10; i += 2) { int found = 0; newOne = (struct ListEntry*)malloc(sizeof(struct ListEntry)); newOne->number = i; node = start.next; do{ if (node->number > newOne->number) { newOne->next = node; newOne->previous = node->previous; node->previous->next = newOne; node->previous = newOne; found = 1; } else{ node = node->next; } }while ((node->next) && (!found)); if (!found) if (node->number > newOne->number) { newOne->next = node; newOne->previous = node->previous; node->previous->next = newOne; node->previous = newOne; } else{ newOne->next = NULL; newOne->previous = node; node->next = newOne; } } node = start.next; do{ printf("%d", node->number); node = node->next; } while (node); system("pause"); }
这个代码非常经典,将1-10 不按书顺序存入双向链表,并且重新编排,使其最终输出 为 12345678910
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。