当前位置:网站首页>静态链表(数组的下标代替指针)
静态链表(数组的下标代替指针)
2022-07-03 12:59:00 【fitpolo】
static_list.h
#ifndef _STATICLIST_H_
#define _STATICLIST_H_
#include "stdio.h"
#include "stdlib.h"
#define AVAILABLE -1
typedef struct _tag_static_list_node static_list_node;
typedef struct _tag_static_list static_list;
struct _tag_static_list_node
{
void *pdata;
int next;
};
struct _tag_static_list
{
int capacity;
int len;
static_list_node node[];
};
static_list* static_list_create(int capacity);
void static_list_destroy(static_list* list);
void static_list_clear(static_list* list);
int static_list_length(static_list* list);
int static_list_capacity(static_list* list);
int static_list_insert(static_list* list, void *pdata, int pos);
void *static_list_get(static_list *list,int pos);
void *static_list_delete(static_list *list, int pos);
void static_list_printf(static_list *list);
#endif
static_list.c
#include "static_list.h"
static_list* static_list_create(int capacity)
{
int i;
static_list *result=0;
result = malloc(sizeof(static_list)+sizeof(static_list_node)*(capacity+1));
if (result==0)
{
printf("static_list_create error -1\n");
return 0;
}
result->capacity = capacity;
result->len = 0;
result->node[0].next = 0;
for (i=1; i<(capacity+1); i++)
{
result->node[i].next = AVAILABLE;
}
return result;
}
void static_destroy(static_list* list)
{
free(list);
}
void static_list_clear(static_list* list)
{
int i;
list->len=0;
list->node[0].next=0;
for (i=1; i<(list->capacity+1); i++)
{
list->node[i].next = AVAILABLE;
}
}
int static_list_length(static_list* list)
{
return list->len;
}
int static_list_capacity(static_list* list)
{
return list->capacity;
}
int static_list_insert(static_list* list, void *pdata, int pos)
{
int i,j;
int current = 0;
if (list->capacity <= pos)
{
return -1;
}
//从数字找到一个可以用的
for (i=1; i<(list->capacity+1); i++)
{
if (list->node[i].next == AVAILABLE)
{
printf("available index:%d\n",i);
break;
}
}
if ( (list->capacity+1) == i)
{
printf("out of memory\n");
return -2;
}
list->node[i].pdata = pdata;
for (j=0; j<pos&&list->node[current].next!=0; j++)
{
current = list->node[current].next;
}
list->node[i].next = list->node[current].next;
printf("%d-i:%d-current:%d\n",list->node[current].next,i,current);
list->node[current].next = i;
list->len++;
return 0;
}
void *static_list_get(static_list *list,int pos)
{
void *result = 0;
int current = 0;
int i;
current = list->node[0].next;
if (current!=0 && pos<list->len)
{
for (i=0; i<pos; i++)
{
current = list->node[current].next;
}
result = list->node[current].pdata;
}
return result;
}
void *static_list_delete(static_list *list, int pos)
{
int pre_current = 0;
int current = 0;
int tmp;
int i = 0;
void *result = 0;
if (list->len>0 && pos>=0 && pos<list->len)
{
pre_current = 0;
current = list->node[0].next;
if (current)
{
for (i=0; i<pos; i++)
{
if (list->node[current].next==0)
{
break;
}
pre_current = current;
current = list->node[current].next;
}
tmp = list->node[current].next;
list->node[pre_current].next = tmp;
result = list->node[current].pdata;
list->node[current].next = AVAILABLE;
list->node[current].pdata = 0;
list->len--;
}
}else
{
printf("static_list_delete error :%d-%d\n",list->len,pos);
}
return result;
}
void static_list_printf(static_list *list)
{
int current = 0;
int i = 0;
void *pdata;
current = list->node[0].next;
printf("static_list_printf-");
for (i=0; i<list->len; i++)
{
pdata = list->node[current].pdata;
printf("[%d,0x%02x,value:%d]-",i,current,*((unsigned int *)pdata));
current = list->node[current].next;
}
printf("\n");
}
测试代码
#include "static_list.h"
#include "stdio.h"
#include "stdint.h"
int main(void)
{
static_list *head=0;
uint8_t i;
int tmp[11];
for (i=0; i<11; i++)
{
tmp[i] = i+1;
}
printf("hello \r\n");
head = static_list_create(10);
#if 1
for (i=0; i<11; i++)
{
static_list_insert(head,&tmp[i],0);
}
#else
for (i=0; i<9; i++)
{
static_list_insert(head,&tmp[i],0);
}
static_list_insert(head,&tmp[9],1);
#endif
static_list_printf(head);
// for (i=9; i>0; i--)
// {
// static_list_delete(head,i);
// static_list_printf(head);
// }
// static_list_delete(head,0);
// static_list_printf(head);
// static_list_delete(head,0);
// static_list_printf(head);
for (i=0; i<12; i++)
{
static_list_delete(head,0);
static_list_printf(head);
}
while(1)
{
}
}
边栏推荐
- 使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2
- 物联网毕设 --(STM32f407连接云平台检测数据)
- Logseq 评测:优点、缺点、评价、学习教程
- The R language GT package and gtextras package gracefully and beautifully display tabular data: nflreadr package and gt of gtextras package_ plt_ The winloss function visualizes the win / loss values
- Red hat satellite 6: better management of servers and clouds
- PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
- Libuv库 - 设计概述(中文版)
- 2022-02-13 plan for next week
- KEIL5出现中文字体乱码的解决方法
- Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
猜你喜欢
Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
8皇后问题
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [sqlserver2012 comprehensive exercise]
Multi table query of MySQL - multi table relationship and related exercises
elk笔记24--用gohangout替代logstash消费日志
Mysql database basic operation - regular expression
stm32和电机开发(从mcu到架构设计)
[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈..."/>
开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...
Detailed explanation of multithreading
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
随机推荐
Useful blog links
阿南的疑惑
Open PHP error prompt under Ubuntu 14.04
Ubuntu 14.04 下开启PHP错误提示
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter 7 exercises]
Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
The network card fails to start after the cold migration of the server hard disk
MySQL
Luogup3694 Bangbang chorus standing in line
json序列化时案例总结
Sword finger offer 11 Rotate the minimum number of the array
Detailed explanation of multithreading
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
File uploading and email sending
Resource Cost Optimization Practice of R & D team
Tencent cloud tdsql database delivery and operation and maintenance Junior Engineer - some questions of Tencent cloud cloudlite certification (TCA) examination
Spark practice 1: build spark operation environment in single node local mode
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
Setting up remote links to MySQL on Linux
My creation anniversary: the fifth anniversary