当前位置:网站首页>Western Agricultural University C plus
Western Agricultural University C plus
2022-07-22 06:19:00 【numb and dying】
problem H: oop Internship -8. Inheritance and derivation _ Linked list
Title Description
Design single linked list class , And based on the single linked list class to achieve stack class and queue class :
(1) Design student information StudentRecord, It is required to include public data members :string stuName and int stuNo, Public member functions designed to output student information :void print, The output format is :Name: stuName, Number: stuNo.
(2) Design the node class of students' linked list StudentNode, It is required to include public data members :StudentRecord data and StudentNode *next.
(3) Design student linked list class LinkedList, It is required to define header insertion 、 Head delete 、 Tail insertion 、 Traverse and other public member functions
.
(4) from LinkedList The derived LinkedStack class , Based on the function of single linked list class, realize the member functions of pressing stack and pushing stack :void Push(StudentRecord record) and bool Pop(StudentRecord &record).
(5) from LinkedList The derived LinkedQueue class , Based on the function of single linked list class, the member functions of entering and leaving the team are realized :void EnQueue(StudentRecord record) and bool DeQueue(StudentRecord &record).
stay main Function :
Define a LinkedQueue Class object queue And a LinkedStack Class object stack, And according to the input of the user queue and stack Take corresponding actions . if "Push", Then press the stack ; if "EnQueue", Then join the team ; if "Pop", Out of the stack ; if "DeQueue", Out of the line ; if "Exit", The exit ; In case of others , Then the prompt message is given "Input error!". When entering the stack and joining the team , Enter student name and student number . When leaving the stack and the team , If not empty , Then output the deleted student information ; If the stack is empty , The output Stack is empty!"; If the team is empty , The output "Queue is empty!".
Input
Operation name ; The student's name , Student number .
Output
Deleted information ; Prompt information .
The sample input
Sample output
Tips
#include <iostream>
#include <string>
using namespace std;
class StudentRecord{// Ying ︾ Generate data ?
private:
string stuName;
int stuNo;
public:
void print()
{
cout<<"Name: "<<stuName<<", Number: "<<stuNo<<endl;
}
StudentRecord& operator=(const StudentRecord &stu)// Don't read the Buddha
{
stuName=stu.stuName;
stuNo=stu.stuNo;
return *this;
}
void setData(string str,int no)// Bi bin € And operate ?
{
stuName=str;
stuNo=no;
}
friend class LinkedList;
};
class StudentNode{// Contact ?
public:
StudentRecord data;
StudentNode *next;
StudentNode()
{
next=NULL;
}
};
class LinkedList{// Click "yes ㄧ By
protected:
StudentNode *head,*tail;
public:
LinkedList()// To create a deferred payment 〃
{
head=tail=new StudentNode;
head->next=NULL;
(head->data).setData("head_node",0);
}
~LinkedList()// To delete 〃
{
StudentNode *temp;
while(head!=tail)
{
temp=head;
head=head->next;
delete temp;
}
delete head;
}
void headAdd(StudentNode &node)// It's too late ョ End shackles ?
{
node.next=head;
head=&node;
}
bool deleteNode()// From ご To delete € Jujube End shackles ?
{
if(empty()){
return false;// To delete a connection
}
StudentNode *temp;
if((head->data).stuName=="head_node"){// If yes Boil ?
if((head->next)->next==NULL){// If yes There is a € Jujube End shackles ? Let's go ゅご Contact )
tail=head;
temp=head->next;
head->next=NULL;
delete temp;
return true;// Deletion succeeded
}
else{// If more than 1 Jujube End shackles ? Let's go ゅご Contact )
temp=head->next;
head->next=(head->next)->next;
delete temp;
return true;// Deletion succeeded
}
}
else{// If yes ご Pick up ?
temp=head;
head=head->next;
delete temp;
return true;// Deletion succeeded
}
}
void tailAdd(StudentNode &node)// Torch switch ョ End shackles ?
{
tail->next=&node;
tail=&node;
}
bool empty()// ð ゆ Breaking threshold 〃 Yes A rainbow ┖
{
if(head==tail){
return true;
}
else return false;
}
bool getData(StudentRecord &record)// Luan Feng Yao yuan 〃 Jujube Jujube € Jujube Data ?
{
if(empty()){
return false;// Luan Feng's head touches
}
if((head->data).stuName=="head_node"){
record=(head->next)->data;
return true;// Luan Feng succeeded
}
else{
record=head->data;
return true;// Luan Feng succeeded
}
}
};
class LinkedStack:protected LinkedList{// Click "yes ㄦ Chundian Yiling ?
public:
void Push(StudentRecord record)
{
StudentNode *temp=new StudentNode;
temp->data=record;
headAdd(*temp);// A kind of ユ Rush ご Insert
}
bool Pop(StudentRecord &record)
{
if(!getData(record)){// Save it Exit group point data
return false;// Screw ┖ There is no way to take action ?
}
else{// The screws are not tight ?
deleteNode();// Out of the oven
return true;// The baking is successful
}
}
};
class LinkedQueue:protected LinkedList{// Click "yes ㄦ Chundian queue ?
public:
void EnQueue(StudentRecord record)
{
StudentNode *temp=new StudentNode;
temp->data=record;
tailAdd(*temp);// A kind of ラ The queue is waiting ?
}
bool DeQueue(StudentRecord &record)
{
if(!getData(record)){// Control ユ It's too late Data of discharge queue points
return false;// Vacuum valve ┖ There is no way to perform the emergency operation ?
}
else{// It doesn't work ?
deleteNode();// Discharge queue
return true;// The flood discharge queue succeeded
}
}
};
int main()
{
LinkedQueue queue;
LinkedStack stack;
//signal A kind of ㄦ To perform the following operations: ame A kind of ㄦ To control ュ Play Ying ︾ Birth name
string signal,name;
//number A kind of ㄦ To control ユ Collect ︾ Sheng Ying ﹀ Buddha
int number;
//save A kind of ㄦ To save the transaction data
StudentRecord save;
cin>>signal;
while(signal!="Exit")
{
// The operation is not to input the screws or ラ Queue
if(signal=="Push"||signal=="EnQueue"){
cin>>name>>number;// Control ユ Collection data
save.setData(name,number);// Get the contact data
if(signal=="Push"){// A kind of ユ Operation
stack.Push(save);// Contact ユ Bang
}
else queue.EnQueue(save);// A kind of ラ When the queue is running, it will enter the valve ?
cin>>signal;// Set the peak next Operate ?
continue;
}
// The operation is not correct or out of the queue
if(signal=="Pop"||signal=="DeQueue"){
if(signal=="Pop"){// Discharge operation
if(stack.Pop(save)){// Successfully exit
save.print();// Print out the contact data
}
else cout<<"Stack is empty!"<<endl;
}
else{// Discharge queue operation
if(queue.DeQueue(save)){// Successfully exit the queue
save.print();// Print the data of the discharge queue
}
else cout<<"Queue is empty!"<<endl;
}
cin>>signal;// Set the peak next Operate ?
continue;
}
// Not necessarily ユ Inlet valve and outlet valve are not outlet valve and outlet valve, but not €€ Out of the rainbow ▼ Well ?
cout<<"Input error!"<<endl;
cin>>signal;
}
return 0;
}
边栏推荐
- LeetCode刷题:反转链表 与 链表中的中间节点
- Exploration of running applet of domestic Tongxin UOS system
- "New capabilities" of rongyun Super Group
- 基于通用单片机(久齐) 半导体制冷制热的控制 手机散热器按摩仪器中冷热头的控制
- 剑指 Offer II 015. 字符串中的所有变位词
- Discussion on killing free exe Technology
- 费曼学习法
- Navicate 连接阿里云(两种方式及原理讲解)
- LeetCode刷题:用栈实现队列 与 字符串解码
- 6.1 malicious code prevention
猜你喜欢
Redis: master-slave replication
第2集 vmware虚拟机安装最牛B教程(13天)
NETCORE - Middleware (1)
使用Lingo求解简单的线性规划问题
Idea connects to MySQL database
Wechat applet_ 19. Custom components -behaviors
LeetCode刷题:对称二叉树与二叉树的最大深度
leetcode 310. 最小高度树
Discussion on passing the d-shield PHP webshell without killing horses
Analysis on the characteristics of two-layer industrial switch
随机推荐
vector容器成员函数——reserve()及迭代器失效问题
while和do-while循环
Goldfish rhca memoirs: cl210 Integrated Identity Management - project organization management + chapter experiment
金鱼哥RHCA回忆录:CL210集成身份管理--项目组织管理+章节实验
软件测试:等价类测试与基于判定表测试及综合题解析
Wechat applet_ 19. Custom components -behaviors
Typora Beta版过期解决
Sword finger offer II 099 Sum of minimum paths
DOM之事件代理(二)
Selenium common practical function Guide
64. Minimum path and
NETCORE - Middleware (1)
网络连接工具大全
【读书会第13期】+第三章 视频文件的编码格式
[book club issue 13] + Chapter III coding format of video files
leetcode 310. Minimum height tree
leetcode 1380. 矩阵中的幸运数
C#多线程和异步(二)——Task和async/await详解
C multithreading and asynchronism (II) -- detailed explanation of task and async/await
基于通用单片机(久齐) 半导体制冷制热的控制 手机散热器按摩仪器中冷热头的控制