线性表链式存储(单链表)
2018-02-22 17:04
232 查看
#include<iostream> #define maxsize 20 #define error 0 #define ok 1 typedef int elemtype; typedef struct node { elemtype data; struct node *next; }node; typedef struct node *linklist; //search elemtype getelem(linklist l, int i, elemtype *e) { int j; linklist p; p = l->next; j = 1; while (p&&j < i) { p = p->next; ++j; } if (!p || j>i) return error; *e = p->data; return ok; } //insert elemtype listinsert(linklist *l, int i, elemtype e) { int j; linklist p, s; p = *l; j = 1; while (p&&j < i) { p = p->next; ++j; } if (!p || j>i) return error; s = new node; s->data = e; s->next = p->next; p->next = s; return ok; } //delete elemtype listdelete(linklist *l, int i,elemtype *e) { int j; linklist p, q; p = *l; j = 1; while (p->next&&j < i) { p = p->next; ++j; } if (!(p->next) || j>i) return error; q = q->next; p->next = q->next; *e = q->data; return ok; } //头插法 void createlisthead(linklist *l, int n) { linklist p; int i; *l = new node; //head (*l) -> next = NULL; for (i = 0; i < n; i++) { p = new node; p->next = (*l)->next; (*l)->next = p; } } //尾插法 void createlisttail(linklist *l, int n) { linklist p, r; int i; *l = new node; r = *l; for (i = 0; i < n; i++) { p->next = p; r = p; } r->next = NULL; }表中元素在内存中分散存储,所以需要额外增加指针单元来指出下一元素地址,不能随机访问,需要遍历,查找时间O(n)
增删时间O(1)(视情况而定,看是否需要遍历)。相比顺序表好处在于不用大规模修改元素地址。
相关文章推荐
- 线性表的Java实现--链式存储(单向链表)
- JAVA数据结构之线性表的链式存储结构——单链表
- [SDUT](2116)数据结构实验之链表一:顺序建立链表 ---链式存储(线性表)
- 线性表的链式存储——链表(带源码)
- 数据结构之线性表——链表的链式存储(链式描述)
- 结构之美:线性表的链式存储结构——链表
- 线性表的链式存储结构--链表
- 线性表的Java实现--链式存储(单向链表)
- 线性表的链式存储结构-单链表
- Java基础 - 线性表之链式存储结构-循环链表
- 结构之美:线性表的链式存储结构——链表
- 数据结构——线性表的伪链表存储(顺序存储链式遍历)
- 结构之美:线性表的链式存储结构——链表
- 线性表的链式存储(单链表)C语言实现
- C语言 数据结构 线性表 单链表 线性表的链式存储结构之一
- 数据结构之线性表链式存储(链表)
- 数据结构之线性表——链表的链式存储(链式描述)
- 数据结构之线性表-链式存储之静态链表(二)
- [SDUT](2117)数据结构实验之链表二:逆序建立链表 ---链式存储(线性表)
- Java基础 - 线性表之链式存储结构-双向链表