C++&QT day6

1> 将之前定义的栈类和队列类都实现成模板类

栈:

#include 
#define MAX 128
using namespace std;
template
class Stack_s
{
private:
    T *p=new T[MAX];//栈的数组
    int top;//记录栈顶的变量
public:
    //构造函数
    Stack_s(int t=-1)
    {
        top=t;
        cout<<"无参构造函数"<
int Stack_s::stack_push(T e)
{
    if(stack_full())
    {
        cout<<"入栈失败"<
int Stack_s::stack_pop()
{
    if(stack_empty())
    {
        cout<<"出栈失败"<
int Stack_s::stack_delete()
{
    while(top!=-1)
    {
        stack_pop();
    }
    delete [] p;
    p=nullptr;
    cout<<"清空栈成功"<
bool Stack_s::stack_empty()
{
    if(top==-1)
    {
        cout<<"栈空"<
bool Stack_s::stack_full()
{
    if(top==MAX-1)
    {
        cout<<"栈满了"<
int Stack_s::stack_gettop()
{
    cout<<"栈顶元素是:"<
void Stack_s::stack_getsize()
{
     cout<<"栈的大小为:"< s1;
    string e;
    int l;
    s1.stack_empty();
    cout<<"请输入要入栈的个数:";
    cin>>l;
    for(int i=0;i>e;
        s1.stack_push(e);
    }
    s1.stack_gettop();
     s1.stack_getsize();
    for(int i=0;i s2;
    char e1;
    int l1;
    s1.stack_empty();
    cout<<"请输入要入栈的个数:";
    cin>>l1;
    for(int i=0;i>e1;
        s2.stack_push(e1);
    }
    s2.stack_gettop();
     s2.stack_getsize();
    for(int i=0;i

结果:

C++&QT day6_第1张图片

队列:

#include 
#define MAX 128
using namespace std;
template
class Queue_q
{
private:
    T *p=new T[MAX];//队列的数组
    int tail;//记录队尾元素
    int head;//记录对头元素
public:
    //构造函数
    Queue_q(int t=0)
    {
        head=t;
        tail=t;
        cout<<"无参构造函数"<
int Queue_q::queue_push(T e)
{
    if(queue_full())
    {
        cout<<"入队失败"<
int Queue_q::queue_pop()
{
    if(queue_empty())
    {
        cout<<"出队失败"<
int Queue_q::queue_delete()
{
    while(head!=tail)
    {
        queue_pop();
    }
    delete [] p;
    p=nullptr;
    cout<<"清空队列成功"<
bool Queue_q::queue_empty()
{
    if(head==tail)
    {
        cout<<"队列空"<
bool Queue_q::queue_full()
{
    if((tail+1)==0)
    {
        cout<<"队列满了"<
void Queue_q::queue_getsize()
{
    int size;
    size=(tail-head+MAX)%MAX;
    cout<<"队的大小为:"< q1;
    double e;
    int s;
    q1.queue_empty();
    cout<<"请输入要入队的个数:";
    cin>>s;
    for(int i=0;i>e;
        q1.queue_push(e);
    }
     q1.queue_getsize();
    for(int i=0;i q2;
    string e1;
    int s2;
    q2.queue_empty();
    cout<<"请输入要入队的个数:";
    cin>>s2;
    for(int j=0;j>e1;
        q2.queue_push(e1);
    }
     q2.queue_getsize();
    for(int j=0;j

结果:

C++&QT day6_第2张图片

思维导图:

你可能感兴趣的:(c++,qt,开发语言)