C++ 用数组实现stack,queue

/*intstack.h*/

#include<iostream>

using namespace std;

class Stack
{
private:
    int index;
    int capability;
    int *arr;
public:
    Stack(int cap);
    ~Stack();
    int size();
    void pop( );
    void push(int a);
    bool isfull();
    bool isempty();
    void printStack();

};

/*intstack.cpp*/

#include"intstack.h"
#include<iostream>
//#include<vector>
using namespace std;

Stack::Stack(int cap)
{
    capability=cap;
    arr=new int[capability];
    index=0;
}
Stack::~Stack()
{
    delete arr;
}
int Stack::size()
{
    return index;
}
void Stack::pop()
{
    if(size()==0)
    {cout<<"there is no data";}
    else
        {
            cout<<"the data to pop out is: "<<arr[index-1]<<" "<<endl;
            index=index-1;
        }
    cout<<endl;
}
void Stack::push(int a)
{
    if(index<=(capability-1))
    {
        arr[index]=a;
        index++;
    }
    if(isfull())
        cout<<"you can not push in anymore"<<endl;
}
bool Stack::isfull()
{
    if(index==capability)
        return true;
    else
        return false;
}

void Stack::printStack()
{
    cout<<"the stack now has elements: "<<endl;
    for(int i=0;i<index;i++)
        cout<<arr[i]<<" ";
    cout<<endl;
}

/*main.cpp*/

#include<iostream>
using namespace std;
#include"intstack.h"

void main()
{
    Stack* s=new Stack(4);
    s->printStack();
    s->push(5);
    s->printStack();
    s->push(2);
    s->printStack();
    s->push(4);
    s->printStack();
    s->push(1);
    s->printStack();
    s->isfull();
    s->pop();
    s->printStack();
    s->pop();
    s->printStack();
    system("pause");
}

/*queue.h*/

#ifndef QUEUE_H_
#define QUEUE_H_
class Queue
{
private:
    int *a;
    int front;
    int rear;
    int len;
    int index;
public:
    Queue(int l=10);
    ~Queue();
    void enqueue(int d);
    void dequeue();
    bool isfull();
    bool isempty();
    void show();
};

#endif  QUEUE_H_

/*queue.cpp*/

#include"queue.h"
#include<iostream>
using namespace std;

Queue::Queue(int l)
{
    if(l<1)
        l=10;
    a=new int[l];
    front=0;
    rear=0;
    len=l;
    index=0;
}
Queue::~Queue()
{
    delete[]a;
    front=0;
    rear=0;
    len=0;
    index=0;
}
bool Queue::isempty()
{
    if(index==0)
        return true;
    else
        return false;
}
bool Queue::isfull()
{
    if(index==len)
        return true;
    else
        return false;
}
void Queue::enqueue(int d)
{
    if(isfull()==true)
    {
        cout<<"there is no space"<<endl;
        return;
    }
    index=index+1;
    a[rear]=d;
    rear=index;
    
}
void Queue::dequeue()
{
    if(isempty()==true)
    {
        cout<<"it is a empty queue"<<endl;
        return;
    }
    int data;
    data=a[front];
    index=index-1;
    rear=index;
    for(int i=front;i<=index;i++)
        a[i]=a[i+1];
    cout<<data<<"has been dequeue"<<endl;
    

}
void Queue::show()
{
    for(int i=0;i<len;i++)
        cout<<a[i]<<" ";
    cout<<endl;
}

/*mail.cpp*/

#include"queue.h"

void main()
{
    Queue *q=new Queue(6);
    q->enqueue(3);
    q->enqueue(5);
    q->enqueue(12);
    q->enqueue(1);
    q->enqueue(9);
    q->enqueue(13);
    q->dequeue();
    q->dequeue();
    q->dequeue();
    q->enqueue(6);
    q->enqueue(8);
    q->enqueue(0);
    q->show();
}

/*因为我用的是固定长度的数组,所以出队几个元素也要入队几个元素,初学者,实现的比较繁琐*/





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值