#include <stdio.h>
#define N 128
typedef int data_type;
typedef struct {
data_type data[N];
int last;
}sqlist;
sqlist * list_create();
int list_show(sqlist * L);
int list_clear(sqlist * L);
int list_destory(sqlist * L);
int list_empty(sqlist * L);
int list_length(sqlist * L);
int list_local(sqlist * L, data_type value);
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_find_index(sqlist * L, data_type value);
int list_delete(sqlist * L, int pos);
#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_show(sqlist * L){
int i;
if (L==NULL){
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){
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){
return -1;
}
free(L);
L = NULL;
return 0;
}
int list_empty(sqlist * L){
if(L==NULL){
return -1;
}
if(L->last == -1){
printf("%s\n", "list is empty");
return 1;
}
return 0;
}
int list_length(sqlist * L){
if(L==NULL){
return -1;
}
return L->last+1;
}
int list_local(sqlist * L, data_type value){
return 0;
}
// 指定索引插入
int list_insert(sqlist * L, data_type value, int pos){
int i;
if (L==NULL){
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){
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){
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){
// empty
if(list_empty(L) == 1){
return 0;
}
// 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_find_index(sqlist * L, data_type value){
if (L==NULL){
return -1;
}
int i;
for (i = 0; i < L->last+1; i++){
if(value == L->data[i]){
return i;
}
}
return -1;
}
// 删除指定索引的元素
int list_delete(sqlist * L, int pos){
// empty
if(list_empty(L) == 1){
return 0;
}
// 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;
}
#include <stdio.h>
#include "sqlist.h"
int main(int argc, char const *argv[])
{
sqlist * L;
L = list_create();
if (L==NULL){
return -1;
}
// list_append(L, 33);
// list_insert(L, 10, 0);
list_modify(L, 20, 0);
// list_delete(L, 4);
list_show(L);
return 0;
}
标签:顺序,return,int,list,C语言,sqlist,last,数据结构,data
From: https://www.cnblogs.com/Wang-py/p/17720910.html