#include<iostream>
#include<string.h>
using namespace std;
typedef struct {
int id;
string name;
}Node;//结点定义
typedef struct {
Node* element;//基地址(动态长度)
int length;//表长
}Linklist;
#define MAXSIZE 100//最大长度
void menu();//声明菜单函数
void Createlist(Linklist& L);//创造空表函数
void display(Linklist& L);//遍历输出顺序表中的内容
int insert(Linklist& L, Node data, int n);//插入数据
bool find(Linklist& L);//查找数据
void add(Linklist &L);//追加数据;
void Delete(Linklist& L);//删除数据
int main()
{
Linklist L;
int flag=1;
while (flag)
{
cout << "请选择操作:" << endl;
menu();
cin >> flag;
switch(flag)
{
case 1:
Createlist(L);
cout<<"创建成功!" <<endl;
break;
case 2:
display(L);
break;
case 3:
if (find(L))
{
cout << "已找到数据!" << endl;
}
break;
case 4:
{
cout << "输入学号、姓名:" << endl;
Node node;
cin >> node.id >> node.name;
cout << "输入插入的位置:";
int n;
cin >> n;
if (insert(L, node, n)==1)
{
}
else {
if (insert(L, node, n) == 2)
cout << "数据不合理!"<<endl;
else
cout << "顺序表已满!"<<endl;
}
break;
}
case 5:
add(L);
break;
case 6:
{
Delete(L);
break;
}
default :
break;
}
}
return 0;
}
void menu()
{
cout << "-----------菜单----------" << endl;
cout << "-----------1创建---------" << endl;
cout << "-----------2输出---------" << endl;
cout << "-----------3查找---------" << endl;
cout << "-----------4插入---------" << endl;
cout << "-----------5追加---------" << endl;
cout << "-----------6删除---------" << endl;
}
void Createlist(Linklist& L)//创造空表函数
{
L.element = new Node[MAXSIZE];
if (!L.element)exit(EOVERFLOW);//存储分配失败并退出;
L.length = 0;//定义表长
}
void display(Linklist& L)//遍历输出顺序表的内容
{
int i = 0;
for (i;i < L.length;i++)
{
cout << (L.element + i)->id << " ";
cout << (L.element + i)->name << endl;
}
}
int insert(Linklist& L,Node data,int n)//插入函数
{
if (n<1 || n>L.length+1) return 2;
else{
if (L.length == MAXSIZE)return 0;
else{
for (int i = L.length-1; i>=n-1; i--)
{
L.element[i + 1] = L.element[i];
}
L.element[n-1].id= data.id;
L.element[n-1].name = data.name;
L.length++;
return 1;
}
}
cout<<"插入成功!"<<endl;
}
bool find(Linklist& L)//查找函数
{
bool flag=0;
int n;
cout << "请选择查找方式:1 结点位置 2 学号 3 姓名 " << endl;
cin >> n;
switch (n)
{
case 1:
cout << "请输入要查询的结点数:";
int nn;
cin >> nn;
if (nn > L.length)
{
cout << "超出查找范围!" << endl;
return 0;
}
else
{
cout << "第" << nn<< "个结点内容:学号: " << (L.element+nn-1)->id << "姓名:" << (L.element+nn-1)->name<<endl;
return 1;
}
break;
case 2:
{
int i = 1;
cout << "请输入学号:";
int _id;
cin >> _id;
while (i <= L.length)
{
if (_id == L.element->id)
{
cout << "第" << i+1 << "个结点, 学号:" << _id << "姓名:" << L.element->name << endl;
break;
return 1;
}
L.element++;
}
break;
return 0;
};
case 3:
{
int i = 0;
cout << "请输入姓名:";
string _name;
cin >> _name;
while (i < L.length)
{
if (_name == L.element->name)
{
cout << "第" << i+1 << "个结点, 学号:" << L.element->id << "姓名:" << L.element->name << endl;
return 1;
break;
}
L.element++;
}
return 0;
}
};
}
void Delete(Linklist& L)
{
int n;
cout << "输入删除的结点:";
cin >> n;
for (int i = n; i < L.length; i++)
{
L.element[i-1] = L.element [i];
}
L.length--;
cout << "删除成功!" << endl;
}
void add(Linklist& L)
{
cout<<"请输入追加结点个数:";
int n;
cin>>n;
Node node;
while(n--)
{
cout << "学号:";
cin >> node.id;
cout << "姓名:";
cin >> node.name;
L.element[L.length] = node;
L.length++;
cout<<"追加成功!"<<endl;
}
}