当前位置:网站首页>顺序表(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)
{
}
}
边栏推荐
- The difference between stratifiedkfold (classification) and kfold (regression)
- MySQL
- 正则表达式
- Can newly graduated European college students get an offer from a major Internet company in the United States?
- Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
- stm32和电机开发(从mcu到架构设计)
- Task6: using transformer for emotion analysis
- The principle of human voice transformer
- The reasons why there are so many programming languages in programming internal skills
- 【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
猜你喜欢
Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL
[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈..."/>
开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...
[colab] [7 methods of using external data]
Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
Elk note 24 -- replace logstash consumption log with gohangout
February 14, 2022, incluxdb survey - mind map
已解决(机器学习中查看数据信息报错)AttributeError: target_names
[sort] bucket sort
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
Setting up remote links to MySQL on Linux
随机推荐
Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL
Tencent cloud tdsql database delivery and operation and maintenance Junior Engineer - some questions of Tencent cloud cloudlite certification (TCA) examination
Kivy tutorial how to load kV file design interface by string (tutorial includes source code)
SwiftUI 开发经验之作为一名程序员需要掌握的五个最有力的原则
The principle of human voice transformer
R language uses the data function to obtain the sample datasets available in the current R environment: obtain all the sample datasets in the datasets package, obtain the datasets of all packages, and
Cadre de logback
2022-02-11 heap sorting and recursion
CVPR 2022 | 美团技术团队精选6篇优秀论文解读
实现CNN图像的识别和训练通过tensorflow框架对cifar10数据集等方法的处理
File uploading and email sending
Understanding of CPU buffer line
[colab] [7 methods of using external data]
Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
Libuv库 - 设计概述(中文版)
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
Logseq 评测:优点、缺点、评价、学习教程
人身变声器的原理
阿南的疑惑