西北农林科技大学2024学年C++面向对象程序设计OJ——T10 单链表类的继承与派生

一.题目描述

单链表类的继承与派生

Description

设计单链表类,并基于单链表类实现栈类和队列类:

(1)设计学生信息类StudentRecord,要求包含公有数据成员:string stuName和int stuNo,设计用于输出学生信息的公有成员函数:void Print(),输出格式为: stuName stuNo。

(2)基于STL中的<list>,设计学生链表类LinkedList,私有数据成员定义为:list<StudentRecord> lst,要求基于list实现头插入、头删除、尾插入、尾删除、遍历输出所有学生信息等公有成员函数。(参考:cplusplus.com/reference/list/list/)

(3)由LinkedList派生LinkedStack类,基于单链表类的功能实现压栈和出栈的成员函数:void Push(const StudentRecord &record)和boolPop(StudentRecord &record)。

(4)由LinkedList派生LinkedQueue类,基于单链表类的功能实现入队和出队的成员函数:void EnQueue(const StudentRecord &record)和bool DeQueue(StudentRecord &record)。

在main函数中:

定义一个LinkedQueue类的对象queue和一个LinkedStack类的对象stack,并根据用户的输入分别对queue和stack作出相应的操作。若为"Push",则压栈;若为"EnQueue",则入队;若为"Pop",则出栈;若为"DeQueue",则出队;若为"Exit",则退出,并分别输出堆栈和队列中的所有元素;若为其它字符串,则给出提示信息"Input error!"。入栈和入队时,输入学生姓名和学号。出栈和出队时,若非空,则输出被删除的学生信息;若栈空,则输出Stack is empty!";若队空,则输出"Queue is empty!"。

Input

操作名;学生姓名,学号。

Output

删除信息;提示信息。

二.输入与输出

Sample Input 1 

Push
ZhangSan 200905
Push
LiSi 200906
Push
WangWu 200908
EnQueue
LiKui 200915
EnQueue
ZhenShiyin 200916
EnQueue
ZhanGuang 200917
EnQueue
ChengRixing 200918
EnQueue
HuLai 200919
Dequeue
Pop
exit
Exit

Sample Output 1

Input error!
WangWu 200908
Input error!
LiSi 200906
ZhangSan 200905
LiKui 200915
ZhenShiyin 200916
ZhanGuang 200917
ChengRixing 200918
HuLai 200919


三.代码

#include <iostream>
#include <list>
#include <string>
using namespace std;
class StudentRecord {
public:
    string stuName;
    int stuNo;

    void Print() {
        cout << stuName << " " << stuNo << endl;
    }
};
class LinkedList {
protected:
    list<StudentRecord> lst;

public:
    void HeadInsert(const StudentRecord& record) {
        lst.push_front(record);
    }

    void HeadDelete() {
        if (!lst.empty()) {
            lst.pop_front();
        }
    }

    void TailInsert(const StudentRecord& record) {
        lst.push_back(record);
    }

    void TailDelete() {
        if (!lst.empty()) {
            lst.pop_back();
        }
    }

    void PrintAll() {
        for ( auto& student : lst) {
            student.Print();
        }
    }
};
class LinkedStack : public LinkedList {
public:
    void Push(const StudentRecord& record) {
        HeadInsert(record);
    }

    bool Pop(StudentRecord& record) {
        if (!lst.empty()) {
            record = lst.front();
            HeadDelete();
            return true;
        }
        else {
            cout << "Stack is empty!" << endl;
            return false;
        }
    }
};
class LinkedQueue : public LinkedList {
public:
    void EnQueue(const StudentRecord& record) {
        TailInsert(record);
    }

    bool DeQueue(StudentRecord& record) {
        if (!lst.empty()) {
            record = lst.front();
            HeadDelete();
            return true;
        }
        else {
            cout << "Queue is empty!" << endl;
            return false;
        }
    }
};
int main() {
    LinkedStack stack;
    LinkedQueue queue;
    string operation;
    string name;
    int number;

    while (true) {
        cin >> operation;
        if (operation == "Push") {
            cin >> name >> number;
            StudentRecord record = { name, number };
            stack.Push(record);
        }
        else if (operation == "EnQueue") {
            cin >> name >> number;
            StudentRecord record = { name, number };
            queue.EnQueue(record);
        }
        else if (operation == "Pop") {
            StudentRecord record;
            if (stack.Pop(record)) {
                record.Print();
            }
        }
        else if (operation == "DeQueue") {
            StudentRecord record;
            if (queue.DeQueue(record)) {
                record.Print();
            }
        }
        else if (operation == "Exit") {
            stack.PrintAll();
            queue.PrintAll();
            break;
        }
        else {
            cout << "Input error!" << endl;
        }
    }

    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为何出现在彼此生活又离开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值