c语言实现顺序表?--顺序表.h
#includestdio.h
#includestdlib.h
#includemalloc.h
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define TRUE 1
#define FLASE 0
typedef int Elemtype;
typedef int Status;
/*接口定义
Status InitList_Sq(SqList L,int size,int inc);
void CreateList_Sq(SqList L);
void print_Sq(SqList L);
int Search_Sq(SqList L, Elemtype e);
Status DestroyList_Sq(SqList L);
Status ClearList_Sq(SqList L);
Status ListEmpty_Sq(SqList L);
int ListLength_Sq(SqList L);
Status GetElem_Sq(SqList L, int i, Elemtype e);
Status PutElem_Sq(SqList L, int i, Elemtype e);
Status Append_Sq(SqList L, Elemtype e);
Status DeleteLast_Sq(SqList L, Elemtype e);
*/
--------------------
#include "顺序表.h"
//定义顺序表类型
typedef struct {
Elemtype *elem;
int length;
int listsize;
【c语言顺序表诸函数怎么写 顺序表的取值c语言代码】int increment;
}SqList;
//初始化顺序表
Status InitList_Sq(SqList L,int size,int inc) {
L.elem = (Elemtype *)malloc(size * sizeof(Elemtype));
L.length = 0;
L.listsize = size;
L.increment = inc;
return TRUE;
}
//创建顺序表
Status CreateList_Sq(SqList L) {
int i;
printf("请输入你要创建的顺序表元素个数:\n");
scanf_s("%d", L.length);
if (L.length = L.listsize) {
L.elem = (Elemtype *)realloc(L.elem, (L.listsize + L.increment) * sizeof(Elemtype));
}
if (!L.elem) {
return FLASE;
}
printf("请输入你要创建的顺序表:\n");
for (i = 0; iL.length; i++) {
scanf_s("%d", L.elem[i]);
}
}
//遍历顺序表
void print_Sq(SqList L) {
int i;
for (i = 0; iL.length; i++) {
printf("%4d", L.elem[i]);
}
}
//查找元素的位置
int Search_Sq(SqList L, Elemtype e) {
int i = 0;
while (L.elem[i] != eiL.length) {
i++;
}
if (iL.length)
return -1;
else
return i + 1;//因为C语言是从下标为0开始的,当i=0时表示第一个元素
}
//销毁顺序表
Status DestroyList_Sq(SqList L) {
if (L.elem == NULL)
return -1;
else
free(L.elem);
printf("\n销毁成功\n");
return TRUE;
}
//清空顺序表
Status ClearList_Sq(SqList L) {
if (L.elem == NULL)
exit(0);
int i;
Elemtype *p_elem = L.elem;
for (i = 0; iL.length; i++) {
*L.elem = NULL;
L.elem++;
}
L.elem = p_elem;
}
//判断顺序表是否为空
Status ListEmpty_Sq(SqList L) {
int i;
Elemtype* p_elem = L.elem;
for (i = 0; iL.length; i++) {
if (*L.elem != 0) {
L.elem = p_elem;
return FLASE;
}
L.elem++;
}
return TRUE;
}
//求顺序表的长度
int ListLength_Sq(SqList L) {
return L.length;
}
//用e返回顺序表L中第i个元素的值
Status GetElem_Sq(SqList L, int i, Elemtype e) {
int j;
Elemtype* p_elem = L.elem;
if (i1 || iL.length)
return FLASE;
for (j = 1; j = i; j++)
L.elem++;
e = *L.elem;
L.elem = p_elem;
return TRUE;
}
//将顺序表L中第i个元素赋值为e
Status PutElem_Sq(SqList L, int i, Elemtype e) {
L.elem[i - 1] = e;
return TRUE;
}
//在顺序表L表尾添加元素e
Status Append_Sq(SqList L, Elemtype e) {
L.elem[L.length] = e;
L.length++;
L.listsize += L.increment;
return TRUE;
}
//删除顺序表L表尾元素
Status DeleteLast_Sq(SqList L, Elemtype e) {
e = L.elem[L.length - 1];
L.length--;
return TRUE;
推荐阅读
- 假面骑士gis,假面骑士ginpen
- 电脑伪黑屏怎么修复呢,电脑黑屏了按哪个键可以修复
- 如何构建选题框架新媒体,新媒体选题规划的方法有
- php删除整个数据库备份 php删除数据表
- 豫北高中公众号关注学生,滑县豫北高级中学校长电话
- 体育游戏匍匐爬行教案,幼儿园匍匐爬行教案
- 服务器不识别移动硬盘,服务器识别不到移动硬盘
- mysql表怎么删除主键 mysql如何删除主键
- zblog用七牛cdn教程,七牛云静态cdn