数据结构 顺序表

上传一些自己瞎写的数据结构作业

补了一个顺序表的模板,用起来更方便
看这里

实验目的:

  1. 掌握程序设计的基本方法,要求能够对C/C++实现简单的算法设计
  2. 熟练掌握指针的应用
  3. 熟练掌握线性表的基本运算在顺序存储结构和链式存储结构上的实现
  4. 掌握顺序表以及线性链表的基本操作及其实现
  5. 能使用线性表来解决实际中遇到的问题

实验要求:

  1. 顺序表:完成初始化、插入、删除等基本功能

要点:

  1. 插入时需将插入位置后的元素全部向后移动一个位置
  2. 删除时需将删除位置后的全部元素向前移动一个位置
SqList.h
#pragma once
#include<iostream>
#define MAX_LIST_SIZE 100
using namespace std;

enum Status
{
	ERROR = 0,
	OK = 1
};

typedef int ElemType;

typedef struct SqList
{
	ElemType et[MAX_LIST_SIZE];
	int currentLength;
}SqList;

Status initSqList(SqList& sl);//初始化
Status insertSqList(SqList& sl, int target, ElemType e);//顺序表 插入位置 插入内容
Status deleteSqList(SqList& sl, int target);//顺序表 删除位置
ostream& operator<<(ostream& os, SqList& sl);//运算符重载
void SqListMenu();//菜单
void SqListChoose(SqList& sl, bool& flag);//选择功能
void insertPart(SqList& sl);//插入模块
void deletePart(SqList& sl);//删除模块
void displayPart(SqList& sl);//显示模块

SqList.cpp
#include "SqList.h"
using namespace std;

Status initSqList(SqList& sl)//初始化
{
	sl.currentLength = 0;
	return Status::OK;
}

Status insertSqList(SqList& sl, int target, ElemType e)//顺序表 插入位置 插入内容
{
	int index = target - 1;//索引
	if (sl.currentLength >= MAX_LIST_SIZE)
	{
		cout << "顺序表满" << endl;
		return Status::ERROR;
	}
	if (target > MAX_LIST_SIZE || target < 1)
	{
		cout << "范围错误" << endl;
		return Status::ERROR;
	}
	if (target > sl.currentLength + 1)
	{
		cout << "不能跳格" << endl;
		return Status::ERROR;
	}
	for (int i = sl.currentLength - 1; i >= index; i--)
	{
		sl.et[i + 1] = sl.et[i];
	}
	sl.et[index] = e;
	cout << "插入成功" << endl;
	sl.currentLength++;
	return Status::OK;
}

Status deleteSqList(SqList& sl, int target)//顺序表 删除位置
{
	int index = target - 1;
	if (target > MAX_LIST_SIZE || target < 1)
	{
		cout << "范围错误" << endl;
		return Status::ERROR;
	}
	for (int i = index; i < sl.currentLength - 1; i++)
	{
		sl.et[i] = sl.et[i + 1];
	}
	sl.currentLength--;
	cout << "删除成功" << endl;
	return Status::OK;
}

ostream& operator<<(ostream& os, SqList& sl)//运算符重载
{
	for (int i = 0; i < sl.currentLength; i++)
	{
		os << sl.et[i] << " ";
	}
	return os;
}

void SqListMenu()//菜单
{
	system("cls");
	cout << "1、插入" << endl;
	cout << "2、删除" << endl;
	cout << "3、显示" << endl;
	cout << "0、退出" << endl;
}

void insertPart(SqList& sl)//插入模块
{
	int target;
	int e;
	cout << "输入要插入的位置:";
	cin >> target;
	cout << "输入要插入的内容:";
	cin >> e;
	insertSqList(sl, target, e);
	system("pause");
}

void deletePart(SqList& sl)//删除模块
{
	int target;
	cout << "输入要删除的位置:";
	cin >> target;
	deleteSqList(sl, target);
	system("pause");
}

void displayPart(SqList& sl)//显示模块
{
	cout << sl << endl;
	system("pause");
}

void SqListChoose(SqList& sl, bool& flag)//选择功能
{
	int choice;
	cout << "选择:";
	cin >> choice;
	switch (choice)
	{
	case 1:
		insertPart(sl);
		break;
	case 2:
		deletePart(sl);
		break;
	case 3:
		displayPart(sl);
		break;
	case 0:
		flag = false;
		break;
	default:
		cout << "输入有误" << endl;
		system("pause");
		break;
	}
}

main.cpp
#include<iostream>
#include"SqList.h"

using namespace std;

int main()
{
	SqList sl;
	initSqList(sl);
	bool flag = true;
	while (flag)
	{
		SqListMenu();
		SqListChoose(sl, flag);
	}

	system("pause");

	return 0;
}


插入
错误插入
显示

删除
显示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值