龙空技术网

数据结构顺序表基本操作

三七二十一天 124

前言:

此时小伙伴们对“顺序表查找操作”大概比较关心,咱们都想要学习一些“顺序表查找操作”的相关知识。那么小编同时在网络上搜集了一些对于“顺序表查找操作””的相关内容,希望咱们能喜欢,姐妹们快快来了解一下吧!

#include <iostream>

using namespace std;

#define OK 1

#define ERROR 0

#define MAXSIZE 10

typedef int ElemType;

typedef int Status;

typedef struct {

ElemType *data;

int length;

} SqList;

void menu();

Status InitList(SqList &L);//初始化

void Assign(SqList &L);//输入值

void PrintList(SqList L);//输出值

Status GetElem(SqList &L, int i, ElemType &e);//取值

/*Status LocateElem(SqList L, ElemType e); //查找值;*/

Status ListInsert(SqList &L, int i, ElemType e);//插入值;

void ListSort(SqList &L);//排序算法

Status ListDelete( SqList &L, int i); //删除值

Status List_Insert( SqList &L, ElemType e);//顺序插入

void UnionList(SqList &La, SqList Lb);//合并线性表

int main() {

menu();

return 0;

}

void menu() {

SqList L;

if (InitList(L) == OK)

cout << "创建成功" << endl;

else

cout << "创建失败" << endl;

int m;

while (1) {

cout << "\n根据所作操作选择以下序列号:" << endl;

cout << "1.创建顺序表 2.执行插入操作 3.执行删除操作" << endl;

cout << "4.执行输出操作 5.执行查找操作 6.执行排序操作" << endl;

cout << "7.有序表插入 8.有序表合并 0.退出" << endl;

int num, i, x;

cin >> num;

switch (num) {

case 1:

Assign(L);//写入线性表;

break;

case 2:

int elem1, i;

cout << "请输入插入元素e和插入位置i" << endl;

cin >> elem1 >> i;

ListInsert(L, i, elem1);

if (ListInsert(L, i, elem1) == ERROR)

cout << "插入错误" << endl;

break;

case 3:

cout << "请输入删除的位置" << endl;

int LOC2;//删除的位置;

cin >> LOC2;

ListDelete( L, LOC2);

PrintList(L);

break;

case 4:

PrintList(L);

break;

case 5: {

int e = 0, LOC;

cin >> LOC;//location

if (GetElem(L, LOC, e) == ERROR)//取值操作

cout << "ERROR" << endl;

else

cout << "元素值为:" << e << endl;

}

break;

case 6: {

ListSort(L);//排序操作

break;

}

case 7://顺序插入操作

cout << "请输入插入元素" << endl;

int num3;

cin >> num3;

List_Insert(L, num3);

break;

case 8://合并表

SqList Lb;

InitList (Lb);

cout << "请创建新表Lb" << endl;

Assign(Lb);

UnionList(L, Lb);

PrintList(L);

break;

case 0:

return ;

}

}

}

Status InitList(SqList &L) {

L.data = new ElemType{MAXSIZE};

if (L.data == NULL)

return ERROR;

L.length = 0;

return OK;

}

void Assign(SqList &L) {

int N;

cout << "请输入元素的个数:" << endl;

cin >> N;

cout << "请输入顺序表中的元素" << endl;

for (int i = 0; i < N; i++) {

cin >> L.data[i];

L.length ++;

}

}

void PrintList(SqList L) {//输出函数

for (int i = 0; i < L.length ; i++) {

cout << L.data [i] << " ";

}

}

Status GetElem(SqList &L, int i, ElemType &e) {

if (i < 1 || i > L.length)

return ERROR;

e = L.data[i - 1];

return OK;

}

/*int LocateElem(SqList L, ElemType e) {

//查找值为e的

for (int i = 0; i < L.length ; i++)

if (L.data [i] = e)

return i + 1;

return 0;

}*/

Status ListInsert(SqList &L, int i, ElemType e) {

//插入值

if ((i < 1) || (i > L.length + 1))

return ERROR;

if (L.length == MAXSIZE)

return ERROR;

for (int j = L.length - 1 ; j >= i - 1; j--) {

L.data [j + 1] = L.data [j];

}

L.data [i - 1] = e;

++L.length ;

PrintList (L);

return OK;

}

Status ListDelete(SqList &L, int i) {

//删除第i个元素

if ((i < 1) || (i > L.length ))

return ERROR;

for (int j = i; j <= L.length - 1; j++) {

L.data [j - 1] = L.data [j];

}

--L.length ;

return OK;

}

void ListSort(SqList &L) {

int i, j;

for (i = 0; i < L.length - 1; i++) {

for (j = 0; j < L.length - i - 1; j++) {

if (L.data [j] > L.data [j + 1]) {

int n = L.data [j];

L.data [j] = L.data [j + 1];

L.data[j + 1] = n;

}

}

}

}

Status List_Insert(SqList &L, ElemType e) {

int j = 1;

for (int i = 1; i <= L.length ; i++) {

if (e > L.data[i] )

j++;

}

ListInsert(L, j, e);

cout << endl;

return OK;

}

void UnionList(SqList &La, SqList Lb) {

int j = La.length ;

int k = Lb.length ;

for (int i = j + 1; i <= j + k ; i++) {

ListInsert(La, i, Lb.data[i - j - 1 ]);

}

}



标签: #顺序表查找操作