数据结构之单向链表实现
来源:互联网 发布:八爪鱼数据 编辑:程序博客网 时间:2024/05/01 05:31
在单向链表中,数据被存储在一个一个的“结点”中,这些“结点”在内存中并非是处于相邻的位置,而是“见缝插针”,由系统分配在一小块一小块的内存中。在进行插入数据的操作时,你需要告诉系统你需要一块内存,系统会根据你设定的大小在堆中寻找空闲的内存用来存储新的“结点”;而在进行删除操作时,你需要手动将要删除的“结点”的空间释放掉,同时注意将其置空(NULL)。这样,链表的存储结构克服了顺序表的插入与删除数据需要移动结点的操作,更加简便,同时也提高了空间的利用率。
//***************************************************// function:单向链表//////*************************************************#include <stdio.h>//#define datatype int;typedef struct LNode{ int data; struct LNode *next_node;}LNode,*Linklist;//1.创建空链表int initlist(Linklist L){ L = (Linklist)malloc(sizeof(struct LNode)); if(L == NULL) { printf("over flow!\n"); return -1; } L->next_node = NULL; return 0;}//2.判断是否为空链表int isempty(Linklist L){ if(L->next_node == NULL) return 0; else return -1;}//3.销毁链表int destorylist(Linklist L){ Linklist ptmp; while(L) { ptmp = L->next_node; free(L); L = NULL;//此处必须置空 L = ptmp; } return 0;}//4.清除链表中的所有元素,即将链表置为空int clearlist(Linklist L){ Linklist p,q; p = L->next_node; while(p) { q = p->next_node; free(p); p = NULL; p = q; } //L->data = 0; L->next_node = NULL; return 0;}//5.获取链表中的元素的个数int getlength(Linklist L){ int len = 0; if(!isempty(L)) return -1; while(L->next_node != NULL) { len ++; L = L->next_node; } return len;}//6.取链表中位置location处的元素int getvalue(Linklist L,int location,int value){ Linklist tmp; int i; tmp = L->next_node; while((tmp != NULL) && (location < i)) { i++; tmp = tmp->next_node; } if((tmp == NULL) || (location > i)) return -1; value = tmp->data; return value;}//7.定位元素value在表中的位置int locationlist(Linklist L,int value){ Linklist p; int location = 1; p = L->next_node; while(p != NULL) { p = p->next_node; location++; if(p->data == value) break; } if(p == NULL) { printf("is not here!\n"); return -1; } return location;}//8.取元素cur_e的前驱元素int prevalue(Linklist L,int cur_e,int pre_e){ Linklist p,q; p = L->next_node; while(p) { q = p->next_node; if(cur_e == q->data) { pre_e = p->data; return 0; } p = q; } return -1;}//9.取元素cur_e的后继元素int nextvalue(Linklist L,int cur_e,int next_e){ Linklist p,q; p = L->next_node; while(p) { q = p->next_node; if(cur_e == p->data) { next_e = q->data; return 0; } p = q; }}//10.在位置location处插入元素valueint insertvalue(Linklist L,int location,int value){ Linklist p,q,new; int i = 1; while(p && (i<location)) { i++; p = p->next_node; } if((p == NULL) || (i > location)) return -1; new = (Linklist)malloc(sizeof(struct LNode)); if(new == NULL) { printf("overflow!\n"); return -1; } new->data = value; new->next_node = p->next_node; p->next_node = new; return 0;}//11.删除位置location处的元素int deletevalue(Linklist L,int location){ Linklist p,q; int i = 1; p = L->next_node; while(p && (i<location)) { i++; p = p->next_node; } if((p == NULL) || (i > location)) return -1; //p->next_node = p->next_node->next_node; q = p->next_node; p->next_node = q->next_node; free(q); q = NULL; return 0;}//12.遍历输出链表中的所有元素void travellist(Linklist L){ int i = 0; Linklist p; p = L->next_node; while(p) { i++; printf("data[i]=%d\n",p->data); }}int main(void){ printf("signallist!\n"); return 0;}
- 数据结构之单向链表实现
- 动手实现 数据结构 之 “单向链表”
- java实现数据结构之单向链表
- 数据结构之单向链表
- 数据结构之单向链表
- 数据结构之单向链表
- 数据结构之单向链表
- 数据结构之单向链表
- 数据结构之单向链表(java实现)
- 数据结构学习之单向链表[附Java实现代码]
- 《数据结构之链表篇》--单向链表的C++实现
- 数据结构之单向链表的实现【C++】
- 数据结构练习--单向链表的实现
- 数据结构--单向链表C实现
- 单向链表实现数据结构中的栈
- 【数据结构】单向循环链表实现
- 数据结构学习之单向链表结构
- 数据结构之单向循环链表
- js屏蔽默认快捷键,调用自定义事件
- 详细解析Java中抽象类和接口的区别
- DB2 - v9版本安装许可证
- mysql的复制设置详解
- 【Android程序的反破解技术】笔记
- 数据结构之单向链表实现
- 红黑树
- usb掉线居然大有人在
- Linux内核驱动开发之KGDB单步调试内核(kgdboc方式)
- vnc安装及配置
- 话术
- 各种网贷的大概利率
- 在Eclipse中使用JUnit4进行单元测试
- struts1的基本知识点—简单构建第一个Action