码迷,mamicode.com
首页 > 其他好文 > 详细

C经典之14-双向链表存储1-10---ShinePans

时间:2019-05-23 19:39:18      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:oid   经典   div   print   main   new   pos   template   pid   

#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

C经典之14-双向链表存储1-10---ShinePans

标签:oid   经典   div   print   main   new   pos   template   pid   

原文地址:https://www.cnblogs.com/mqxnongmin/p/10913944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
分享档案
周排行
mamicode.com排行更多图片
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!