顺序表的创建以及基本操作函数(C语⾔)两种创建⽅式以及初始化函数都在下⽅代码中:
#define _CRT_SECURE_NO_WARNINGS
#pragma once//防⽌头⽂件重复包含
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 50
#define OK 1;
#define ERROR 0;
typedef int ElemType;
typedef struct {
ElemType data[MAXSIZE];
int Length;
}SqList_;
typedef struct {
ElemType* elems;
int Length;
int Listsize;
}SqList;
int InitList_(SqList_* L){
memset(L->data, 0, sizeof(L->data));
return OK;
}
int InitList(SqList* L){
L->elems=(ElemType*)malloc(MAXSIZE * sizeof(ElemType));
if (L->elems == NULL)
{
printf("内存开辟失败\n");
return ERROR;
}
L->Length = 0;
L->Listsize = MAXSIZE;
return OK;
}
int Length(SqList L) {
return L.Length;
};
int LocateElem(SqList L, ElemType e) {
for (int i = 0; i < L.Length; i++)
{
if (L.elems[i] == e) {
return i;
}
}
return -1;
};
ElemType GetElem(SqList L, int i) {
return L.elems[i-1];
};
int ListInsert(SqList* L, int i, ElemType e) {
c语言listinsert函数if (i<1 || i>L->Length+1)
return ERROR;
if (L->Length == MAXSIZE)
return ERROR;
for (int j = L->Length; j > i; j--)
{
L->elems[j] = L->elems[j - 1];
}
L->elems[i-1] = e;
L->Length++;
return OK;
return OK;
};
int Append(SqList* L, ElemType e) {
if (L->Length == MAXSIZE)
return ERROR;
L->elems[L->Length++] = e;
}
int ListDelete(SqList* L, int i, ElemType* e) { if (i<1 || i>L->Length)
return ERROR;
*e = L->elems[i - 1];
for (int j = i; j < L->Length; j++)
{
L->elems[j-1] = L->elems[j];
}
L->Length--;
return OK;
};
void PrintList(SqList L) {
for (int i = 0; i < L.Length; i++)
{
printf("%d\n", L.elems[i]);
}
};
int Empty(SqList L) {
if (L.Length == 0) return 1;
else return 0;
};
int DestroyList(SqList* L) {
L->Length = 0;
L->Listsize = 0;
free(L->elems);
L = NULL;
};
int main() {
SqList List;
SqList* L = &List;
InitList(L);
for (int i = 0; i < 10; i++)
{
Append(L, i + 1);
}
PrintList(List);
return 0;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。