顺序表在生活中不怎么使用,人们一般用顺序表的话,我感觉就都用成了数组。
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERLOW -2
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType* elem;
int length;
int listsize;
}Sqlist;
bool IsFull(Sqlist* L)
{
assert(L != nullptr);
return L->length >= L->listsize;
}
Status InitList_Sq(Sqlist* L)
{
assert(L != nullptr);
L->elem = (ElemType*)malloc(sizeof(ElemType) * LIST_INIT_SIZE);
if (L->elem == nullptr) exit(1);
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return OK;
}
void Reward(Sqlist* L)
{
assert(L != nullptr);
ElemType* newbase = (ElemType*)realloc(L->elem, sizeof(ElemType) * (L->listsize + LISTINCREMENT));
if (newbase == nullptr) exit(OVERLOW);
L->elem = newbase;
L->listsize = L->listsize += LISTINCREMENT;
}
Status Insert_Sq2(Sqlist* L, int pos, int val)
{
assert(L != nullptr);
if (pos<1 || pos>L->length + 1) return ERROR;
if (IsFull(L)) Reward(L);
for (int i = L->length - 1; i >= pos - 1; --i)
{
L->elem[i + 1] = L->elem[i];
}
L->elem[pos - 1] = val;
L->length += 1;
return OK;
}
Status Delete_Sq(Sqlist* L, int pos)
{
assert(L != nullptr);
if (pos<1 || pos>L->length) exit(1);
for (int i = pos; i <= L->length-1; ++i)
{
L->elem[i-1] = L->elem[i];
}
L->length -= 1;
return OK;
}
Status Resarch_Sq(Sqlist* L, int val)
{
assert(L != nullptr);
for (int i = 0; i <= L->length - 1; ++i)
{
if (L->elem[i] == val)
{
return i + 1;
}
}
return ERROR;
}
Status Updata_Sqval(Sqlist* L, int val, int newval)
{
assert(L != nullptr);
int pos = Resarch_Sq(L, val);
L->elem[pos - 1] = newval;
return OK;
}
Status Updata_Sqpos(Sqlist* L, int pos, int newval)
{
assert(L != nullptr);
L->elem[pos - 1] = newval;
return OK;
}
void Print_Sq(Sqlist* L)
{
assert(L != nullptr);
for (int i = 0; i < L->length; ++i)
{
printf("%d ", L->elem[i]);
}
}