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<string>
#include<list>
#include<sstream>
using namespace std;
class StudentRecord
{
public:
string stuname;
int stuno;
void set(string a, int b)
{
stuname = a;
stuno = b;
}
void Print()
{
cout << stuname << " " << stuno << endl;
}
};
class LinkedList
{
protected: list<StudentRecord>lst;
public:
void push_front(const StudentRecord& a)
{
lst.push_front(a);
}
void push_back(const StudentRecord& a)
{
lst.push_back(a);
}
void pop_front()
{
lst.pop_front();
}
void pop_back()
{
lst.pop_back();
}
};
class LinkedStack:virtual public LinkedList
{
public:
bool empty()
{
return lst.empty();
}
void out()
{
if (!lst.empty()) {
list<StudentRecord>::iterator it = --(lst.end());
cout << (*it).stuname << " " << (*it).stuno << endl;
}
}
void foreachout()
{
if (!empty())
{
list<StudentRecord>::iterator it = --(lst.end());
for (; it != (lst.begin()); it--)
{
(*it).Print();
}
(*it).Print();
}
}
};
class LinkedQueue:virtual public LinkedList
{
public:
bool empty()
{
return lst.empty();
}
void out()
{
if (!empty()) {
list<StudentRecord>::iterator it = lst.begin();
cout << (*it).stuname << " " << (*it).stuno << endl;
}
}
void foreachout()
{
if (!empty())
for (list<StudentRecord>::iterator it = lst.begin(); it != lst.end(); it++)
{
(*it).Print();
}
}
};
int main()
{
LinkedQueue queue;
LinkedStack stack;
StudentRecord x;
string input;
string a, b;
int c;
while (1) {
cin >> a;
if (a == "Exit")
{
stack.foreachout();
queue.foreachout();
break;
}
if (a == "Push")
{
cin >> b >> c;
x.set(b, c);
stack.push_back(x);
}
else if (a == "EnQueue")
{
cin >> b >> c;
x.set(b, c);
queue.push_back(x);
}
else if (a == "Pop")
{
if (stack.empty())
cout << "Stack is empty!" << endl;
else {
stack.out();
stack.pop_back();
}
}
else if (a == "DeQueue")
{
if (queue.empty())
cout << "Queue is empty!" << endl;
else {
queue.out();
queue.pop_front();
}
}
else {
cout << "Input error!" << endl;
}
}
return 0;
}