顺序表的12种基本操作-洋葱先生-杨少通

注:本程序由Visual Studio 2015编写,与VC++6.0稍有区别,复制到VC++6.0下注释掉“#include “stdafx.h””即可运行,复制到VS下可直接运行。
在这里插入图片描述
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include <iostream>

#include <conio.h>

#include <malloc.h>

#define LIST_INIT_SIZE 80 //指定存储数据的个数

#define LISTINCREMENT 10 //每次增量是10个

#define OK 1

#define ERROR 0

typedef int Status;

typedef int Elemtype;

using namespace std;

typedef struct {

	char workerName[12];

	int workerAge;

	int workerSalary;

}ElemType;   //存储空间14

typedef struct {

	ElemType *elem;

	int length;

	int listSize;

}SqList;

//1.初始化顺序表

Status InitList(SqList &L) {

	L.elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));

	if (!L.elem) exit(OVERFLOW);

	L.length = 0;

	L.listSize = LIST_INIT_SIZE;

	cout << endl << "\t\t初始化数序表结束。";

	return OK;

}

//2.输入数据元素

void InputElem(SqList &L) {

	int num;   //输入元素的数量

	ElemType E;

	cout << endl << "\t   请输入您要输入的元素个数:";

	cin >> num;

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

		cout << endl << "\t     请输入第" << i + 1 << "个数据。" << endl;

		cout << "\t\t请输入员工姓名(12位):";

		cin >> E.workerName;

		cout << "\t\t请输入员工年龄:";

		cin >> E.workerAge;

		cout << "\t\t请输入员工工资:";

		cin >> E.workerSalary;

		L.elem[i] = E;

		L.length++;

	}

	cout << endl << "\t   输入结束,一共输入了" << num << "个元素";

}

//3.在指定位置插入元素

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

	ElemType *newBase;

	if (i<1 || i>L.length + 1)  //判断位置是否合法

		return ERROR;

	if (L.length == L.listSize)  //空间已满,增加分配

	{

		newBase = (ElemType*)realloc(L.elem, (L.listSize + LISTINCREMENT) * sizeof(ElemType));

		if (!newBase)

			exit(OVERFLOW);

		L.elem = newBase;

		L.listSize += LISTINCREMENT;

	}

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

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

	L.elem[i-1] = e;

	L.length++;

	return OK;

}

//4.删除指定位置的元素

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

	if (i<1 || i>L.length + 1)  //判断位置是否合法

		return ERROR;

	e = L.elem[i-1];

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

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

	L.length–;

	return  OK;

}

//5.判断顺序表是否为空

Status ListEmpty(SqList L) {

	return L.length == 0;

}

//6.获取顺序表长度

int ListLength(SqList L) {

	return L.length;

}

//7.获取指定位置元素

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

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

		return ERROR;

	e = L.elem[i-1];

	return OK;

}

//12.获取指定元素的位序

int LocateElem(SqList L, ElemType e) {

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

	{

		if ((strcmp(e.workerName, L.elem[i].workerName) == 0) && (e.workerAge == L.elem[i].workerAge) && (e.workerSalary == L.elem[i].workerSalary))

			return i;

	}

	return -1;

}

//8.获取指定元素的前驱

Status PriorElem(SqList L, ElemType cur_e, ElemType &pre_e) {

	int index = LocateElem(L, cur_e);

	if (index<1 || index>L.length-1)

		return ERROR;

	pre_e = L.elem[index-1];

	return OK;

}

//9.获取指定元素的后继

Status NextElem(SqList L, ElemType cur_e, ElemType &next_e) {

	int index = LocateElem(L, cur_e);

	if (index<0 || index>=L.length-1)

		return ERROR;

	next_e = L.elem[index + 1];

	return OK;

}

//10.清空顺序表

void ClearList(SqList &L) {

	L.length = 0;

	cout << endl << "\t\t顺序表清空完毕!";

}

//11.输出顺序表全部元素

void ListTraverse(SqList L) {

	cout << endl << "\t   顺序表中所有元素如下:" << endl << endl;

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

	{

		cout << "\t\t员工姓名:" << L.elem[i].workerName << ",员工年龄:" << L.elem[i].workerAge << ",员工工资:" << L.elem[i].workerSalary << endl;

	}

	cout << endl << "\t   顺序表中所有元素输出完毕,共" << L.length << "个。";

}

//13.销毁顺序表

void DestoryList(SqList &L) {

	free(L.elem);

	L.length = 0;

	L.listSize = 0;

	L.elem = NULL;

	cout << endl << "\t\t顺序表销毁完毕!";

}

int main()

{

	bool executive = true;  //外层循环执行

	int userSelect, loc;   //用户选择和插入、删除位置

	SqList L;

	ElemType E;

	//ElemType *e;

	cout << "\t\t\t\t*\t\t\t\t\t*";

	cout << endl << "\t\t\t\t*\t计科1512-02210151232-杨少通\t*" << endl;

	cout << "\t\t\t\t*****************************************" << endl << endl;

	cout << "\t\t1. 初始化顺序表 \t\t2.输入数据元素\t\t\t3.在指定位置插入元素" << endl;

	cout << "\t\t4.删除指定位置的元素\t\t5. 判断顺序表是否为空 \t\t6.获取顺序表长度" << endl;

	cout << "\t\t7. 获取指定位置元素 \t\t8.获取指定元素的前驱\t\t9. 获取指定元素的后继" << endl;

	cout << "\t\t10.清空顺序表\t\t\t11. 输出顺序表全部元素\t\t12.获取指定元素的位序" << endl;

	cout << "\t\t13.销毁顺序表\t\t\t0.退出" << endl;

	while (executive)

	{

		cout << endl << endl << "\t*请输入您的选择:";

		cin >> userSelect;

		switch (userSelect)

		{

		case 0://退出程序

			cout << endl << endl << "\t\t\t\t\t\t感谢您的使用!";

			executive = false;

			break;

		case 1://初始化顺序表

			InitList(L);

			break;

		case 2://输入数据元素

			InputElem(L);

			break;

		case 3://在指定位置插入数据元素

			cout << endl << "\t\t请输入员工姓名(12位):";

			cin >> E.workerName;

			cout << "\t\t请输入员工年龄:";

			cin >> E.workerAge;

			cout << "\t\t请输入员工工资:";

			cin >> E.workerSalary;

			cout << "\t\t请输入插入的位置:";

			cin >> loc;

			cout << endl << ((ListInsert(L, loc, E) == 0) ? "\t   插入失败!您输入的位置不合法!" : "\t   成功插入元素!");

			break;

		case 4://删除指定位置的数据元素

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

			cin >> loc;

			//e = (ElemType*)malloc(sizeof(ElemType));

			//if (ListDelect(L, loc, *e))

			if (ListDelete(L, loc, E))

			{

				cout << endl << "\t   删除成功!删除元素为“员工姓名:" << E.workerName << ",员工年龄:" << E.workerAge << ",员工工资:" << E.workerSalary << "”";

				//cout <<endl<< "\t   删除成功!删除元素为“员工姓名:"<<e->workerName<<",员工年龄:"<<e->workerAge<<",员工工资:"<< e->workerSalary<<"”";

			}

			else

			{

				cout << endl << "\t   删除失败!您输入的位置不合法!";

			}

			break;

		case 5://判断顺序表是否为空

			cout << endl << "\t\t顺序表的长度为:" << L.length << (ListEmpty(L) ? ",即为空表!" : (",即为非空表!"));

			break;

		case 6://获取顺序表的长度

			cout << endl << "\t    顺序表的长度为:" << ListLength(L);

			break;

		case 7://获取指定位置处的数据

			loc = 0;

			cout << endl << "\t   请输入您要取出的元素位置:";

			cin >> loc;

			if (GetElem(L, loc, E)) {

				cout << endl << "\t   取出成功!取出元素为“员工姓名:" << E.workerName << ",员工年龄:" << E.workerAge << ",员工工资:" << E.workerSalary << "”";

			}

			else

			{

				cout << endl << "\t   取出失败!您输入的位置不合法!";

			}

			break;

		case 8://获取指定元素的前驱

			cout << endl << "\t   请输入需要被查前驱的元素:";

			cout << endl << "\t\t请输入员工姓名(12位):";

			cin >> E.workerName;

			cout << "\t\t请输入员工年龄:";

			cin >> E.workerAge;

			cout << "\t\t请输入员工工资:";

			cin >> E.workerSalary;

			if (PriorElem(L, E, E)) {

				cout << endl << "\t   该元素前驱为“员工姓名:" << E.workerName << ",员工年龄:" << E.workerAge << ",员工工资:" << E.workerSalary << "”";

			}

			else

			{

				cout << endl << "\t   元素前驱查询失败!您输入的位置不合法!";

			}

			break;

		case 9://查后继

			cout << endl << "\t   请输入需要被查后继的元素:";

			cout << endl << "\t\t请输入员工姓名(12位):";

			cin >> E.workerName;

			cout << "\t\t请输入员工年龄:";

			cin >> E.workerAge;

			cout << "\t\t请输入员工工资:";

			cin >> E.workerSalary;

			if (NextElem(L, E, E)) {

				cout << endl << "\t   该元素后继为“员工姓名:" << E.workerName << ",员工年龄:" << E.workerAge << ",员工工资:" << E.workerSalary << "”";

			}

			else

			{

				cout << endl << "\t   元素后继查询失败!您输入的位置不合法!";

			}

			break;

		case 10://清空顺序表

			ClearList(L);

			break;

		case 11://输出顺序表

			ListTraverse(L);

			break;

		case 12:   //查询元素位序

			cout << endl << "\t   请输入需要被查位序的元素:";

			cout << endl << "\t\t请输入员工姓名(12位):";

			cin >> E.workerName;

			cout << "\t\t请输入员工年龄:";

			cin >> E.workerAge;

			cout << "\t\t请输入员工工资:";

			cin >> E.workerSalary;

			if (LocateElem(L, E) != -1) {

				cout << endl << "\t   该元素的位序为:" << LocateElem(L, E) + 1;

			}

			else

			{

				cout << endl << "\t   查询失败!";

			}

			break;

		case 13://销毁顺序表

			DestoryList(L);

			break;

		}

	}

	_getch();

	return 0;

}

本人初学数据结构,代码可能存在诸多问题,望各位海涵。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DS顺序表(也称为数组)是一线性数据结构,它使用连续的存储空间来存储元素。我可以告诉你如何进行一些常见的操作,例如插入、删除和访问元素。 1. 插入元素: 要在顺序表中插入元素,首先需要确定插入的位置。假设要在第i个位置插入元素x,需要将第i个位置及其之后的所有元素向后移动一位,然后将x放入第i个位置。这样可以保持顺序表的连续性。 代码示例: ```python def insert_element(arr, i, x): n = len(arr) arr.append(None) # 在末尾添加一个空元素 for j in range(n, i, -1): arr[j] = arr[j-1] arr[i] = x # 示例使用 my_list = [1, 2, 3, 4, 5] insert_element(my_list, 2, 10) print(my_list) # 输出: [1, 2, 10, 3, 4, 5] ``` 2. 删除元素: 要从顺序表中删除元素,首先需要确定删除的位置。假设要删除第i个位置的元素,需要将第i+1个位置及其之后的所有元素向前移动一位,然后将最后一个元素删除。 代码示例: ```python def delete_element(arr, i): n = len(arr) for j in range(i, n-1): arr[j] = arr[j+1] arr.pop() # 示例使用 my_list = [1, 2, 3, 4, 5] delete_element(my_list, 2) print(my_list) # 输出: [1, 2, 4, 5] ``` 3. 访问元素: 顺序表的元素可以通过索引访问。要访问第i个位置的元素,可以直接使用arr[i]进行访问。 代码示例: ```python def access_element(arr, i): return arr[i] # 示例使用 my_list = [1, 2, 3, 4, 5] print(access_element(my_list, 2)) # 输出: 3 ``` 这些是DS顺序表的一些常见操作,希望能对你有所帮助!如果有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值