指针的指针---一个有序链表的实现
来源:互联网 发布:协同过滤的算法有哪些 编辑:程序博客网 时间:2024/06/05 00:49
#include <stdio.h>#include <stdlib.h>#include <math.h>#define BOOL int#define FALSE 0#define TRUE 1typedef struct _Node{ int value; struct _Node *next;}Node;BOOL insert(Node **root,int new_value);void print(Node *root);int main (int argc, const char * argv[]){ Node *root; insert(&root, 22); insert(&root, 23); insert(&root, 12); insert(&root, 26); print(root); int a = 10; int *p = &a; printf("%d,%d",p,*p); return 0;}BOOL insert(Node **root,int new_value){ Node *current; Node *previous; Node *new; //root指的就是链表首节点的指针的地址, //现在current指向root指向的内容,就是链表指针,也就是链表首节点的地址, //如果改变current的值只能影响到链表中的内容,不能影响到链表首地址的地址。 //如果想改变链表首地址的地址,只能用*root。 current = *root; previous = NULL; while (current != NULL && current->value < new_value) { previous = current; current = current->next; } new = (Node *)malloc(sizeof(Node)); if (new == NULL) { return FALSE; } new->value =new_value; new->next = current; if (previous == NULL) { *root = new; } else previous->next = new; return TRUE; }void print(Node *root){ while (root != NULL) { printf("%d\n",root->value); root = root->next; }}
实现了添加和打印出链表内容的insert和print函数。
因为链表是有序的,所以如果插入的元素是最小的,那么就要改变链表首元素的地址。如果insert参数是Node*类型的,那么只能改变
链表的内容,而不能改变链表首元素地址。
- 指针的指针---一个有序链表的实现
- 一个智能指针的实现
- 给你两个有序链表,编写一个函数,把两个链表合并成一个新的有序链表,返回头指针
- 一个双向链表的单指针实现
- 表的指针实现
- 递归实现有序链表(不需要使用两个指针)
- 向头指针为 L的有序单链表(从小到大有序)中插入一个结点,使插入后链表仍然有序
- 把两个有序链表合并为一个有序链表(注意空指针异常!)
- 写程序。设ha和hb分别是两个带头结点的非递减有序单链表的头指针,试设计算法,将这两个有序链表合并成一个非递增有序的单链表。要求使用原链表空间,
- 智能指针原理,并实现一个简单的智能指针
- 一个简单的智能指针的实现
- 一个简单的智能指针的实现
- 一个智能指针的实现(代码)
- 一个智能指针的实现(改进)
- 一个智能指针的实现(代码)
- 一个简单的智能指针实现
- 一个C++智能指针的实现
- 一个智能指针的简单实现
- Makefile中的 wildcard patsubst
- 关于RemoteView 的一些字体的一些问题
- 4.3对象结构型模式(composite)
- 使用Directshow+Xvid采集并压缩avi视频
- HOJ 3034 Mysterious (二分)
- 指针的指针---一个有序链表的实现
- VSTO程序基本知识(三)
- 游戏:VIM大冒险(哈哈,真是有才,即玩游戏,又熟悉vim快捷键)
- window下安装virtualBox,Ubuntu
- 这是怎样的一个女孩子
- VSTO程序基本知识(四)
- HDU 4127
- linux工作队列
- Excel转换成XML文件