栈和队列的实现

顺序栈

#include<iostream>
using namespace std;
typedef int SelemType;
typedef struct Stack
{
    SelemType * top;
    SelemType * base;
    int Maxnum;
};
void Create(Stack &S)
{
    S.base = new SelemType[100];
    if(!S.base)
        return;
    S.top = S.base;
    S.Maxnum = 100;
}
void Destroy(Stack &S)
{
    if(!S.base)
        return;
    for(int i=0; i<S.Maxnum; i++)
    {
        delete S.base;
        S.base++;
    }
    S.base = S.top = NULL;
    S.Maxnum = 0;
}
void Clean(Stack &S)
{
    if(!S.base||S.base==S.top)
        return;
    S.top = S.base;
}
bool Empty(Stack &S)
{
    if(S.base == S.top||!S.base)
    {
        return true;
    }
    else
    {
        return false;
    }
}
int Length(Stack &S)
{
    if(!S.base)
        return 0;
    return S.top-S.base;
}
SelemType Top(Stack &S)
{
    if(Empty(S)||!S.base)
        return -1;
    return * (S.top-1);
}
void Insert(Stack &S,SelemType e)
{
    if(!S.base||Length(S)==S.Maxnum)
        return ;
    *S.top++ = e;
}
SelemType Pop(Stack &S)
{
    if(!S.base||Empty(S))
        return -1;
    return *--S.top;
}
void Last(Stack &S)
{
    if(!S.base||Empty(S))
        return ;
    while(S.top!=S.base)
    {
        cout<<*--S.top<<"\t";
    }
}
int main()
{
    Stack S;
    Create(S);
    Clean(S);
    if(Empty(S))
    {
        for(int i = 1;i<=5;i++)
        {
            Insert(S,i);
        }
    }
    cout<<"Length:"<<Length(S)<<endl;
    cout<<"Top:" <<Top(S)<<endl;
    Pop(S);
    cout<<"Top:" <<Top(S)<<endl;
    Last(S);
    Clean(S);
    cout<<Empty(S);
    Destroy(S);
    cout<<Empty(S);
    cout<<"Length:"<<Length(S)<<endl;
}


我感觉用结构体,来实现栈,太假了。真实的情况,应该是封装在一个类里面的,并且还调用了模板函数一类的东西,但具体是怎么实现的,我也不知道。就自己估摸着,写了一个。

链栈

#include<iostream>
#include <typeinfo>
using namespace std;
typedef int NUMBER;
template<typename T>
class Node
{
private:
    T num;
    Node * S = NULL;
public:
    Node * next;
    void Push(T x);
    void Pop();
};
template<typename T>
void Node<T>::Push(T x)
{
    Node * p = new Node;
    if(!p) return;
    p->num = x;
    p->next = S;
    S = p;
}
template<typename T>
void Node<T>::Pop()
{
    if(this->S == NULL) return;
    cout<<typeid(this->S->num).name()<<"\t"<<this->S->num<<endl;
    this->S = this->S->next;
}
int main()
{
    Node<string>S;
    string x;
    for(int i=0;i<5;i++)
    {
        cin>>x;
        S.Push(x);
    }
    for(int i=0;i<5;i++)
    {
        S.Pop();
    }
    return 0;
}

后来又写了一个队列的

顺序队列

#include<iostream>
#define Maxnum 100
using namespace std;
template<typename T>
class Queue
{
private:
    T * name;
    int Rear;
    int Front;
public:
    Queue();
    void Push(T x);
    void Pop();
};
template<typename T>
Queue<T>::Queue()
{
    this->name = new T[Maxnum];
    if(!this->name)
        return;
    this->Rear = this->Front = 0;
}
template<typename T>
void Queue<T>::Push(T x)
{
    if((this->Rear+1)%Maxnum==this->Front)
        return;
    this->name[this->Rear] = x;
    this->Rear = (++this->Rear)%Maxnum;
}
template<typename T>
void Queue<T>::Pop()
{
    if(this->Front==this->Rear)
        return;
    cout<<this->name[this->Front];
    this->Front = (++this->Front)%Maxnum;
}
int main()
{
    Queue<string> Q;
    string x;
    for(int i=0;i<5;i++)
    {
        cin>>x;
        Q.Push(x);
    }
    for(int i=0;i<5;i++)
    {
        Q.Pop();
    }
}

链队

#include<iostream>
using namespace std;
template<class T>
struct Queue
{
    T data;
    Queue * next;
};
template<typename T>
class Queues
{
private:
    Queue<T> * first;
    Queue<T> * last;
public:
    Queues()
    {
        first = last = new Queue<T>;
    }
    void Push(T x);
    void Pop();
};
template<typename T>
void Queues<T>::Push(T x)
{
    Queue<T> * p = new Queue<T>;
    if(!p) return;
    p->data = x;
    this->last->next = p;
    this->last = p;
}
template<typename T>
void Queues<T>::Pop()
{
    if(this->first == NULL)
        return;
    Queue<T> * p = this->first->next;
    delete this->first;
    cout<<p->data;
    this->first = p;
}
int main()
{
    Queues<char> Q;
    char x[5] ={'a','b','c','d','e'};
    for(int i=0;i<5;i++)
    {
        Q.Push(x[i]);
    }
    for(int i=0;i<5;i++)
    {
        Q.Pop();
    }
    return 0;
}

既然学到了类,就复习一下类的继承相关的知识:
不同的继承方式:

  • public继承: 基类public成员,protected成员,private成员的访问属性在派生类中分别变成:
    public(类内:可访问(直接访问);类外:可访问(直接访问)),protected(类内:可访问(直接访问);类外:可访问(间接访问)),private(类内:可访问(间接访问);类外:可访问(间接的间接访问))
  • protected继承: 基类public成员,protected成员,private成员的访问属性在派生类中分别变成:
    protected(类内:可访问(直接访问);类外:可访问(间接访问)),protected(类内:可访问(直接访问);类外:可访问(间接访问)),private(类内:可访问(间接访问);类外:可访问(间接的间接访问))
  • private继承: 基类public成员,protected成员,private成员的访问属性在派生类中分别变成:
    private(类内:可访问(直接访问);类外:可访问(间接访问)),private(类内:可访问(直接访问);类外:可访问(间接访问)),private(类内:可访问(间接访问);类外:可访问(间接的间接访问))
  • 间接访问: 例如基类中定义共有函数A,A可以调用基类的私有成员;在派生类中定义公有函数B,用来调用A,那么就达到了派生类间接访问基类私有成员的目的。
  • 间接的间接访问: 定义派生类的对象c,c.B这种形式就达到了派生类外间接的间接访问基类私有成员的目的。
总结:

protected继承和private继承一毛一样!

protected成员和private成员有且仅有一个不同:
派生类可直接调用基类的protected成员(无论哪种继承方式),不能直接调用基类的private成员(无论哪种继承方式)。
摘自:
https://blog.csdn.net/weixin_43971764/article/details/87976165

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值