数据结构(15)静态链表
来源:互联网 发布:拍照姿势的软件 编辑:程序博客网 时间:2024/05/17 04:29
用数组描述的链表,即称为静态链表,它的表现形式即为结构体数组,结构体变量包括数据域data和指针域next。这种存储结构,仍需要预先分配一个较大的空间,但在作为线性表的插入和删除操作时不需移动元素,仅需修改指针,故仍具有链式存储结构的主要优点。而且在不具有指针类型的高级语言中,无法使用指针类型,也就无法用指针实现单链表结构,所以静态链表是个不错的选择。
静态链表的定义为:
l 顺序表数组中的元素由两个数据域组成:data和next
l Data域用于存储数据
l Next域用于存储下一个元素在数组中的下标
这里介绍静态链表的常用操作:
l 创建静态链表
l 销毁静态链表
l 清空静态链表
l 表元素插入
l 表元素删除
l 获取表中某个位置的元素
l 获取表长度
l 获取表的最大长度
代码总分为三个文件:
StaticList.h : 放置功能函数的声明,以及表和表结点的声明
StaticList.h : 放置功能函数的定义,以及表和表结点的定义
Main.c : 主函数,使用功能函数完成各种需求,不过一般用作测试
这里着重说下获取操作﹑插入操作和删除操作:
获取操作:
获取第pos个元素方法:
首先判断线性表﹑pos位置是否合法
由表头开始通过next指针移动pos次后,当前元素的next域即要获取元素在数组中的下标
插入操作:
如图
插入元素方法:
首先判断线性表﹑插入位置是否合法
在数组中从头开始查找空闲位置index(其实也就是判断每个数组元素的next是否为空闲状态)
由表头开始通过next域移动pos次后,当前元素的next域为要插入的位置
将新元素插入
静态链表长度加1
删除操作:
如图
删除元素方法:
判断表和删除位置是否合法
获取第pos个元素
将第pos个元素从链表中删除
表长度减1
OK! 上代码:
StaticList.h
- #ifndef _STATICLIST_H_
- #define _STATICLIST_H_
- typedef void StaticList;
- typedef void StaticListNode;
- StaticList* StaticList_Create(int capacity);
- void StaticList_Destroy(StaticList* list);
- void StaticList_CLear(StaticList* list);
- int StaticList_Length(StaticList* list);
- int StaticList_Capacity(StaticList* list);
- int StaticList_Insert(StaticList* list, StaticListNode* node, int pos);
- StaticListNode* StaticList_Get(StaticList* list, int pos);
- StaticListNode* StaticList_Delete(StaticList* list, int pos);
- #endif
StaticList.c
- #include <stdio.h>
- #include <malloc.h>
- #include "StaticList.h"
- #define AVAILBLE -1
- typedef struct _tag_StaticListNode
- {
- unsigned int data;
- int next;
- }TStaticListNode;
- typedef struct _tag_StaticList
- {
- int capacity;
- TStaticListNode header;
- TStaticListNode node[];
- }TStaticList;
- StaticList* StaticList_Create(int capacity)
- {
- TStaticList* ret = NULL;
- int i = 0;
- if(capacity >= 0)
- {
- ret = (TStaticList*)malloc(sizeof(TStaticList)+sizeof(TStaticListNode)*(capacity+1));
- }
- if(ret != NULL)
- {
- ret->capacity = capacity;
- ret->header.next = 0;
- ret->header.data = 0;
- for(i=1; i<=capacity; i++)
- {
- ret->node[i].next = AVAILBLE;
- }
- }
- return ret;
- }
- void StaticList_Destroy(StaticList* list)
- {
- free(list);
- }
- void StaticList_Clear(StaticList* list)
- {
- TStaticList* sList = (TStaticList*)list;
- int i = 0;
- if(sList!=NULL)
- {
- sList->header.next = 0;
- sList->header.data = 0;
- for(i=1; i<=sList->capacity; i++)
- {
- sList->node[i].next = AVAILBLE;
- }
- }
- }
- int StaticList_Length(StaticList* list)
- {
- TStaticList* sList = (TStaticList*)list;
- int ret = -1;
- if(sList!=NULL)
- {
- ret = sList->header.data;
- }
- return ret;
- }
- int StaticList_Capacity(StaticList* list)
- {
- TStaticList* sList = (TStaticList*)list;
- int ret = -1;
- if(sList!=NULL)
- {
- ret = sList->capacity;
- }
- return ret;
- }
- int StaticList_Insert(StaticList* list, StaticListNode* node, int pos)
- {
- TStaticList* sList = (TStaticList*)list;
- int ret = (sList!=NULL);
- int index = 0;
- int i = 0;
- int current = 0;
- ret = ret && (sList->header.data+1 <= sList->capacity);
- ret = ret && (pos>=0) && (node!=NULL);
- if(ret)
- {
- for(i=1; i<sList->capacity; i++)
- {
- if(sList->node[i].next == AVAILBLE)
- {
- index = i;
- break;
- }
- }
- sList->node[index].data = (unsigned int)node;
- sList->node[0] = sList->header;
- for(i=0; (i<pos)&&(sList->node[current].next!=0); i++)
- {
- current = sList->node[current].next;
- }
- sList->node[index].next = sList->node[current].next;
- sList->node[current].next = index;
- sList->node[0].data++;
- sList->header = sList->node[0];
- }
- return ret;
- }
- StaticListNode* StaticList_Get(StaticList* list, int pos)
- {
- TStaticList* sList = (TStaticList*)list;
- TStaticListNode* ret = NULL;
- int i = 0;
- int current = 0;
- int object = 0;
- if((sList!=NULL)&&(pos>=0)&&(pos<sList->header.data))
- {
- sList->node[0] = sList->header;
- for(i=0; i<pos; i++)
- {
- current = sList->node[current].next;
- }
- object = sList->node[current].next;
- ret = (StaticListNode*)(sList->node[object].data);
- }
- return ret;
- }
- StaticListNode* StaticList_Delete(StaticList* list, int pos)
- {
- TStaticList* sList = (TStaticList*)list;
- StaticListNode* ret = NULL;
- int current = 0;
- int object = 0;
- int i = 0;
- if((sList!=NULL)&&(0<=pos)&&(pos<sList->header.data))
- {
- sList->node[0] = sList->header;
- for(i=0; i<pos; i++)
- {
- current = sList->node[current].next;
- }
- object = sList->node[current].next;
- sList->node[current].next = sList->node[object].next;
- sList->node[0].data--;
- sList->header = sList->node[0];
- sList->node[object].next = AVAILBLE;
- ret = (StaticListNode*)(sList->node[object].data);
- }
- return ret;
- }
Main.c
- #include <stdio.h>
- #include <stdlib.h>
- #include "StaticList.h"
- int main(void)
- {
- StaticList* list = StaticList_Create(10);
- int index = 0;
- int i=0, j=1, k=2, x=3, y=4, z=5;
- StaticList_Insert(list, &i, 0);
- StaticList_Insert(list, &j, 0);
- StaticList_Insert(list, &k, 0);
- for(index=0; index<StaticList_Length(list); index++)
- {
- int* p = (int*)StaticList_Get(list, index);
- printf("%d\n", *p);
- }
- printf("\n");
- while(StaticList_Length(list) > 0)
- {
- int* p = (int*)StaticList_Delete(list, StaticList_Length(list)-1);
- printf("%d\n", *p);
- }
- StaticList_Insert(list, &x, 0);
- StaticList_Insert(list, &y, 0);
- StaticList_Insert(list, &z, 0);
- printf("Capacity:%d Length:%d\n", StaticList_Capacity(list), StaticList_Length(list));
- printf("\n");
- for(index=0; index<StaticList_Length(list); index++)
- {
- int* p = (int*)StaticList_Get(list, index);
- printf("%d\n", *p);
- }
- StaticList_Destroy(list);
- return 0;
- }
- 数据结构(15)静态链表
- 【数据结构】静态链表
- 数据结构:静态链表
- 数据结构-静态链表
- 【数据结构】静态链表
- 数据结构:静态链表
- 数据结构:静态链表
- 数据结构--静态链表
- 数据结构-静态链表
- 数据结构 链表(静态链表)
- 数据结构-静态链表(学习笔记)
- 数据结构实验(二) 静态链表
- 数据结构实现静态链表
- 【数据结构】静态链表_StaticLinkList
- 数据结构之静态链表
- 数据结构之静态链表
- 数据结构之静态链表
- 数据结构之静态链表
- 基于 Android NDK 的学习之旅-----HelloWorld
- 535 Error: authentication failed
- poj2362 Square
- MySQL数据库与登录注册
- java基础第十讲——数据库、单例设计模式、服务器、项目(数据库—服务器—网络)
- 数据结构(15)静态链表
- python发送各类邮件的主要方法 smpt
- 1374 - Power Calculus (迭代加深+剪枝)
- 选择了tableView的某一行后以modal的方式跳转的到子控制器,以及子控制器导航栏左上角"关闭"按钮
- CheckBox的一些用法(内容包含动态加载布局)
- hdu 2023 求平均成绩
- CSS笔记
- hdu2680 Choose the best route 最短路,超级起点(多源Dijkstra算法,)
- nyoj 75 日期计算