【数据结构】(三)栈和队列

Stack 栈:先进后出

1 栈

栈(stack)是限制对元素的插入(push)和删除(pop)只能在一个位置上进行的表,该位置是表的末端,叫做栈的栈顶(top)

在这里插入图片描述
在这里插入图片描述

1.1 顺序栈类实现

top = 0,即elem[0] 不存储数据

template<class ItemType>
class SqStack
{
private:
    int top;                //栈顶指针
    int maxSize = 100;      //最大容量
    ItemType elem[100]; //一维数组


public:
    SqStack() {top = 0;}
    ~SqStack() {delete[] this->elem;}

    void setEmpty() {top = 0;}  //置为空
    bool isEmpty()              //判断是否为空
    {return (this->top == 0 ? true : false); }

    void push(ItemType e);      //入栈
    ItemType pop();             //出栈
    void printOut();            //打印
};

template<class ItemType>
void SqStack<ItemType>::printOut()
{
    if(this->top == 0) cout << "is Empty." << endl;
    else
    {
        for(int k = this->top; k > 0; k--)
            cout << "the elem[" << k << "] is : " << elem[k] << endl;
    }
}


template<class ItemType>
void SqStack<ItemType>::push(ItemType e)
{
    if(top == this->maxSize-1)
    {
        cout << "\n this Stack is full." << endl;
    }
    else
    {
        this->top++;            //top=0 栈底不存储元素
        this->elem[top] = e;
    }
}
template<class ItemType>
ItemType SqStack<ItemType>::pop()
{
    if(this->top == 0)
        throw "is Empty.";

    ItemType e = this->elem[this->top];
    this->top--;
    return e;
}


//test
SqStack<int> *stack = new SqStack<int>();
int e;



for(int i = 0; i < 3; i++)
{
    cout << "Input num : "; cin >> e;
    stack->push(e);
}
stack->printOut();

cout << stack->pop() << endl;
stack->printOut();

1.2 链表栈类实现

栈底不存储数据

链表栈

struct LSNode
{
    int data;
    LSNode *next;
};

class LSStack
{
private:
    LSNode *top; //头指针

public:
    LSStack();
    ~LSStack();

    void setEmpty(); //置为空
    bool isEmpty();  //判断为空
    void display();  //显示
    void push(int e);//入栈
    int pop();       //出栈
    int length();    //长度
};

LSStack::LSStack() {top = NULL;}
LSStack::~LSStack() {this->setEmpty();}

void LSStack::setEmpty()
{
    if(this->top != NULL)
    {
        LSNode *p, *q;
        p = this->top;
        q = p->next;

        while (p != NULL)
        {
            delete p;
            p = q;

            if(q != NULL) q = p->next;
        }
        this->top = NULL;
    }
}

int LSStack::length()
{
    LSNode *p = this->top;
    int count = 0;
    while (p != NULL)
    {
        count++;
        p = p->next;
    }

    return count;
}

bool LSStack::isEmpty()
{
    return this->length() == 0 ? true : false;
}

void LSStack::display()
{
    LSNode *p = this->top;
    while (p != NULL)
    {
        cout << "data : " << p->data << endl;
        p = p->next;
    }
}

void LSStack::push(int e)
{
    LSNode *newNode = new LSNode;
    newNode->data = e;
    newNode->next = this->top;

    this->top = newNode;
}

int LSStack::pop()
{
    if(this->isEmpty())
        throw "lsStack is empty.";

    LSNode *p = this->top;
    this->top = this->top->next;

    int data = p->data;
    delete p;

    return data;
}


//test
LSStack *lss = new LSStack();
int e;

cout << "empty ? : " << lss->isEmpty() << endl;

for(int i = 0;i < 3; i++)
{
    cout << "Input num : "; cin >> e;
    lss->push(e);
}

lss->display();

cout << "length : " << lss->length() << endl;
cout << "empty ? : " << lss->isEmpty() << endl;


cout << "pop e :" << lss->pop() << endl;
lss->display();
Queue 队列:先进先出

2 队列

队列只允许在后端(称为rear)进行插入操作,在前端(称为front)进行删除操作。
队列的操作方式和堆栈类似,唯一的区别在于队列只允许新数据在后端进行添加。

在这里插入图片描述

2.1 顺序(循环)队列

一开始:front = rear = -1
空队列条件:front == rear
满队列条件:(rear + 1) % MaxSize == front

插入:rear = (rear + 1) % MaxSize
删除:front = (front + 1) % MaxSize

在这里插入图片描述
在这里插入图片描述

const int MaxSize = 6; // need a const
class SeQueue
{
private:
    int elem[MaxSize];
    int front, rear; //头指针,尾指针

public:
    SeQueue();
    ~SeQueue();

    bool isEmpty();  //判断是否为空
    void display();  //显示
    void addQ(int e);//入队
    int delQ();      //出队
};

SeQueue::SeQueue()
{
    this->front = -1;
    this->rear = -1;
}

bool SeQueue::isEmpty()
{
    return (this->front == this->rear ? true : false);
}

void SeQueue::display()
{
    int p = this->front;
    while ((p % MaxSize) != this->rear)
    {
        p++;
        cout << "elem[" << p << "] : "
             << this->elem[p] << endl;
    }
}


void SeQueue::addQ(int e)
{
    this->rear = (this->rear + 1) % MaxSize;
    this->elem[this->rear] = e;

    cout << "add Success!" << endl;
}

int SeQueue::delQ()
{
    //判断是否为空
    if(this->isEmpty())
        throw "Queue is empty.";

    this->front = (this->front + 1) % MaxSize;

    return this->elem[this->front];
}



//for test
SeQueue *sq = new SeQueue();

cout << sq->isEmpty() << endl;

int e;
for(int i = 0; i < 2; i++)
{
    cout << "Input num : "; cin >> e;
    sq->addQ(e);
}

sq->display();

cout << "************" << endl;

cout << "del : " << sq->delQ() << endl;
cout << "************" << endl;
sq->display();

for(int i = 0; i < 2; i++)
{
    cout << "Input num : "; cin >> e;
    sq->addQ(e);
}

在这里插入图片描述

2.2 链表队列

在这里插入图片描述

struct QueNode
{
    int data;
    QueNode *next;
};


class LsQueue
{
private:
    QueNode *front; //头结点 [出队]
    QueNode *rear;  //尾结点 [入队]

public:
    LsQueue();
    ~LsQueue();

    bool isEmpty();
    void display();
    void addQ(int e);
    int delQ();
};


LsQueue::LsQueue()
{
    this->front = new QueNode;
    this->rear = this->front;
    this->front->next = NULL;
}

bool LsQueue::isEmpty()
{
    return (this->front == this->rear ? true : false);
}

void LsQueue::display()
{
    if(this->isEmpty())
    {
        cout << "LsQueue is empty." << endl;
        return;
    }

    QueNode *p = this->front;
    while (p != this->rear)
    {
        cout << "out data : " << p->next->data << endl;
        p = p->next;
    }
}

void LsQueue::addQ(int e)
{
    QueNode *addE = new QueNode;
    addE->data = e;
    addE->next = NULL;

    this->rear->next = addE;
    this->rear = addE;
    cout << "add success." << endl;
}

int LsQueue::delQ()
{
    if(this->isEmpty())
        throw "LsQueue is empty";

    QueNode *delE = this->front;
    this->front = this->front->next;
    delete delE;

    return this->front->data;
}


// for test
LsQueue *lq = new LsQueue();

cout << "is empty : " << lq->isEmpty() << endl;

int e;
for(int i = 0; i < 3; i++)
{
    cout << "Input num : "; cin >> e;
    lq->addQ(e);
}
lq->display();


cout << "************" << endl;

cout << "del : " << lq->delQ() << endl;
cout << "************" << endl;
lq->display();

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值