请实现一个队列,既可以存放整数,又可以存放字符串。简单的说,队列是一种数据结构,按照先进先出的顺序管理进、出队列的元素...

请实现一个队列,既可以存放整数,又可以存放字符串。简单的说,队列是一种数据结构 ,按照先进先出的顺序管理进、出队列的元素。本题要求完成:

  (1) 实现描述队列的类Queue,其中定义了队列的大小Size(即队列中可以存放的元素个数),并包括进队列函数Add,出队列函数Delete、显示队列头部元素的函数Head和显示队列尾部元素的函数Tail.

  (2) 定义基类Element,至少包含纯虚函数ShowMe.

  (3) 从基类Element中派生整数类MyInteger和字符串类MyString, 具体实现上述纯虚函数ShowMe,显示该元素的类型和相应的值。

  (4) 重载输入“>>”*作符,使得可以通过cin直接读入上述整数类和字符串类的对象值。

  (5) 编写main函数,测试上述所要求的各种功能,即可以根据菜单命令增加队列元素、删除队列元素、显示队列头部元素和队列尾部元素,其中的元素可以是整数和/或字符串。

  提示:

  虚拟基类Element的定义至少包括以下纯虚函数ShowMe.

class Element
{
// ……
public:
virtual void ShowMe () = 0;
// ……
};
*/

#include <iostream> 
#include <cstring> 
using namespace std;
const int MAXN=1000; 
#define NULL 0

class Element 
{ 
public:
	virtual void ShowMe() = 0; 
}; 

class MyInteger:public Element 
{ 
	int a; 
public: 
	//MyInteger() ;
	friend istream &operator>>(istream &is, MyInteger &MyI); 
	void ShowMe()
	{
		cout << "整形" << a << endl;
	}
}; 
istream &operator>>(istream &is, MyInteger &MyI) 
{ 
	cout<<" 请输入整数:"; 
	is>>MyI.a; 
	return is;
} 

class MyString:public Element 
{ 
	char s[100]; 
public: 
	friend istream &operator>>(istream &is, MyString &MyS); 
	void ShowMe()
	{
		cout << "字符串" << s << endl;
	}

}; 
istream &operator>>(istream &is, MyString &MyS) 
{ 
	cout<<" 请输入字符串:"; 
	is>>MyS.s; 
	return is; 
} 
class Queue 
{ 
	int size; 
	Element *Elm[MAXN]; 
	MyInteger MyI[MAXN]; 
	MyString MyS[MAXN]; 
public: 
	Queue()
	{ 
		for(int i=0; i<MAXN; i++) 
			Elm[i]=NULL; 
		size=-1; 
	} 
	void add(MyInteger &My) 
	{ 
		if (full()) cout<<"队列已满"<<endl; 
		else { 
			MyI[++size]=My; 
			Elm[size]=new MyInteger; 
			Elm[size]=&MyI[size]; 
		} 
	} 
	void add(MyString &My) 
	{ 
		if (full()) cout<<"队列已满"<<endl; 
		else { 
			MyS[++size]=My; 
			Elm[size]=new MyString; 
			Elm[size]=&MyS[size]; 
		} 
	} 

	void tail() 
	{ if(empty()) cout<<"队列为空"<<endl;  
	else{ 
		cout<<" 队列的尾元素为"; 
		Elm[size]->ShowMe(); 
	} 
	} 
	void head() 
	{  
		if(empty()) cout<<"队列为空"<<endl; 
		else{ 
			cout<<" 队列的头元素为"; 
			Elm[0]->ShowMe(); 
		} 
	} 
	void del() 
	{ 
		if(empty()) cout<<"队列为空"<<endl;  
		else{ 
			cout<<" 出队列的元素为"; 
			Elm[size--]->ShowMe();  
		} 
	} 
	bool empty() 

	{ 
		return (bool)(size==-1);  
	} 
	bool full() 
	{ 
		return (bool)(size==MAXN-1); 
	} 
}; 
int main() 
{ 
	MyInteger my1; 
	MyString my2; 
	Queue queue; 
	int s=1; 
	while(s) 
	{ 
		cout<<"Please select 1-6 "<<endl; 
		cout<<" 1: 整数进队列;"<<endl; 
		cout<<" 2: 字符串进队列;"<<endl; 
		cout<<" 3: 显示队列头元素;"<<endl; 
		cout<<" 4: 显示队列尾元素"<<endl; 
		cout<<" 5: 出队列;"<<endl; 
		cout<<" 6: 退出程序"<<endl; 
		cout<<"--------------------------------------"<<endl; 
		cout<<"请选择您的*作:"; 
		cin>>s; 
		switch(s) 
		{ 
		case 1: cin>>my1; queue.add(my1); break; 
		case 2: cin>>my2; queue.add(my2); break; 
		case 3: queue.head(); break; 
		case 4: queue.tail(); break; 
		case 5: queue.del(); break; 
		default: s=0; break; 
		} 
} 
}



转载于:https://my.oschina.net/N3verL4nd/blog/866724

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值