数据结构之循序表

#include<iostream>
#include<string>
#define MAX 100
#define OK 1
#define ERROR 0 
#define OVERFLOW -1
using namespace std;


typedef struct//学生结构体 
{
	string no;
	string name;
}Stu;


typedef struct //循序表结构体 
{
	Stu *elem;//此处必为指针,因为在新建表的时候为他开辟了空间。相当于数组,指向第一个元素
	int length;
}SqList;

void Show(Stu s)
{
	cout << "学号:" << s.no << " " << "姓名:" << s.name << endl;
}

int InitList(SqList &L)//新建顺序表 
{
	L.elem = new Stu[MAX];
	if (!L.elem)
		exit(OVERFLOW);
	L.length = 0;
	cout << "循序表建立成功!" << endl;
	return OK;
}

int SearchNO(SqList L, string no)//按学号查找 
{
	for (int i = 0; i < L.length; i++)
	{
		if (L.elem[i].no == no)
		{
			Show(L.elem[i]);
			return 0;
		}
	}
	cout << "未查询到此学号!!!" << endl;
	return 0 ;
}

int SearchNA(SqList L, string name)//按姓名查找 
{
	for (int i = 0; i < L.length; i++)
	{
		if (L.elem[i].name == name)
		{
			Show(L.elem[i]);
			return 0;
		}
	}
	cout << "未查询到此姓名!!!" << endl;
	return 0;
}

int Insert(SqList &L, int i, Stu s)//插入 
{
	if ((i < 1) || (i > L.length + 1))
	{
		return ERROR;
	}
	if (L.length == MAX)
	{
		cout << "储存空间已满" << endl;
	}
	for (int j = L.length - 1; j >= i - 1; j--)
	{
		L.elem[j + 1] = L.elem[j];
	}
	L.elem[i - 1] = s;
	L.length++;
	cout << "插入成功!" << endl;
	return OK;
}

int Delete(SqList &L, int i)//删除 
{
	if ((i < 1) || (i > L.length))
	{
		return ERROR;
	}
	for (int j = i - 1; j < L.length; j++)
	{
		L.elem[j] = L.elem[j + 1];
	}
	L.length--;
	cout << "删除成功!" << endl;
	return OK;
}

void Show(SqList L)//显示信息 
{

	for (int i = 0; i < L.length; i++)
	{
		cout << "学号:" << L.elem[i].no << " " << "姓名:" << L.elem[i].name << endl;
	}
}


void Features()
{
	cout << "1. 建立顺序表\n";
	cout << "2. 输入数据\n";
	cout << "3. 按学号查找\n";
	cout << "4. 按姓名查找\n";
	cout << "5. 插入\n";
	cout << "6. 删除\n";
	cout << "7. 输出数据\n";
	cout << "0. 退出\n\n";
	

}
int main()
{
	SqList L;//循序表 
	Stu s;
	string no;
	string name;
	int insert;
	int i;
	int choose = -1;
	Features();
	while (choose != 0)
	{
		cout << "请输入你要选择的操作:\n";
		cin >> choose;
		switch (choose)//选择操作
		{
			case 1:
				InitList(L);
				break;
			case 2:
				cout << "请输入你要输入数据的个数:";
				cin >> i;
				for (int j = 0; j < i; j++)
				{
					cin >> L.elem[j].no >> L.elem[j].name;
				}
				L.length = i;
				break;
			case 3:
				cout << "请输入你要查找的学号" << endl;
				cin >> no;
				SearchNO(L, no);
				break;
			case 4:
				cout << "请输入你要查找的姓名" << endl;
				cin >> name;
				SearchNA(L, name);
				break;
			case 5:
				cout << "请输入你要插入的位置" << endl;
				cin >> insert;
				cout << "请输入你要插入的学生信息" << endl;
				cin >> s.no >> s.name;
				Insert(L, insert, s);
				break;
			case 6:
				cout << "请输入你要删除的位置" << endl;
				cin >> i;
				Delete(L, i);
				break;
			case 7:
				Show(L);
				break;
		}
	}
	cout << "已退出!" << endl;
	return 0;
}

Github

https://github.com/chp0304/Data-Structure/tree/master/%E5%BE%AA%E5%BA%8F%E8%A1%A8

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值