给出学生信息以类形式定义的链表的编程:

[Experiment topic 3]

**class Student//学生结点类
{
long no;
char *name;
int score;
Student link;
public:
Student(long nno,char nn="",int score1=0,Student next=NULL)
{
name=new char[strlen(nn)+1];
no=nno;score=score1;strcpy(name,nn);link=next
}
~Student(){delete []name;}
};
class lnkstu //学生链表类
{
Student head;//链表头指针
public:
lnkstu()
{
head=new Student;//头指针指向头结点,头结点中不存放数据,只是利用其后继指针;
}
Student
Locate(int i);//在head为头指针的单链表中查找第i个结点,返回指向其的指针;
long Locate(char nn)//在head为头指针的单链表中查找姓名为nn的结点,返回其学号;
~lnkstu();//释放单链表
void DeleteList(int i);//删除第i个结点
void InsertList(int i);//在第i个位置插入一条学生信息
void Display();//显示单链表中学生信息
};
void main()
{
lnkstu s;
…//通过调用成员函数,建立学生信息单链表,并在其中删除、查找相应的学生结点,最后显示完整链表信息
}

1. Experimental procedure

#include<iostream>
#include<cstring>
#pragma warning(disable : 4996)
//vs中对strcpy的使用有安全限制,故此语句关闭该警告
using namespace std;
class lnkstu;
class Student//学生结点类
{
    long no;
    char* name;
    int score;
    Student* link;
public:
    Student(long nno=0, const char* nn = "None", int score1 = 0, Student* next = NULL)
    {
        name = new char[strlen(nn) + 1];
        no = nno; score = score1; strcpy(name, nn); link = next;
    }
    ~Student() { delete[]name;}
    friend lnkstu;//将链表类设为学生类的友元类方便书写链表操作函数;
};

class lnkstu  //学生链表类
{
    Student* head;//链表头指针
public:
    lnkstu()
    {
        head = new Student;//头指针指向头结点,头结点中不存放数据,只是利用其后继指针;
    }
    Student* Locate(int i);//在head为头指针的单链表中查找第i个结点,返回指向其的指针;
    long Locate(char* nn);//在head为头指针的单链表中查找姓名为nn的结点,返回其学号;  
        ~lnkstu();//释放单链表
    void DeleteList(int i);//删除第i个结点
    void InsertList(int i);//在第i个位置插入一条学生信息
    void Display();//显示单链表中学生信息 
    
};
lnkstu::~lnkstu() //定义链表类析构函数
{
    Student* s = head;
    while (head != NULL) {
        head = head->link;
        delete s;
        s = head;
    }cout << "Release linked list end.\n";
}
Student* lnkstu::Locate(int i) 
{
    Student* current = head;
    int num = 0;
    while (current != NULL && num < i) {
        current=current->link;
        num++;
    }if (current == NULL)cout << "The number of " << i << " Student doesn's exist.\n";
    return current;
}
long lnkstu::Locate(char* nn) {
    Student* t = head;
    while (t != NULL && strcmp(t->name, nn) != 0) {
        t = t->link;
    }if (t == NULL)//如果学生中没有姓名是nn的则输出查找失败并返回0
    {
        cout << "Search failed.\n";
        return 0;
    }
    else return t->no;
}void lnkstu::DeleteList(int i) {
    Student* current = head,*aft,*t;
    current=Locate(i-1);//要删除i节点需要定位到i-1节点处
    t = current -> link;
    aft = t->link;
    delete t;
    current->link = aft;
    cout << "Delete end.\n";
}void lnkstu::InsertList(int i) {
    Student* current = head,*aft;
    current = Locate(i - 1);//要插入到第i节点需定位到第i-1节点处
    aft = current->link;
    cout << "Please input the student information to be inserted: student number, name, score: ";
    Student* newstu = new Student;
    cin >> newstu->no >> newstu->name >> newstu->score;
    current->link = newstu;
    newstu->link = aft;
    cout << "Insert end.\n";
}void lnkstu::Display() {
    Student* s = head->link;
    while (s != NULL) {
        cout << "Student number: " << s->no
            << " name: " << s->name
            << " score: " << s->score << endl;
        s = s->link;
    }
}
int main()
{
    lnkstu  s;
    //通过调用成员函数,建立学生信息单链表,并在其中删除、查找相应的学生结点,最后显示完整链表信息
    int n,i;
    cout << "Please input the total number of student: ";
    cin >> n;
    for (i = 1; i <= n; i++) {
        s.InsertList(i);
    }//完成链表创建
    s.Display();
    cout << "Next, test the linked list operation function.\n";
    cout << "Please input the number of link you want to delete in linked list:";
    cin >> i;
    s.DeleteList(i);
    cout << "Please input the number of the place you want to add in linked list:"; 
    cin >> i;
    s.InsertList(i);
    cout << endl;
    s.Display();
}

2. Experimental results and analysis

在这里插入图片描述
In the vs debugging process, because of the security problem of strcpy() function, I can’t compile and run it all the time, so I added a line to ignore the warning code in the file; finally, the test results are all normal.
在vs调试过程中,由于strcpy()函数的安全问题一直不能编译运行故我加了一行忽略文件内警告的代码;最后测试结果一切正常。

3. Experiment process analysis

During the experiment, I review the knowledge of linked list, pointer and dynamic allocation better. As well as the default parameter settings in the constructor parameter list, the destructor needs to release the memory allocated dynamically with new
实验过程中更好的回顾了相关链表,指针和有关动态分配的知识。以及构造函数参数列表中对默认参数的设置,析构函数中需释放前面用new动态分配的内存等。
Also, I set the linked list class as the friend class of the student class to facilitate the member functions in the linked list class to operate the data members. That is to say, the member functions in a friend class are all friend functions.
还有我将链表类设置成了学生类的友元类以方便链表类中的成员函数来操作数据成员。即友元类中的成员函数都是友元函数。

4. Empirial conclusion

This experiment let me know many advantages of class, for example, using class to create a linked list has better encapsulation for data than using structure to create a linked list, and can define the functions of the linked list operation in the class body to make the whole program modular.
本次实验让我了解到类的诸多优点,例如用类来创建链表相对用结构体来创建链表来说对数据有着更好的封装性,以及可以将对链表操作的函数放在类体内定义成类方法使得整个程序模块化。

By-----Suki 2020 4 14
std::cout<<“Good night, babys.”<<endl;

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值