// 实验一--顺序表的实现.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include <iostream>
#include<string.h>
#define ERROR 0
#define MAXSIZE 1000
#define OVERFLOW -2
#define OK 1
using namespace std;
typedef struct stu {
char no[8];//八位学号
char name[20];//学生姓名
int score;//所得成绩
}Student;
typedef Student ElemType;
typedef int Status;
typedef struct {
Student* elem;
int length;
}SqList;
SqList StudentList;
Student IniNode()
{
Student* node = new Student;
return *node;
}
void CreateNode(Student& Stu)
{
cout << "Now you need to input the ID,name and score of the student.\n";
cin>>Stu.no>>Stu.name>>Stu.score;
return;
}
bool IniList(SqList& StudentList)
{
StudentList.elem = new ElemType[MAXSIZE];
if (!StudentList.elem) exit(ERROR);//分配失败,退出
StudentList.length = 0;
return OK;
}
Status ListCreate_Sq(SqList &L,int i ,ElemType e)
{
strcpy(L.elem[i].no,e.no);
strcpy(L.elem[i].name,e.name);
L.elem[i].score = e.score;
++L.length; //表长增1
return OK;
}
void PrintStudent(SqList& StuList)
{
for (int i = 0; i < StuList.length; i++)
{
cout << StuList.elem[i].no << ' ';
cout << StuList.elem[i].name << ' ';
cout << StuList.elem[i].score << '\n';
}
}
void FindStudentByName(SqList& StuList, string person)
{
for (int i = 0; i < StuList.length; i++)
{
if (StuList.elem[i].name == person) {
cout << "Find it! There is the student's information:\n";
cout << StuList.elem[i].no << ' '<<StuList.elem[i].name <<' '<< StuList.elem[i].score << '\n';//问题
return;
}
}
cout << "Sorry,I can't find the person you called." << '\n';
}
//类型原来是Student
void FindStudentByIndex(SqList& StuList, int index)
{
if (index < 1 || index > StuList.length)
{
cout << "Index Error!\n";
exit(OVERFLOW);
}
else {
//return StuList.elem[index - 1];
cout << StuList.elem[index-1].no<<' ';
cout << StuList.elem[index-1].name<<' ';
cout << StuList.elem[index-1].score << '\n';
}
}
Status InsertStudent(SqList& StuList, int index, Student& Stu)
{//正难则反,在顺序表L中第index个位置插入新元素,index的合法范围是1<=index<=L.length+1
if(index<1 || index>StuList.length+1) return ERROR; //i值不合法
if(StuList.length==MAXSIZE) return ERROR; //当前存储空间已满
for(int j=StuList.length-1;j>=index-1;j--)
{ //L.elem[j+1]=L.elem[j];结构体不可以直接赋值
//插入位置及之后的元素后移
strcpy(StuList.elem[j+1].no,StuList.elem[j].no);
strcpy(StuList.elem[j+1].name,StuList.elem[j].name);
StuList.elem[j+1].score = StuList.elem[j].score;
}
// StuList.elem[index-1]=e; //将新元素e放入第i个位置
strcpy(StuList.elem[index-1].no,Stu.no);
strcpy(StuList.elem[index-1].name,Stu.name);
StuList.elem[index-1].score = Stu.score;
StuList.length++;
cout << "Insert successfully!\n";
}
Status DeleteStudent(SqList& StuList, int index)
{//同上理,在顺序表L中第index个位置插入新元素,index的合法范围是1<=index<=L.length
if(!StuList.length) return ERROR;//空表没有结点删了
if (index<1 || index>StuList.length)
{
cout << "Index Error!";
return ERROR;
}
for (int j = index; j < StuList.length; j++)
{
StuList.elem[j - 1] = StuList.elem[j];
}
StuList.length--;
cout << "Delete successfully!\n";
}
int main()
{
int n,i=0;
string cmd;
SqList StudentList;
Student e;
cout << "Hello!Welcome The System of SqList\n";
cout << "Now let's initiate a SqList!\n";
IniList(StudentList);
cout << "To create a SqList...\nPlease input the population of students:\n";
cin>>n;
cout<<"Now let's input the information including ID, name and score of all students:\n";
for(i=0;i<n;i++)
{
cin>>e.no>>e.name>>e.score;
ListCreate_Sq(StudentList,i,e);
}
cout<<"Oh!Input finished!\n";
cout << "Please input your cmd to express the operation you want('#' is to the end).\n";
cout << "The cmds is there:\nPrintStudent(Print) FindStudentByName(FindByName)\nFindStudnetByIndex(FindByIndex) InsertStudent(Insert)\nDeleteStudent(Delete)\n";
cin >> cmd;
while (cmd[0] != '#')
{
if (cmd == "PrintStudent" || cmd == "Print") PrintStudent(StudentList);
if (cmd == "FindByName" || cmd == "FindStudentByName") {
string person;
cin >> person;
FindStudentByName(StudentList, person);
}
if (cmd == "FindByIndex" || cmd == "FindStudentByIndex") {
int index;
cin >> index;
FindStudentByIndex(StudentList, index);
}
if (cmd == "Insert" || cmd == "InsertStudent") {
cout << "OK! Please input the index of the student.\n";
int index;
cin >> index;
Student Stu = IniNode();
CreateNode(Stu);
InsertStudent(StudentList, index, Stu);
}
if (cmd == "Delete" || cmd == "DeleteStudent") {
cout << "OK! Please input the index of the student.\n";
int index;
cin >> index;
DeleteStudent(StudentList, index);
}
cout <<"What is the next cmd you want give?\n";
cin >> cmd;
}
return 0;
}
数据结构线性表----顺序表的基本实现和简单操作(个人代码)
最新推荐文章于 2024-09-11 09:33:21 发布