c语言线性表
#include "stdio.h"
#include "stdlib.h"
#define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量
#define LISTINCREMENT 10 //线性表存储空间的分配增量
#define OK 1;
#define ERROR 0;
typedef struct {
int *elem; //存储空间基指
int length; //当前长度
int listsize;//当前分配的存储容量
}Sqlist;
typedef int Status;
void main()
{
//初始化线性表
Status InitList(Sqlist *L);
//销毁线性表
Status DestroyList(Sqlist *L);
//清空线性表
Status ClearList(Sqlist *L);
//判断线性表是否为空
Status ListEmpty(Sqlist L);
//获取线性表的长度
Status ListLength(Sqlist L);
//获取某个元素
Status GetElem(Sqlist L, int i, int *e);
//返回当前元素的下标
Status LocateElem(Sqlist L, int e, int *index);
//获取当前元素的前驱
Status PriorElem(Sqlist L, int cur_e, int *pre__e);
//获取当前元素的后继
Status NextElem(Sqlist L, int cur_e, int *next_e);
//在第i个元素插入元素
Status ListInsert(Sqlist *L, int i, int e);
//删除第i个元素并将元素保存在del_e中
Status ListDelete(Sqlist *L, int i, int *del_e);
//访问顺序表的每个元素
Status ListTraverse(Sqlist L);
Sqlist L;
InitList(&L);
ListInsert(&L, 1, 2);
ListInsert(&L, 2, 3);
ListInsert(&L, 1, 1);
//输出顺序链表中的所有值
for (int i = 0; i < L.length; i++)
{
printf("元素的第%d值为%d\n", i + 1, L.elem[i]);
}
int del_e = 0;
ListDelete(&L, 3, del_e);
printf("删除的元素为%d\n", del_e);
printf("输出删除后的线性表\n");
//输出顺序链表中的所有值
for (int i = 0; i < L.length; i++)
{
printf("元素的第%d值为%d\n", i + 1, L.elem[i]);
}
}
//初始化顺序线性表
Status InitList(Sqlist *L) {
L->elem = (int *)malloc(sizeof(int)*LIST_INIT_SIZE);
if (L->elem == NULL) {
exit(0);
}
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return OK;
}
//在下标为i的地址插入元素
Status ListInsert(Sqlist *L, int i, int e) {
if (i<1 || i>L->length + 1) {
return ERROR;
}
if (L->length >= L->listsize) {
//重新分配内存
L->elem = (int *)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(int));
};
for (int j = L->length - 1; j >= (i - 1); j--)
{
L->elem[j + 1] = L->elem[j];
}
L->elem[i - 1] = e;
L->length++;
return OK;
}
//删除第i个元素并将元素保存在del_e中
Status ListDelete(Sqlist *L, int i, int *del_e) {
if (i<1 || i>L->length) {
return ERROR;
}
//保留删除的元素
del_e = L->elem[i - 1];
for (int j = i - 1; j<L->length; j++)
{
L->elem[j] = L->elem[j + 1];
}
L->length--;
}
//销毁顺序表
Status DestroyList(Sqlist *L) {
if (L->elem != NULL) {
free(L->elem);
return OK;
}
return ERROR;
}
//清空顺序表
Status ClearList(Sqlist *L) {
L->length = 0;
return OK;
}
//判断顺序表是否为空
Status ListEmpty(Sqlist L) {
if (L.length < 1) {
return OK;
}
return ERROR;
}
//返回顺序表的长度
Status ListLength(Sqlist L) {
return L.length;
}
//返回线性表的第I个元素
Status GetElem(Sqlist L, int i, int *e) {
if (i<1 && i>L.length) {
return ERROR;
}
e = L.elem[i - 1];
}
//返回当第一个元素的下标
Status LocateElem(Sqlist L, int e, int *index) {
for (int i = 0; i < L.length; i++)
{
if (e == L.elem[i]) {
index = i;
return OK;
}
}
return ERROR;
}
//获取当前元素的前驱
Status PriorElem(Sqlist L, int cur_e, int pre__e) {
for (int i = 0; i < L.length; i++)
{
if (cur_e == L.elem[i] && i != 0) {
pre__e = L.elem[i - 1];
return OK;
}
}
return ERROR;
}
//获取当前元素的后继
Status NextElem(Sqlist L, int cur_e, int next_e) {
for (int i = 0; i < L.length; i++)
{
if (cur_e == L.elem[i] && i < L.length) {
next_e = L.elem[i + 1];
return OK;
}
}
return ERROR;
}
//访问线性表中的所有元素
Status ListTraverse(Sqlist L) {
for (int i = 0; i < L.length; i++)
{
if (L.elem[i] == NULL) {
return ERROR;
}
}
return OK;
}
Status MergeList(Sqlist L, Sqlist L1, Sqlist *L2) {
int *pa, *pb, *pc, *pa_last, *pb_last;
pa = L.elem;
pb = L1.elem;
L2->listsize = L2->length = L.length + L1.length;
pc = L2->elem = (int *)malloc(L2->length * sizeof(int));
if (L2->elem == NULL) {
exit(0);
}
pa_last = L.elem + L.length - 1;
pb_last = L1.elem + L1.length - 1;
while (pa <= pa_last&&pb <= pb_last)
{
if (*pa <= *pb) {
*pc++ = *pa++;
}
else
{
*pc++ = *pb++;
}
}
while (pa <= pa_last)
{
*pc++ = *pa++;
}
while (pb <= pb_last)
{
*pc++ = *pb++;
}
return OK;
}