链表问题 设计单链表类,并基于单链表类实现栈类和队列类
定义一个LinkedQueue类的对象queue和一个LinkedStack类的对象stack,并根据用户的输入分别对queue和stack作出相应的操作。若为"Push",则压栈;若为"EnQueue",则入队;若为"Pop",则出栈;若为"DeQueue",则出队;若为"Exit",则退出;若为其它,则给出提示信息"Input error!"。入栈和入队时,输入学生姓名和学号。出栈和出队时,若非空,则输出被删除的学生信息;若栈空,则输出Stack is empty!";若队空,则输出"Queue is empty!"我的Pop和DeQueue异常,但是不知道怎样修改
例:输入
Push
ZhangSan 200905
Push
LiSi 200906
EnQueue
WangWu 200907
Pop
exit
Exit
输出
ame: LiSi, Number: 200906
Input error!
以下是我的代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
static int Alternative(string situation);
class StudentRecord
{
public:
string StuName;
int StuNo;
StudentRecord();
StudentRecord(string name,int number);
void print();
};
StudentRecord::StudentRecord()
{
StuName="Hello";
StuNo=2014;
}
StudentRecord::StudentRecord(string name,int number)
{
StuName=name;
StuNo=number;
}
void StudentRecord::print()
{
cout<<"Name:"<<StuName<<", Number:"<<StuNo<<endl;
}
class StudentNode
{
public:
StudentRecord data;
StudentNode *next;
public:
StudentNode();
StudentNode(StudentRecord record);
~StudentNode();
};
StudentNode::StudentNode()
{
this->next=NULL;
}
StudentNode::StudentNode(StudentRecord record)
{
this->data=record;
this->next=NULL;
}
StudentNode::~StudentNode()
{
/*StudentNode *p,*q;
p=next->next;
while(p)
{
q=p;
p=p->next;
delete q;
}*/
}
class LinkedList
{
public:
StudentNode* HeadNode;
public:
LinkedList();
void PushFront(StudentNode* node);
void PopFront();
void PushBack(StudentNode* node);
void PopBack();
void AllList();
};
LinkedList::LinkedList()
{
HeadNode=new StudentNode;
HeadNode->next=NULL;
}
void LinkedList::PushFront(StudentNode* node)
{
node->next=HeadNode->next;
HeadNode->next=node;
}
void LinkedList::PopFront()
{
StudentNode* temp=HeadNode->next;
if(temp==NULL)
{
cout<<"Stack is empty!"<<endl;
}
else
{
temp->data.print();
HeadNode->next=temp->next;
delete temp;
}
}
void LinkedList::PushBack(StudentNode* node)
{
StudentNode* temp=HeadNode;
if(temp)
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=node;
}
else
{
HeadNode->next=node;
}
}
void LinkedList::PopBack()
{
StudentNode* temp=HeadNode;
StudentNode* q=temp;
if(temp->next==NULL)
{
cout<<"Queue is empty!"<<endl;
}
else
{
while(q->next)
{
temp=q;
q=q->next;
}
q->data.print();
temp->next=NULL;
delete q;
}
}
void LinkedList::AllList()
{
StudentNode* temp=HeadNode;
while(temp->next!=NULL)
{
temp->next->data.print();
temp=temp->next;
}
}
class LinkedStack:public LinkedList
{
public:
void Push(StudentRecord record);
void Pop();
};
void LinkedStack::Push(StudentRecord record)
{
StudentNode* temp=new StudentNode;
temp->data.StuName=record.StuName;
temp->data.StuNo=record.StuNo;
temp->next=NULL;
LinkedList::PushFront(temp);
}
void LinkedStack::Pop()
{
LinkedList::PopFront();
}
class LinkedQueue:public LinkedList
{
public:
void EnQueue(StudentRecord record);
void DeQueue();
};
void LinkedQueue::EnQueue(StudentRecord record)
{
StudentNode* temp=new StudentNode;
temp->data.StuName=record.StuName;
temp->data.StuNo=record.StuNo;
temp->next=NULL;
LinkedList::PushBack(temp);
}
void LinkedQueue::DeQueue()
{
LinkedList::PopBack();
}
static int Alternative(string situation)
{
int Alter;
if(situation=="Push")
{
Alter=1;
}
else if(situation=="Pop")
{
Alter=2;
}
else if(situation=="EnQueue")
{
Alter=3;
}
else if(situation=="DeQueue")
{
Alter=4;
}
else if(situation=="Exit")
{
Alter=5;
}
else
{
Alter=6;
}
return Alter;
}
int main()
{
StudentRecord record;
StudentNode node;
LinkedStack stack;
LinkedQueue queue;
string situation;
string name;
int number;
while(1)
{
cin>>situation;
switch(Alternative(situation))
{
case 1:
cin>>name>>number;
record=StudentRecord(name,number);
stack.Push(record);
getchar();
break;
case 2:
stack.Pop();
getchar();
break;
case 3:
cin>>name>>number;
record=StudentRecord(name,number);
queue.EnQueue(record);
getchar();
break;
case 4:
queue.DeQueue();
getchar();
break;
case 5:
exit(0);
break;
case 6:
cout<<"Input error!"<<endl;
break;
}
}
return 0;
}