实验二:线性表综合实验(1.单链表)

来源:互联网 发布:route add linux 编辑:程序博客网 时间:2024/06/05 05:25

一.实验目的

1.巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。

.实验内容

1.建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。

三.实验步骤

1.通过建立一个Student类来储存姓名与成绩信息。

2.建立一个Student类的友元类Manage来设置成员函数以达到功能的实现。

单链表实现:

代码:

#include<iostream>

#include<fstream>

#include<string>

#include<stdio.h>

#include<conio.h>

#include<Windows.h>

using namespace std;

class Student

{

protected:

char Name[20];

string Score;

Student *next;

public:

Student(char *Name,string Score)

{

strcpy(this->Name,Name);

this->Score= Score;

}

friend class Manage;

};

class Manage

{

private:

Student *student;

public:

Manage()

{

student = 0;

}

~Manage()

{

Student *s;

s = student;

while (s)

{

s = s->next;

delete student;

student = s;

}

student = 0;

}

void Find(char Name[20]);        //查找信息

void Input();                    //添加信息

void Delete(char Name[20]);      //删除信息

void Printlist();                //输出信息

void Output(Student *s)          //显示数据

{

cout<< "\t姓名:" << s->Name<< endl;

cout<< "\t成绩:" << s->Score<< endl;

cout<< endl;

}

};

void Manage::Input()

{

system("cls");           //清屏

Student *s, *s2;          //新结点指针

string  Score;

char Name[20];

char c;

cout<< "\n添加学生成绩信息:\n";         

cout<< "输入姓名:\t";

cin>> Name;

cout<< endl;

cout<< "输入成绩:\t";

cin>> Score;

cout<< endl;

s =new Student(Name, Score);

s->next = 0;           

if (student)            //若已经存在结点

{

s2 = student;

while (s2->next)   //查找尾结点

{

s2 = s2->next;

}

s2->next = s; //连接

}

else //若不存在结点(表空)

{

student = s; //连接

}

system("cls");

cout<< "\t\t\t   ***添加成功***\n" << endl;

    return;

}

void Manage::Delete(char Name[20])//删除人员

{

system("cls");

cout<< "输入要删除的学生姓名:\t";

cin>> Name;

cout<< endl;       //查找要删除的结点

Student *s1, *s2;

s1 = student;

while (s1)

{

if (strcmp(s1->Name,Name) == 0)

break;

else

{

s2 = s1;

s1 = s1->next;   //结点下移

}

}                  

if (s1 != NULL)    //若找到结点,则删除

{

if (s1 == student) //若要删除的结点是第一个结点

{

student = s1->next;

delete s1;

}

else //若要删除的结点是后续结点

{

s2->next = s1->next;

delete s1;

}

cout<< "\t\t***删除成功***\n";

}

else //未找到结点

cout<< "未找到该学生!\n";

getch();

}

void Manage::Find(char Name[20])

{

Student *s;

s = student;

cout<< "输入姓名 Name:  ";

cin>> Name;

while (s)

{

if (strcmp(s->Name,Name) == 0)

{

Output(s);

}

s = s->next;

}

system("pause");

}

void Manage::Printlist()

{

system("cls");

Student *s;

s = student;

while (s)

{

cout<< "姓名: "<< s->Name<< endl;

cout<<"成绩: " << s->Score<< endl;

s = s->next;

}

system("pause");

}

int main(void)

{

char Name[20];

Manage m;

int c;

do

{

system("cls");

cout<< "                    ==============================" << endl;

cout<< "                    \t|    1.新增学生成绩    |\t" << endl;

cout<< "                    \t|    2.删除学生成绩    |\t" << endl;

cout<< "                    \t|    3.查询学生成绩    |\t" << endl;

cout<< "                    \t|    4.显示学生成绩    |\t" << endl;

cout<< "                    ==============================" << endl;

cout<< "0-退出\t请选择(1-4): ";

cin>> c;

switch (c)

{

case 1: m.Input(); break;

case 2: m.Delete(Name); break;

case 3: {

system("cls");

m.Find(Name);

};break;

case 4:m.Printlist();

default:break;

}

}while (c != 0);

return 0;

}

运行结果:

1.添加信息:

2.信息显示:

3.删除名为tom信息,并显示删除后结果:

4.查询名为jack学生信息结果:




原创粉丝点击