[2023 King's Road Data Structure] [Stack, Queue and Array-page70-05] Complete implementation of C and C++ (can be run directly)

~~~How can the writing style end in a dull way, and the story does not recognize ordinary at the beginning ✌✌✌

If you need the complete code, you can follow the public account below, and reply "code" in the background to get it. Aguang is looking forward to your visit~

Topic:
insert image description here
There are two stacks s1 and s2 that use sequential stacking and share a storage area. In order to maximize the use of space and reduce the possibility of overflow, the storage method in which the top of the stack faces each other and grows head-on can be used. Try to design s1 and s2. Algorithms for pushing and popping the stack.

Problem solving ideas:

>共享栈

Code:

#include <iostream>
using namespace std;
#define MAXSIZE 100

// 单链表定义
typedef struct
{
    int data[MAXSIZE];
    int top1 = -1;
    int top2 = MAXSIZE;
} stk;

void Push(stk &s, int top, int value)
{
    if (top != 1 && top != 2)
    {
        cout << "栈号输入错误" << endl;
        return;
    }

    if (s.top2 - s.top1 == 1)
    {
        cout << "当前栈已满" << endl;
        return;
    }

    if (top == 1)
    {
        s.data[++s.top1] = value;
    }
    else
    {
        s.data[--s.top2] = value;
    }
}

int Pop(stk &s, int top)
{
    if (top != 1 && top != 2)
    {
        cout << "栈号输入错误" << endl;
        return -1;
    }

    if (top == 1)
    {
        if (s.top1 == -1)
        {
            cout << "当前栈已空" << endl;
            return -1;
        }
        else
        {
            return s.data[s.top1--];
        }
    }
    else
    {
        if (s.top2 == MAXSIZE)
        {
            cout << "当前栈已空" << endl;
            return -1;
        }
        else
        {
            return s.data[s.top2++];
        }
    }
}

int main()
{
    stk s;

    Push(s, 1, 2);
    Push(s, 1, 4);
    Push(s, 1, 6);
    Push(s, 2, 3);
    Push(s, 2, 7);

    cout << Pop(s, 1) << endl;
    cout << Pop(s, 2) << endl;
    cout << Pop(s, 2) << endl;
    cout << Pop(s, 2) << endl;
    cout << Pop(s, 1) << endl;
}

Guess you like

Origin blog.csdn.net/m0_47256162/article/details/124439400