sqlist.h
#include <stdio.h>
#define N 128
typedef int data_type;
typedef struct {
data_type data[N];
int last;
}sqlist;
// 创建表
sqlist * list_create();
// 插入表元素
int list_insert(sqlist * L, data_type value, int pos);
int list_head_insert(sqlist * L, data_type value);
int list_append(sqlist * L, data_type value);
// 修改表元素
int list_modify(sqlist * L, data_type value, int pos);
// 删除表元素
int list_delete(sqlist * L, int pos);
// 查找表元素位置
int list_find_elem(sqlist * L, data_type value);
// 打印表
int list_show(sqlist * L);
// 清空表
int list_clear(sqlist * L);
// 销毁表
int list_destory(sqlist * L);
// 表判空
int list_is_empty(sqlist * L);
// 表长度
int list_length(sqlist * L);
// 表合并,且表2与表1有重复的不合并
int list_merge(sqlist * L1, sqlist * L2);
// 去重
int list_remove_duplicate(sqlist * L);
// 表排序
int list_sort(sqlist * L);
sqlist.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlist.h"
sqlist * list_create(){
sqlist * L; // 指向随机的一个地址,需要申请一块内存
L = (sqlist *)malloc(sizeof(sqlist));
if(L == NULL){
printf("%s\n", "malloc failed");
return L;
}
// 该内存空间需要初始化下
memset(L, 0, sizeof(sqlist));
L->last = -1;
return L;
}
// 指定索引插入
int list_insert(sqlist * L, data_type value, int pos){
int i;
if (L==NULL){
printf("%s\n", "list not create");
return -1;
}
// list is full
if (L->last == N - 1){
printf("%s\n","list_insert failed, list is full");
return -1;
}
// pos范围应该是[0, last+1],其中注意last=-1的极限值
if (pos<0 || pos > L->last+1){
printf("%s\n","pos is invalid");
return -1;
}
// 元素移动,先将下标最大的移动到后边
for (i = L->last; i>= pos; i--){
L->data[i+1] = L->data[i];
}
L->data[pos] = value;
L->last++;
return 0;
}
// 头插
int list_head_insert(sqlist * L, data_type value){
int i;
if (L==NULL){
printf("%s\n", "list not create");
return -1;
}
// list is full
if (L->last == N - 1){
printf("%s\n","list_head_insert failed, list is full");
return -1;
}
for (i = L->last; i >=0; i--){
L->data[i+1] = L->data[i];
}
L->data[0] = value;
L->last++;
return 0;
}
// 尾部追加
int list_append(sqlist * L, data_type value){
if (L==NULL){
printf("%s\n", "list not create");
return -1;
}
// list is full
if (L->last == N - 1){
printf("%s\n","list_append failed, list is full");
return -1;
}
L->last++;
L->data[L->last] = value;
return 0;
}
// 修改指定索引的值
int list_modify(sqlist * L, data_type value, int pos){
if(L==NULL){
printf("%s\n", "list not create");
return -1;
}
if(L->last == -1){
printf("%s\n", "list_modify failed, list is empty");
return -1;
}
// pos范围应该是[0, last+1]
if (pos<0 || pos > L->last){
printf("%s\n","pos is invalid");
return -1;
}
L->data[pos] = value;
return 0;
}
// 删除指定索引的元素
int list_delete(sqlist * L, int pos){
if(L==NULL){
printf("%s\n", "list not create");
return -1;
}
if(L->last == -1){
printf("%s\n", "list_delete failed, list is empty");
return -1;
}
// pos范围应该是[0, last]
if (pos<0 || pos > L->last){
printf("%s\n","pos is invalid");
return -1;
}
int i;
// 前移
for (i = pos; i <= L->last; i++){
L->data[i] = L->data[i+1];
}
L->last--;
return 0;
}
int list_find_elem(sqlist * L, data_type value){
if (L==NULL){
printf("%s\n", "list not create");
return -1;
}
int i;
for (i = 0; i <= L->last; i++){
if(L->data[i]==value){
return i;
}
}
return -1;
}
int list_show(sqlist * L){
int i;
if (L==NULL){
printf("%s\n", "list not create");
return -1;
}
for (i = 0; i <= L->last; ++i){
printf("%d ", L->data[i]);
}
puts("");
return 0;
}
// 清空表
int list_clear(sqlist * L){
if(L==NULL){
printf("%s\n", "list not create");
return -1;
}
if(L->last == -1){
return 0;
}
memset(L, 0, sizeof(sqlist));
L->last = -1;
return 0;
}
// 有问题
int list_destory(sqlist * L){
if(L==NULL){
printf("%s\n", "list not create");
return -1;
}
free(L);
L = NULL;
return 0;
}
/*
list_is_empty: is list empty?
para L: list
ret: 1-empty 0-not empty
*/
int list_is_empty(sqlist * L){
if(L==NULL){
printf("%s\n", "list not create");
return -1;
}
if(L->last == -1){
return 1;
}
return 0;
}
int list_length(sqlist * L){
if(L==NULL){
printf("%s\n", "list not create");
return -1;
}
return L->last+1;
}
int list_merge(sqlist * L1, sqlist * L2){
if(L1==NULL && L2==NULL){
printf("%s\n", "list not create");
return -1;
}
int i;
for (i = 0; i <= L2->last; i++){
int ret1 = list_find_elem(L1, L2->data[i]);
if(ret1 == -1){
int ret2 = list_append(L1, L2->data[i]);
if(ret2==-1){
return -1;
}
}
}
}
int list_remove_duplicate(sqlist * L){
if(L==NULL){
printf("%s\n", "list not create");
return -1;
}
int i , j;
for (i = 0; i <= L->last; i++){
for (j = i+1; j <= L->last; j++){
if (L->data[i] == L->data[j]){
list_delete(L, j);
j--;
}
}
}
return 0;
}
int list_sort(sqlist * L){
if (L==NULL){
return -1;
}
int i, j;
data_type t;
for (i = 0; i <= L->last; i++){
for (j = i+1; j <= L->last; j++){
if(L->data[j] < L->data[i]){
t = L->data[i];
L->data[i] = L->data[j];
L->data[j] = t;
}
}
}
return 0;
}
test.c
#include <stdio.h>
#include "sqlist.h"
int main(int argc, char const *argv[])
{
sqlist * L2;
L2 = list_create();
list_insert(L2, 60, 0);
list_remove_duplicate(L2);
list_show(L2);
return 0;
}