当前位置:网站首页>顺序表(C语言实现)
顺序表(C语言实现)
2022-07-03 12:58: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;//里面装的是地址
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
测试代码
#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)
{
}
}
边栏推荐
- Red Hat Satellite 6:更好地管理服务器和云
- TensorBoard可视化处理案例简析
- [Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [sqlserver2012 comprehensive exercise]
- Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
- Asp.Net Core1.1版本没了project.json,这样来生成跨平台包
- [Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter III exercises]
- When we are doing flow batch integration, what are we doing?
- R语言使用data函数获取当前R环境可用的示例数据集:获取datasets包中的所有示例数据集、获取所有包的数据集、获取特定包的数据集
- Brief analysis of tensorboard visual processing cases
- File uploading and email sending
猜你喜欢

今日睡眠质量记录77分

已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)

Road construction issues

Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)

MySQL functions and related cases and exercises

使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2

Servlet

106. How to improve the readability of SAP ui5 application routing URL

When updating mysql, the condition is a query

刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
随机推荐
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter III exercises]
2022-02-14 analysis of the startup and request processing process of the incluxdb cluster Coordinator
Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL
When updating mysql, the condition is a query
Road construction issues
Sword finger offer 14- ii Cut rope II
Useful blog links
正则表达式
Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
2022-02-14 incluxdb cluster write data writetoshard parsing
PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
json序列化时案例总结
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
Kivy教程之 如何通过字符串方式载入kv文件设计界面(教程含源码)
Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial
物联网毕设 --(STM32f407连接云平台检测数据)
In the promotion season, how to reduce the preparation time of defense materials by 50% and adjust the mentality (personal experience summary)
Flink SQL knows why (XI): weight removal is not only count distinct, but also powerful duplication
User and group command exercises