day46:C++ day6 继承过程中的特殊成员函数、多重继承、虚继承、多态、泛型编程模板

一、栈的模板类

#include 
using namespace std;
#define MAX 50

template
class Stack
{
private:
    T *data;
    int top;
public:
    Stack():data(new T[MAX]),top(-1)
    {
        cout<<"Stack::无参构造函数"< &other)
    {
        if(data!=NULL)
        {
            delete[]data;
        }
        this->data=new T[MAX];
        memcpy(this->data,other.data,sizeof(T)*MAX);
        this->top=other.top;
        cout<<"Stack::拷贝构造函数"<
void Stack::stack_push(T &&val)
{
    this->top++;
    this->data[top]=val;
}

template
void Stack::stack_pop()
{
    if(NULL==this->data || stack_empty())
    {
        cout<<"所给顺序栈不合法"<data[top]<<"出栈成功"<top--;
}

template
void Stack::stack_clear()
{
//    while(!stack_empty(S))
//    {
//        stack_pop(S);
//    }
    this->top=-1;
}

template
bool Stack::stack_empty()
{
    return -1==this->top;
}

template
bool Stack::stack_full()
{
    return MAX-1==this->top;
}

template
T Stack::stack_gettop()
{
    return this->data[top];
}

template
int Stack::stack_len()
{
    return this->top+1;
}

template
void Stack::stack_show()
{
    for(int i=top;i>=0;i--)
    {
        cout<data[i]<<" ";
    }
    cout< S;
    cout<

二、队列的模板类

#include 

#define MAX 50
using namespace std;

template
class Queue
{
private:
    T *data;
    int head;
    int tail;

public:
    Queue():data(new T[MAX]),head(0),tail(0)
    {
        cout<<"queue::无参构造函数"<data=new T[MAX];
        memcpy(this->data,other.data,sizeof(T)*MAX);
        /*
        for(int i=other.head;i!=other.tail;i=(i+1)%MAX)
        {
            data[i]=other.data[i];
        }
        */
        this->head=other.head;
        this->tail=other.tail;
        cout<<"拷贝构造函数"<
void Queue::queue_push(T &&val)
{
    data[tail]=val;
    tail=(tail+1)%MAX;
    return;
}
template
void Queue::queue_pop()
{
    cout<
void Queue::queue_clear()
{
    while(!queue_empty())
    {
        queue_pop();
    }
}
template
bool Queue::queue_empty()
{
    return head==tail;
}
template
bool Queue::queue_full()
{
    return head==(tail+MAX)%MAX;
}
template
int Queue::queue_len()
{
    return (tail+MAX-head)%MAX;
}
template
void Queue::queue_show()
{
    for(int i=head;i!=tail;i=(i+1)%MAX)
    {
        cout< Q;
    Q.queue_empty();
    Q.queue_push(5);
    Q.queue_push(8);
    Q.queue_push(5);
    Q.queue_push(8);
    Queue L;
    L=Q;
    Q.queue_full();
    Q.queue_pop();
    Q.queue_show();
    Q.queue_clear();

    L.queue_show();
    return 0;
}

三、思维导图:有道云笔记

你可能感兴趣的:(c++)