注:本程序由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;
}
本人初学数据结构,代码可能存在诸多问题,望各位海涵。