赞
踩
哈喽大家好喔,今天博主继续进行数据结构的分享与学习,今天的主要内容是顺序表与链表,是最简单但又相当重要的数据结构,为以后的学习有重要的铺垫,希望大家一起交流学习,互相进步,让我们开始吧。
数据结构是计算机科学中非常重要的一部分,用于组织和管理数据以便高效地访问和修改。顺序表和链表是两种常见且基础的数据结构,它们各有特点和适用场景。以下是对这两种数据结构的详细分析:
顺序表是在计算机内存中以数组的形式保存的线性表。其有点类似数组,但添加了几个额外的概念。线性表的顺序存储是指用一组地址连续的存储单元依次存储线性表中的各个元素,使得线性表中在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中。
这里面有俩个定义顺序表功能的俩个变量:分别是count和size变量,size是用来确定顺序表的存储空间大小的,count是用来标记顺序表中当前元素位置的。
- 物理存储连续性:顺序表中的所有元素在内存中占有一段连续的存储空间。
- 随机访问:顺序表支持通过下标快速访问任意位置的元素,访问时间复杂度为O(1)。
- 空间分配:
- 静态顺序表:使用定长数组存储元素,适用于数据大小已知的场景,但灵活性较差。
- 动态顺序表:使用动态开辟的数组存储,可以根据需要动态调整存储空间大小,更加灵活。
- 插入和删除操作:由于物理连续性,插入和删除操作需要移动元素,效率较低,时间复杂度为O(n)。
- typedef struct vector {
- int size, count;
- int *data;
- //总大小,元素数量
- //指针指向连续的存储区
- } vector;
- //初始化
- vector *getNewVector(int n) {
- vector *p = (vector *)malloc(sizeof(vector));
- p->size = n;//存储上限
- p->count = 0;
- p->data = (int *)malloc(sizeof(int) * n);
- return p;
- }
-
- //插入操作
- int insert(vector *v, int pos, int val) {
- if (pos < 0 || pos > v->count) return 0;
- if (v->size == v->count && !expand(v)) return 0;
- for (int i = v->count - 1; i >= pos; i--) {
- v->data[i + 1] = v->data[i];
- }
- v->data[pos] = val;
- v->count += 1;
- return 1;
- }
- //删除操作
- void clear(vector *v) {
- if (v == NULL) return ;
- free(v->data);
- free(v);
- return ;
- }
- //销毁操作
- int erase(vector *v, int pos) {
- if (pos < 0 || pos >= v->count) return 0;
- for (int i = pos + 1; i < v->count; i++) {
- v->data[i - 1] = v->data[i];
- }
- v->count -= 1;
- return 1;
- }
需要顺序表扩容的俩个条件:
- 插入到非法位置,即位置小于0或大于等于size。
- 顺序表满了之后如果还需要添加新的元素,就需要扩容。
realloc是 C 标准库中的一个函数,用于调整已经分配的内存块的大小。这个函数常用于动态内存管理,特别是在需要扩展或缩小已经分配的内存时。
realloc开辟内存的三种情况:
- 如果当前内存段后面有足够的空间来满足新的大小要求,
realloc
会尝试扩展这段内存空间,并返回原指针。- 如果当前内存段后面的空闲字节不够,
realloc
会在堆中查找一个能够满足新大小要求的内存块,将原数据复制到新的位置,并释放原来的内存块,然后返回新内存块的首地址。- 如果重新分配失败(例如,因为内存不足),
realloc
会返回NULL
,但此时原始内存块仍然保持有效。注意事项:
- 返回值处理:使用
realloc
时,应始终检查其返回值。如果返回NULL
,表示重新分配失败,此时原始内存块仍然有效,需要适当处理(如释放原始内存块)。- 指针更新:如果
realloc
成功并返回一个新的地址(不同于原始地址),则需要更新指向该内存块的指针,以避免内存泄漏或野指针问题。- 内存释放:当内存不再使用时,应使用
free()
函数释放内存块。如果realloc
失败且原始内存块仍需保留,则不应调用free()
释放它。- 数据保留:
realloc
会保留原始内存块中的数据,并将其复制到新的内存块(如果分配了新的内存块)。但是,如果新大小小于原大小,则超出的数据可能会丢失。
- //扩容操作
- int expand(vector *v) {
- if (v == NULL) return 0;
- printf("expand v from %d to %d\n", v->size, 2 * v->size);
- int *p = (int *)realloc(v->data, sizeof(int) * 2 * v->size);
- if (p == NULL) return 0;
- v->data = p;
- v->size *= 2;
- return 1;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
-
- typedef struct vector {
- int size, count;
- int *data;
- //总大小,元素数量
- //指针指向连续的存储区
- } vector;
- //初始化
- vector *getNewVector(int n) {
- vector *p = (vector *)malloc(sizeof(vector));
- p->size = n;//存储上限
- p->count = 0;
- p->data = (int *)malloc(sizeof(int) * n);
- return p;
- }
-
- int expand(vector *v) {
- if (v == NULL) return 0;
- printf("expand v from %d to %d\n", v->size, 2 * v->size);
- int *p = (int *)realloc(v->data, sizeof(int) * 2 * v->size);
- if (p == NULL) return 0;
- v->data = p;
- v->size *= 2;
- return 1;
- }
- //插入操作
- int insert(vector *v, int pos, int val) {
- if (pos < 0 || pos > v->count) return 0;
- if (v->size == v->count && !expand(v)) return 0;
- for (int i = v->count - 1; i >= pos; i--) {
- v->data[i + 1] = v->data[i];
- }
- v->data[pos] = val;
- v->count += 1;
- return 1;
- }
- //销毁操作
- int erase(vector *v, int pos) {
- if (pos < 0 || pos >= v->count) return 0;
- for (int i = pos + 1; i < v->count; i++) {
- v->data[i - 1] = v->data[i];
- }
- v->count -= 1;
- return 1;
- }
-
- void output_vector(vector *v) {
- int len = 0;
- for (int i = 0; i < v->size; i++) {
- len += printf("%3d", i);
- }
- printf("\n");
- for (int i = 0; i < len; i++) printf("-");
- printf("\n");
- for (int i = 0; i < v->count; i++) {
- printf("%3d", v->data[i]);
- }
- printf("\n");
- printf("\n\n");
- return ;
- }
- //删除操作
- void clear(vector *v) {
- if (v == NULL) return ;
- free(v->data);
- free(v);
- return ;
- }
-
- int main() {
- srand(time(0));
- #define MAX_OP 20
- vector *v = getNewVector(2);
- for (int i = 0; i < MAX_OP; i++) {
- int op = rand() % 4, pos, val, ret;
- switch (op) {
- case 0:
- case 1:
- case 2:
- pos = rand() % (v->count + 2);//为了可以看到插入到非法位置时程序的反应
- val = rand() % 100;
- ret = insert(v, pos, val);
- printf("insert %d at %d to vector = %d\n",
- val, pos, ret);
- break;
- case 3:
- pos = rand() % (v->count + 2);
- ret = erase(v, pos);
- printf("erase item at %d in vector = %d\n",
- pos, ret);
- break;
- }
- output_vector(v);
- }
- clear(v);
- return 0;
- }

链表是一种物理存储结构上非 连续、非顺序的存储结构,但逻辑上是顺序的。链表由一系列结点(链表中每一个元素称为结点)组成,每个结点包含两个部分:数据域和指针域。数据域用来存储数据,而指针域则用来指向下一个结点。
- 单链表:每个节点只包含一个指针,指向下一个节点。
- 双链表:每个节点包含两个指针,一个指向前一个节点,另一个指向下一个节点。
- 循环链表:链表的最后一个节点的指针域指向第一个节点,形成一个环。
- 物理存储非连续性:链表中的元素可以存储在内存中的任意位置,通过指针链接。
- 插入和删除操作高效:链表的插入和删除操作只需要修改指针指向,不需要移动大量元素,时间复杂度为O(1)。
- 不支持随机访问:链表的访问需要从头节点开始依次遍历到目标节点,访问效率较低。
- 空间利用:链表中的每个节点都需要额外的空间来存储指针,但链表可以动态增长,不需要预分配固定大小的空间。
- typedef struct Lnode
- {
- int data;
- struct Lnode *next;
- } Lnode, *Linklist;
-
- void InitList (Linklist *L) //二级指针的目的是地址传递,因为该函数没有返回值,用地址传递带回头节点地址。
- {
- Linklist p;
- p = (Linklist)malloc(sizeof(Lnode));
- if(p == NULL)
- cout << "申请内存空间失败。" << endl;
- p->next=NULL;
- *L = p;
- flag++;
- }//初始化一个空的链表

接下来主要介绍几个链表的基础关键操作:
- void Creatlist(Linklist L,int n)
- {
- int i;
- Lnode *p,*pt;
- pt=L;
- for(i=1;i<=n;i++)
- {
- p=(Linklist)malloc(sizeof(Lnode));
- if(p==NULL)
- cout << "申请内存空间失败。" << endl;
- cout << "请输入链表中元素:" << endl;
- cin >> p->data;
- p->next=pt->next;
- pt->next=p;
- pt=p;
- }
- //flag++;
- }//创建链表

- int Getelem(Linklist L, int i)
- {
- Lnode *p;
- int e;
- int j;
- p=L->next;j=1;
- while (p && j<i)
- {
- p=p->next;
- ++j;
- }
- if(!p || j>i)
- cout << "该位序不存在。" << endl;
- else
- {
- e=p->data;
- cout << "第" << i << "个元素为:" << e << endl;
- }
- return OK;
- }//用e返回L中第i个数据元素的值.

- int Listinsert(Linklist L,int i,int e)
- {
- int j;
- Lnode *p,*s;
- p=L; j=0;
- while(p && j<i-1)
- {
- p=p->next;
- ++j;
- }
- if(!p || j>i-1)
- return ERROR;
- s=(Linklist)malloc(sizeof(Lnode));
- if(s==NULL)
- cout << "申请内存空间失败。" << endl;
- s->data=e;
- s->next=p->next;
- p->next=s;
- cout << "插入的元素是:" << e << endl;
- return OK;
- }//在第i个位置插入元素e

- int DestroyList(Linklist L)
- {
- Lnode *p;
- p=NULL;
- if(L && flag!=0)
- {
- while(L)
- {
- p=L;
- L=L->next;
- free(p);
- }
- cout << "链表已销毁。" << endl;
- }
- else
- cout << "链表不存在。" << endl;
- return OK;
- flag++;
- }//销毁链表

这边列举一个表格方便大家对着俩个功能类似的数据结构的特点有更加充分的理解:
特点 | 顺序表 | 链表 |
---|---|---|
存储方式 | 连续存储 | 非连续存储 |
随机访问 | 支持(O(1)) | 不支持(需要遍历) |
插入/删除操作 | 效率低(O(n)) | 效率高(O(1)) |
空间利用 | 较高(无额外指针空间) | 较低(每个节点需额外空间存储指针) |
灵活性 | 较低(需要预分配空间) | 较高(可以动态增长) |
今天数据结构第二讲:顺序表与链表到这里就结束了,未来俩天会有链表相关延展问题实现的博客出炉,不要忘了点一手关注再走哦,希望喜欢的朋友多多支持我哦~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。