线性表
线性表的初始化
Status InitList(SqList &L) {
L.elem = (ElemType *) malloc(sizeof(ElemType) * MAXSIZE);
if (!L.elem)
exit(OVERFLOW);
L.length = 0;
return OK;
}
线性表的销毁
void DestoryList(SqList &L) {
if (L.elem)
free(L.elem);
L.elem = NULL;
L.length = 0;
}
线性表的清空
void clearList(SqList &L) {
L.length = 0;//将线性表的长度置为0
}
线性表的长度
int GetLength(SqList L) {
return L.length;
}
判断线性表是否为空
void isEmpty(SqList L) {
if (L.length == 0)printf("顺序表为空");
else printf("顺序表不为空");
}
获取元素
int GetElem(SqList L, int i, ElemType &e) {
if (i < 1 || i > L.length)return ERROR;
e = L.elem[i - 1];
return OK;
}
按值查找
int LocateElem(SqList L, ElemType e) {
for (int i = 0; i < L.length; ++i)
if (L.elem[i] == e)return i + 1;
return 0; //查找失败
}
插入
Status SqlInsert(SqList &L, int pos, ElemType e) {
if (pos < 1 || pos > L.length + 1) return ERROR;
if (L.length == MAXSIZE) return ERROR;
for (int i = L.length - 1; i >= pos - 1; --i) {
L.elem[i + 1] = L.elem[i];
}
L.elem[pos - 1] = e;
L.length++;
return OK;
}
删除
Status SqlDelete(SqList &L, int pos) {
if (pos < 1 || pos > L.length) return ERROR;
for (int i = pos - 1; i < L.length; ++i) {
L.elem[i] = L.elem[i + 1];
}
L.length--;
/*
* for(int j =pos;j<=L.length-1;j++){
* L.elem[j-1] = L.elem[j];
* L.length--;
* }
* */
printf("删除成功\n");
}
查看顺序表
void showList(SqList L) {
printf("顺序表如下: ");
for (int i = 0; i < L.length; ++i) {
printf("%2d", L.elem[i]);
}
putchar(10);
}
基本操作是这些 还有排序 反转 等
头文件SqList.h
#include <stdio.h>
#include "stdlib.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int ElemType;
typedef struct SqList{
ElemType *elem;
int length;
}SqList;
//线性表初始化
Status InitList(SqList &L);
//销毁线性表
void DestoryList(SqList &L);
//清空线性表
void clearList(SqList &L);
//线性表的长度
int GetLength(SqList L);
//判断线性表的长度是否为空
void isEmpty(SqList L);
//线性表取值
int GetElem(SqList L,int i,ElemType &e);
//按值查找
int LocateElem(SqList L,ElemType e);
//插入
Status SqlInsert(SqList &L,int pos,ElemType e);
//删除
Status SqlDelete(SqList &L,int pos);
//查看顺序表
void showList(SqList L);
main.cpp
#include "SqList.h"
int main() {
SqList L;
InitList(L);
int select = 1;
while (select) {
printf("************************************\n");
printf("* [1] push [2] GetElem *\n");
printf("* [3] length [4] SqlDelete *\n");
printf("* [5] LocateElem [6] showList *\n");
printf("* [7] clearList [8] isEmpty *\n");
printf("* [9] destory [0] quit_sys *\n");
printf("************************************\n");
printf("请选择:>");
scanf("%d", &select);
ElemType Item;
int pos;
if (select == 0)
break;
switch (select) {
case 1:
printf("请输入要插入的位置和数据(逗号分割):>");
scanf("%d,%d", &pos, &Item);
int k;
k = SqlInsert(L, pos, Item);
if (k == ERROR) {
printf("插入有误\n");
} else {
printf("插入成功");
}
break;
case 2:
printf("请输入获取第几个元素\n");
int i, j;
scanf("%d", &i);
j = GetElem(L, i, Item);
if (j == ERROR) {
printf("查找有误\n");
} else {
printf("查找到的元素为%d\n", Item);
}
break;
case 3:
printf("线性表的长度为%d\n", GetLength(L));
break;
case 4:
printf("请输入要删除元素的位置:>");
scanf("%d", &pos);
printf("%d", SqlDelete(L, pos));
break;
case 5:
printf("输入要查找的值:输出0失败\n");
scanf("%d", &Item);
printf("元素在第%d个位置\n", LocateElem(L, Item));
break;
case 6:
showList(L);
break;
case 7:
clearList(L);
break;
case 8:
isEmpty(L);
break;
case 9:
DestoryList(L);
break;
default:
printf("输入的命令错误,请重新输入。\n");
break;
}
}
return 0;
}
SqList.cpp
#include "SqList.h"
Status InitList(SqList &L) {
L.elem = (ElemType *) malloc(sizeof(ElemType) * MAXSIZE);
if (!L.elem)
exit(OVERFLOW);
L.length = 0;
return OK;
}
void DestoryList(SqList &L) {
if (L.elem)
free(L.elem);
L.elem = NULL;
L.length = 0;
}
void clearList(SqList &L) {
L.length = 0;//将线性表的长度置为0
}
int GetLength(SqList L) {
return L.length;
}
void isEmpty(SqList L) {
if (L.length == 0)printf("顺序表为空");
else printf("顺序表不为空");
}
int GetElem(SqList L, int i, ElemType &e) {
if (i < 1 || i > L.length)return ERROR;
e = L.elem[i - 1];
return OK;
}
int LocateElem(SqList L, ElemType e) {
for (int i = 0; i < L.length; ++i)
if (L.elem[i] == e)return i + 1;
return 0; //查找失败
}
Status SqlInsert(SqList &L, int pos, ElemType e) {
if (pos < 1 || pos > L.length + 1) return ERROR;
if (L.length == MAXSIZE) return ERROR;
for (int i = L.length - 1; i >= pos - 1; --i) {
L.elem[i + 1] = L.elem[i];
}
L.elem[pos - 1] = e;
L.length++;
return OK;
}
Status SqlDelete(SqList &L, int pos) {
if (pos < 1 || pos > L.length) return ERROR;
for (int i = pos - 1; i < L.length; ++i) {
L.elem[i] = L.elem[i + 1];
}
L.length--;
/*
* for(int j =pos;j<=L.length-1;j++){
* L.elem[j-1] = L.elem[j];
* L.length--;
* }
* */
printf("删除成功\n");
}
void showList(SqList L) {
printf("顺序表如下: ");
for (int i = 0; i < L.length; ++i) {
printf("%2d", L.elem[i]);
}
putchar(10);
}
标签:顺序,int,pos,elem,length,SqList,printf
From: https://www.cnblogs.com/hekang520/p/18387096