顺序表
- 线性表是指逻辑结构,在逻辑上呈线性关系
- 顺序表是指存储结构,在内存上是连续存储的
- 优点:简单,方便查找
- 缺点:需要连续内存空间,插入和删需要移动大量空间,特别是在开头
函数的声明:
#ifndef __SQLIST_H__
#define __SQLIST_H__
#define SIZE 100
typedef int data_t;
typedef struct{
data_t data[SIZE];
int len;
}sqlist_t;
//创建表
sqlist_t *sqlist_create(void);
//销毁表
void sqlist_destory(sqlist_t *head);
//判断是否为满表
int sqlist_is_full(sqlist_t *head);
//判断是否为空表
int sqlist_is_empty(sqlist_t *head);
//插入数据
int sqlist_insert(sqlist_t *head, int pos, data_t data);
//按pos删除data
int sqlist_pos_delete(sqlist_t *head, int pos);
//按data删除data
int sqlist_data_delete(sqlist_t *head, data_t data);
//按pos替换data
int sqlist_pos_update(sqlist_t *head, int pos, data_t new_data);
//按old-data替换new-data
int sqlist_old_data_update(sqlist_t *head, data_t old_data, data_t new_data);
//按pos查找data
data_t sqlist_pos_lookup(sqlist_t *head, int pos);
//按data查找pos
int sqlist_data_lookup(sqlist_t *head, data_t data);
//显示数据
void sqlist_display(sqlist_t *head);
#endif
函数的定义:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sqlist.h"
//建立空表
sqlist_t *sqlist_create(void)
{
sqlist_t *head = (sqlist_t *)malloc(sizeof(sqlist_t));
if(NULL == head)
{
printf("malloc loser!!!\n");
return NULL;
}
memset(head,0,sizeof(sqlist_t));
head->len = -1;
return head;
}
//销毁表
void sqlist_destory(sqlist_t *head)
{
free(head);
}
//判断是否为满表
int sqlist_is_full(sqlist_t *head)
{
return (SIZE-1 == head->len);
}
//判断是否为空表
int sqlist_is_empty(sqlist_t *head)
{
return (-1 == head->len);
}
//插入数据
int sqlist_insert(sqlist_t *head, int pos, data_t data)
{
if(1 == sqlist_is_full(head))
{
printf("sqlist full!!! insert error!!\n");
return -1;
}
if(pos <0 || pos > (head->len+1))
{
printf("pos than!!! insert error\n");
return -1;
}
for(int i=head->len+1;i>pos;i--)
head->data[i] = head->data[i-1];
head->data[pos] = data;
head->len++;
return 0;
}
//按pos删除data
int sqlist_pos_delete(sqlist_t *head, int pos)
{
if(sqlist_is_empty(head))
{
printf("sqlist is empty!!! delete error!!!\n");
return -1;
}
if(-1 == sqlist_pos_lookup(head,pos))
{
printf("pos no!!! delete error!!!\n");
return -1;
}
for(int i=pos;i<head->len;i++)
head->data[i] = head->data[i+1];
head->len--;
return 0;
}
//按data删除data
int sqlist_data_delete(sqlist_t *head, data_t data)
{
if(-1 == sqlist_data_lookup(head,data))
{
printf("data no!!! delete error!!!\n");
return -1;
}
/*
for(int i=sqlist_data_lookup(head,data);i<head->len;i++)
head->data[i] = head->data[i+1];
head->len--;*/
sqlist_pos_delete(head,sqlist_data_lookup(head,data));
return 0;
}
//按pos替换data
int sqlist_pos_update(sqlist_t *head, int pos, data_t new_data)
{
if(sqlist_is_empty(head))
{
printf("sqlist is empty!!!update error!!!\n");
return -1;
}
if(pos<0 || pos > head->len)
{
printf("pos no!!! update error!!!\n");
return -1;
}
sqlist_old_data_update(head,sqlist_pos_lookup(head,pos),new_data);
return 0;
}
//按old-data替换new-data
int sqlist_old_data_update(sqlist_t *head, data_t old_data, data_t new_data)
{
if(-1 == sqlist_data_lookup(head,old_data))
{
printf("no old_data!!! update error!!!\n");
return -1;
}
else
{
head->data[sqlist_data_lookup(head,old_data)] = new_data;
return 0;
}
}
//按pos查找data
data_t sqlist_pos_lookup(sqlist_t *head, int pos)
{
if(sqlist_is_empty(head))
{
printf("sqlist is empty!!! lookup error!!!\n");
return -1;
}
if(pos <= head->len && pos >= 0)
return head->data[pos];
else
{
printf("pos no!!! lookup error!!!\n");
return -1;
}
}
//按data查找pos
int sqlist_data_lookup(sqlist_t *head, data_t data)
{
if(sqlist_is_empty(head))
{
printf("sqlist is empty!!! lookup error!!!\n");
return -1;
}
for(int i=0;i<(head->len+1);i++)
{
if(data == head->data[i])
return i;
}
printf("data no!!! lookup error!!!\n");
return -1;
}
//打印数据
void sqlist_display(sqlist_t *head)
{
for(int i=0;i<head->len+1;i++)
{
printf("%d ",head->data[i]);
}
puts("");
}
这样就可以通过运用mian.c来制作顺序表,可以插入,删除,添加,替换等功能
在linux也可以将其做成库,进行直接调用
静态库:
(gcc -c xxx.c -o xxx.o)(gcc -crs libxxx.a xxx.o )在使用时,编译主函数文件时,gcc main.c -L路径 -lxxx 才能运行成功
动态库:
(gcc -fPIC -c xxx.c -o xxx.o)
(gcc -shared -o libxxx.so xxx.o)
得到动态库 libxxx.so
再将动态库存放到/lib或者/usr/lib/中 sudo cp libxxx.so /lib