当前位置:网站首页>Sequence table (implemented in C language)
Sequence table (implemented in C language)
2022-07-03 13:33:00 【fitpolo】
seq_list.c
#include "seq_list.h"
seq_list* seq_list_create(int capacity)
{
seq_list *result=0;
result = malloc( sizeof(seq_list) );
if (result ==0)
{
printf("malloc error\r\n");
return 0;
}
result->capacity = capacity;
result->len = 0;
result->node = malloc(sizeof(seq_list_node) * capacity);
if (result->node == 0)
{
free(result);
printf("malloc error\r\n");
return 0;
}
return result;
}
void seq_list_destroy(seq_list *list)
{
free(list);
}
void seq_list_clear(seq_list *list)
{
list->len = 0;
}
int seq_list_length(seq_list *list)
{
return list->len;
}
int seq_list_capacity(seq_list *list)
{
return list->capacity;
}
int seq_list_insert(seq_list *list,seq_list_node *node,int pos)
{
int i;
if (list->capacity <(list->len+1))
{
printf("seq_list_insert -1,pos:%d,node:0x%x\n",pos,(unsigned int)node);
return -1;
}
if (list->len <= pos)
{
pos = list->len;
}
for (i=list->len; i>pos; i--)
{
list->node[i] = list->node[i-1];
}
list->node[pos] = (seq_list_node)node;// It contains the address
list->len++;
return i;
}
seq_list_node *seq_list_get(seq_list *list,int pos)
{
seq_list_node *result=0;
if (pos >= list->capacity)
return 0;
if (pos >= list->len)
return 0;
result = (seq_list_node*)list->node[pos];
return result;
}
seq_list_node *seq_list_delete(seq_list *list,int pos)
{
int i=0;
if (pos >= list->capacity)
return 0;
if (pos >= list->len)
return 0;
if (list->len == 0)
return 0;
for (i=pos; i<(list->len-1); i++)// 4 0 1 2 3
{
list->node[i] = list->node[i+1];
}
list->len--;
return 0;
}
void seq_list_printf(seq_list *list)
{
int i;
seq_list_node tmp;
seq_list_node *pdata;
printf("capacity:%d-len:%d-\n",list->capacity,list->len);
for (i=0; i<list->len; i++)
{
tmp = list->node[i];
pdata = (seq_list_node*)tmp;
printf("0x%x-%d\n",tmp,pdata[0]);
}
printf("\n");
}
seq_list.h
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
#include "stdio.h"
#include "stdlib.h"
typedef unsigned int seq_list_node;
typedef struct _tag_seq_list seq_list;
struct _tag_seq_list
{
int capacity;
int len;
seq_list_node *node;
};
seq_list* seq_list_create(int capacity);
void seq_list_destroy(seq_list *list);
void seq_list_clear(seq_list *list);
int seq_list_length(seq_list *list);
int seq_list_capacity(seq_list *list);
int seq_list_insert(seq_list *list,seq_list_node *node,int pos);
seq_list_node *seq_list_get(seq_list *list,int pos);
seq_list_node *seq_list_delete(seq_list *list,int pos);
void seq_list_printf(seq_list *list);
#endif
Test code
#include "seq_list.h"
#include "stdio.h"
#include "stdint.h"
int main(void)
{
uint8_t i;
int tmp[11];
seq_list *head;
printf("hello \r\n");
head = seq_list_create(10);
printf("len:%d,capacity:%d\n",head->len,head->capacity);
for (i=0; i<11; i++)
{
tmp[i] = i+1;
}
seq_list_insert(head,(seq_list_node*)&tmp[0],0);
seq_list_insert(head,(seq_list_node*)&tmp[1],0);
seq_list_insert(head,(seq_list_node*)&tmp[2],0);
seq_list_insert(head,(seq_list_node*)&tmp[3],0);
seq_list_insert(head,(seq_list_node*)&tmp[4],0);
seq_list_insert(head,(seq_list_node*)&tmp[5],0);
seq_list_insert(head,(seq_list_node*)&tmp[6],0);
seq_list_insert(head,(seq_list_node*)&tmp[7],0);
seq_list_insert(head,(seq_list_node*)&tmp[8],0);
seq_list_insert(head,(seq_list_node*)&tmp[9],0);
//
seq_list_printf(head);
printf("seq list capacity:%d-len:%d\n",seq_list_capacity(head),seq_list_length(head));
printf("delete pos:%d\n",8);
seq_list_delete(head,8);
seq_list_printf(head);
printf("delete pos:%d\n",0);
seq_list_delete(head,0);
seq_list_printf(head);
printf("seq list capacity:%d-len:%d\n",seq_list_capacity(head),seq_list_length(head));
while(1)
{
}
}
边栏推荐
- Internet of things completion -- (stm32f407 connects to cloud platform detection data)
- Spark实战1:单节点本地模式搭建Spark运行环境
- File uploading and email sending
- 服务器硬盘冷迁移后网卡无法启动问题
- Today's sleep quality record 77 points
- Resource Cost Optimization Practice of R & D team
- pytorch 载入历史模型时更换gpu卡号,map_location设置
- Anan's doubts
- Smbms project
- Flink SQL knows why (12): is it difficult to join streams? (top)
猜你喜欢

显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti

When updating mysql, the condition is a query

Smbms project

Detailed explanation of multithreading

Flink SQL knows why (XI): weight removal is not only count distinct, but also powerful duplication

【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】

MySQL functions and related cases and exercises

Flink SQL knows why (XIV): the way to optimize the performance of dimension table join (Part 1) with source code
![[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion](/img/3b/28327bbf5eb19254f03500a41e2adb.jpg)
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion

Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
随机推荐
Universal dividend source code, supports the dividend of any B on the BSC
rxjs Observable filter Operator 的实现原理介绍
mysql中的字段问题
常见的几种最优化方法Matlab原理和深度分析
Useful blog links
ThreadPoolExecutor realizes multi-threaded concurrency and obtains the return value (elegant and concise way)
Flink SQL knows why (12): is it difficult to join streams? (top)
MySQL_ JDBC
编程内功之编程语言众多的原因
双向链表(我们只需要关注插入和删除函数)
Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
这本数学书AI圈都在转,资深ML研究员历时7年之作,免费电子版可看
开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...
Students who do not understand the code can also send their own token, which is easy to learn BSC
Anan's doubts
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
logback日志的整理
Reptile
Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu
JS 将伪数组转换成数组