有序单链表的建立
来源:互联网 发布:手机淘宝查询消费总额 编辑:程序博客网 时间:2024/05/16 12:14
从C和指针这本书上搬来的:
/***插入到一个有序地单链表,函数的参数是一个指**向链表第一个节点的指针以及需要插入的值。*/#include <stdio.h>#include <stdlib.h>#define TRUE 1#define FALSE 0typedef struct NODE{ struct NODE *next; int value;}Node;int insert1(Node **first,int new_value){ Node *current = *first; Node *pre = NULL; Node *newNode; newNode = (Node *)malloc(sizeof(Node)); if(newNode == NULL) return FALSE; newNode->value = new_value; while(current != NULL && current->value < new_value) //temp不为空,应在判断前! { pre = current; current = current->next; } newNode->next = current; if(pre == NULL) //新节点位于链表首部,包括空链表的情况 *first = newNode; else pre->next = newNode; return TRUE;}/***消除链表首部这种特殊情况*/int insert2(Node **nextp,int new_value){ Node *current = *nextp; Node *newNode; newNode = (Node *)malloc(sizeof(Node)); if(newNode == NULL) return FALSE; newNode->value = new_value; while((current= *nextp)!= NULL && current->value < new_value) //temp不为空,应在判断前! { nextp = &(current->next); } newNode->next = current; *nextp = newNode; //所有情况一致性处理! return TRUE;}/***测试用,输入无序整数,通过调用insert(),**建立有序链表,从而输出有序整数。*/int main(void){ Node *head = NULL; Node *temp; int count,value; count = 5; //测试整数的个数 printf("请输入%d个整数:\n",count); while(count--) { scanf("%d",&value);insert2(&head,value); } temp = head; while(temp) { printf("%d ",temp->value); temp = temp->next; } printf("\n"); return 0;}
- 有序单链表的建立
- 有序双链表的建立
- 有序链表的建立
- 有序链表的建立
- 有序链表的建立
- 有序链表的建立
- 数据结构——有序单链表的建立、插入与删除
- shell脚本建立规整有序的文件夹
- 数据结构——单链表的创建、逆置、插入、有序表的建立、有序单链表合并等基础操作!!
- 【学习点滴-数据结构-单链表】单链表的建立,遍历及有序单链表合并
- 数据结构实验之链表六:有序链表的建立
- 数据结构实验之链表六:有序链表的建立
- 将节点插入到建立起来的有序链表中
- 数据结构实验之链表六:有序链表的建立
- 数据结构实验之链表六:有序链表的建立
- 数据结构之链表六:有序链表的建立
- 数据结构实验之链表六:有序链表的建立
- 数据结构实验之链表六:有序链表的建立
- alsa 编程
- 个人博客wordpress搭建之环境基础篇
- 覆写onMeaure进行measure操作
- uva10002 Center of Masses
- 如何修炼才能成为伟大的程序员?
- 有序单链表的建立
- javascript介绍
- hdu 2544 最短路(spfa)
- 打印出任意年份的日历
- base64加解密
- BOX2D总体关系图
- 单链表的逆置
- “红色病毒”问题--递推
- HDU 4545 魔法串