实现线性顺序表的增删查改功能,主要有main.h main.c list.c 三个模块函数实现。
#ifndef _MAIN_H_
#define _MAIN_H_
#define SIZE 500
typedef int data_t;
typedef struct
{
data_t data[SIZE];
int count;
}LIST;
enum e_List
{
ERROR = -1,
OK
};
enum e_isListFull
{
FALSE = 0,
TRUE
};
LIST * createList();
int insertList(LIST * List, data_t Data, int offset);
int updateList(LIST * List, data_t Data, int offset);
void destroyList(LIST * List);
int deleteList(LIST * List, data_t * Data, int offset);
int updateList(LIST * List, data_t Data, int offset);
void showList(LIST * List);
int isListFull(LIST * List);
#endif
#include "main.h"
#include <stdio.h>
int main(void)
{
LIST * List = NULL;
int Data = 0;
List = createList();
// 插入数据
insertList(List, 1, 0);
insertList(List, 2, 0);
insertList(List, 3, 0);
insertList(List, 4, 0);
insertList(List, 5, 0);
insertList(List, 8, 4);
insertList(List, 5, -1);
insertList(List, 8, 5);
insertList(List, 8, 6);
printf("%d\n",List->count);
showList(List);
//更新数据
if (OK == updateList(List, 100, 3))
{
showList(List);
}
if (OK == updateList(List, 100, 5))
{
showList(List);
}
if (OK == updateList(List, -100, 0))
{
showList(List);
}
//删除数据
if (OK == deleteList(List, &Data, 0))
{
showList(List);
printf("%d\n",Data);
}
if (OK == deleteList(List, &Data, 0))
{
showList(List);
printf("%d\n",Data);
}
if (OK == deleteList(List, &Data, 4))
{
showList(List);
printf("%d\n",Data);
}
if (OK == deleteList(List, &Data, -1))
{
showList(List);
printf("%d\n",Data);
}
destroyList(List);
List = NULL;
return 0;
}
/*
易错点: 1, List->count 容易写成List.count
2, 在offset位置处插入和删除数据的时候,容易越界出现段错误
*/
#include "main.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
LIST * createList()
{
LIST * List = NULL;
List = (LIST *)malloc(SIZE * sizeof(LIST));
if (NULL == List)
{
printf("create List error!\n");
return ;
}
memset(List, 0, SIZE * sizeof(LIST));
List->count = 0;
//List->data[0] = 0;
return List;
}
void destroyList(LIST * List)
{
if (NULL == List)
{
printf("List is empty!\n");
return ;
}
free(List);
List = NULL;
}
int isListFull(LIST * List)
{
if (NULL == List)
{
printf("List is empty!\n");
return ERROR;
}
if (List->count == SIZE - 1)
{
printf("List if full!\n");
return TRUE;
}
return FALSE;
}
int insertList(LIST * List, data_t Data, int offset)
{
if (NULL == List
|| offset < 0
|| offset > List->count)
{
printf("Parameter is error!\n");
return ERROR;
}
if (isListFull(List) == FALSE)
{
int i = 0;
for (i = List->count; i >= offset; i--)
{
List->data[i+1] = List->data[i];
}
List->data[offset] = Data;
}
List->count++;
return OK;
}
void showList(LIST * List)
{
if (NULL == List)
{
printf("List is empty!\n");
return ;
}
int i = 0;
for (i = 0; i < List->count; i++)
{
printf("%d ",List->data[i]);
}
puts("");
//return TRUE;
}
int deleteList(LIST * List, data_t * Data, int offset)
{
if (NULL == List
|| offset < 0
|| offset >= List->count)
{
printf("Parameter is ERROR!\n");
return ERROR;
}
*Data = (List->data[offset]);
int i = offset;
for (i = offset; i < List->count - 1; i++)
{
List->data[i] = List->data[i+1];
}
List->count--;
return OK;
}
int updateList(LIST * List, data_t Data, int offset)
{
if (NULL == List
|| offset < 0
|| offset >= List->count)
{
printf("Parameter is wrong!\n");
return ERROR;
}
List->data[offset] = Data;
return OK;
}