#include <iostream> using namespace std; #include<string> #include"math.h" template<class T> class vlist//线性表抽象类 { public: virtual void in(T &e)=0; virtual T out()=0; }; template<class T> class Element//线性表存储元素 { public: T data; Element<T> next; Element(){} }; //在beign和end之间完成SStack和Quene类的定义及实现,其继承vlist /begin/ /end***/ int main() { vlist<int> *intp; vlist<string> *strp; SStack<int> ints; Quene<int> intq; SStack<string> strs; Quene<string> strq; intp=&ints; int t=2; intp->in(t); t=3; intp->in(t); t=4; intp->in(t); for(int i=0;i<3;i++) cout<<intp->out(); intp=&intq; t=2; intp->in(t); t=3; intp->in(t); t=4; intp->in(t); for(int i=0;i<3;i++) cout<<intp->out(); strp=&strs; string str; cout<<endl; for(int i=0;i<3;i++) { cin>>str; strp->in(str); } for(int i=0;i<3;i++) cout<<strp->out()<<" "; cout<<endl; strp=&strq; for(int i=0;i<3;i++) { cin>>str; strp->in(str); } for(int i=0;i<3;i++) cout<<strp->out()<<" "; }补充代码
时间: 2023-12-20 12:03:43 浏览: 22
以下是SStack和Quene类的定义及实现:
```
template<class T>
class SStack: public vlist<T>//栈
{
Element<T> *head;
public:
SStack(){head=new Element<T>();}
void in(T &e){Element<T> *p=new Element<T>();p->next=head->next;head->next=p;p->data=e;}
T out(){Element<T> *p=head->next;T e=p->data;head->next=p->next;delete p;return e;}
};
template<class T>
class Quene: public vlist<T>//队列
{
Element<T> *head,*tail;
public:
Quene(){head=tail=new Element<T>();}
void in(T &e){Element<T> *p=new Element<T>();tail->next=p;tail=p;p->data=e;}
T out(){Element<T> *p=head->next;T e=p->data;head->next=p->next;if(!head->next)tail=head;delete p;return e;}
};
```
相关问题
#include<iostream> #include<cstdio> using namespace std;是什么意思
#include<iostream> #include<cstdio> using namespace std; 是C++中的预处理指令,用于引入头文件和命名空间。
1. #include<iostream> 是引入iostream头文件,其中包含了输入输出流的定义,例如cout和cin等。
2. #include<cstdio> 是引入cstdio头文件,其中包含了C语言标准输入输出函数的定义,例如printf和scanf等。
3. using namespace std; 是使用std命名空间,std是C++标准库的命名空间,其中包含了很多常用的函数和类。
这些预处理指令的作用是为了在程序中能够使用输入输出流和标准库函数,使得程序更加方便和简洁。
#include <iostream> #include <queue> #include <string> using namespace std;对代码进行注释
代码注释如下:
```c++
#include <iostream> // 引入输入输出流库
#include <queue> // 引入队列库
#include <string> // 引入字符串库
using namespace std; // 使用 std 命名空间
// 主函数
int main() {
queue<string> q; // 定义一个字符串队列 q
// 往队列中添加元素
q.push("apple");
q.push("banana");
q.push("orange");
// 输出队列头部元素
cout << q.front() << endl; // 输出:apple
// 弹出队列头部元素
q.pop();
// 输出队列头部元素
cout << q.front() << endl; // 输出:banana
// 输出队列中元素的个数
cout << "The size of the queue is " << q.size() << endl; // 输出:The size of the queue is 2
return 0; // 返回 0 表示程序正常结束
}
```