Basic operation of dual stack

Basic operation of dual stack

describe

Store the two stacks numbered 0 and 1 in an array space V[m], and the bottom of the stack is at both ends of the array. When the stack top pointer top[0] of stack No. 0 is equal to - 1, the stack is empty; When the stack top pointer top[1] of stack 1 is equal to m, the stack is empty. Both stacks grow from both ends to the middle (see figure below). Try to write the function of double stack initialization to judge whether the stack is empty, full, in and out of the stack algorithm. The order of function calls is: stack entry, stack full judgment, stack exit, stack empty judgment.

The definition of dual stack data structure is as follows:

typedef struct

{

int top[2], bot[2]; // Stack top and bottom pointers

SElemType *V; // Stack array

int m; // Maximum number of elements the stack can hold

}DblStack;

[external link picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-ucmb1wzq-1636560782609)( http://acm.bjfu.edu.cn/acmhome/upload/image/20161104175533032.png )]

Representation of dual stack structure

input

Multiple groups of data, each group of data has four rows, and the data in each row is separated by spaces. The first line is an integer m, which represents the size of array V. the second line is four integers E0, E1, d0, d1, E0 and E1, which represent the lengths of the Integer Sequences E0 and E1 pressed into stack 0 and stack 1 respectively (in the case of sequential stacking without stacking in the middle), and d0 and d1 represent the lengths of the sequences ejected from stack 0 and stack 1 respectively (in the case of sequential stacking without stacking in the middle). The third and fourth lines represent sequences E0 and E1, respectively. When m=0, the input ends.

output

Multiple groups of data, each group of data has four rows, and the data in each row is separated by spaces. The first line is an integer m, which represents the size of array V. the second line is four integers E0, E1, d0, d1, E0 and E1, which represent the lengths of the Integer Sequences E0 and E1 pressed into stack 0 and stack 1 respectively (in the case of sequential stacking without stacking in the middle), and d0 and d1 represent the lengths of the sequences ejected from stack 0 and stack 1 respectively (in the case of sequential stacking without stacking in the middle). The third and fourth lines represent sequences E0 and E1, respectively. When m=0, the input ends.

7
3 4 2 2
1 2 3
2 3 4 5
12
4 6 4 3
1 3 4 5
1 3 5 6 8 1
0
1
3 2 1
5 4 1
0
5 4 3 1 0
1 8 6 1
AC code
// #include <iostream>

// #define MAX 100

// using namespace std;

// typedef struct
// {
//     int *top[2];
//     int *base[2];
//     int maxSize;
// }dStack;

// void initStack (dStack &s, int n)
// {
//     s.base[0] = new int [MAX];
//     s.base[1] = s.base[0] + n - 1;
//     s.top[0] = s.base[0];
//     s.top[1] = s.base[0];

//     s.maxSize = n;
// }

// bool is_full(int *top0, int *top1)
// {
//     if (top0 > top1)
//         return true;
//     else
//         return false;
// }

// bool is_empty0(int *top0, int *base0)
// {
//     if (top0 == base0)
//         return true;
//     else
//         return false;
// }

// bool is_empty1(int *top1, int *base1)
// {
//     if (top1 == base1)
//         return true;
//     else
//         return false;
// }

// void push0(dStack &s, int e)
// {
//     *(s.top[0] ++) = e;
// }

// void push1(dStack &s, int e)
// {
//     *(s.top[1] --) = e;
// }

// void pop0(dStack &s)
// {
//     s.top[0] --;
//     cout << *(s.top[0]) << " ";
// }

// void pop1(dStack &s)
// {
//     s.top[1] ++;
//     cout << *(s.top[1]) << " ";
// }

// int main()
// {
//     int n;
//     int e0, e1, d0, d1;
//     while (cin >> n, n)
//     {
//         int cnt = 0;
//         dStack s;
//         initStack (s, n);

//         cin >> e0 >> e1 >> d0 >> d1;
//         while (e0 --)
//         {
//             int e;
//             cin >> e;
//             push0(s, e);
//             cnt ++;
//         }
//         while (e1 --)
//         {
//             int e;
//             cin >> e;
//             push1(s, e);
//             cnt ++;
//         }

//         // cout << is_ full(s.top[0], s.top[1]) << endl;//  Use two top pointers to judge
//         if (cnt >= n)
//             cout << 1 << endl;
//         else
//             cout << 0 << endl;
//         while (d0 --)
//             pop0(s);                                //  Similarly, deletion and insertion are directly passed into the whole stack
//         cout << ! is_ empty0(s.top[0], s.base[0]) << endl; //  To judge null, you only need to pass in the pointer on one side
//         while (d1 --)
//             pop1(s);
//         cout << !is_empty1(s.top[1], s.base[1]) << endl;
//     }

//     return 0;
// }

// I don't know why the above code is always WA. I'll check it again when I have time

#include <iostream>

#define MAX 100

using namespace std;

typedef struct LNode
{
int *top[2];
int *base[2];
int maxSize;
}DblStack;

void initStack(DblStack &s, int n)
{
s.base[0] = new int [MAX];
s.base[1] = s.base[0] + n - 1;          // Because the base of the second stack is at the far right end
s.top[0] = s.base[0];
s.top[1] = s.base[1];
s.maxSize = MAX;
}

bool isEmpty0(int *base, int *top)          // Pass in two pointers to the first stack
{
if (base == top)
return true;
else
return false;
}

bool isEmpty1(int *base, int *top)          // Pass in two pointers to the second stack
{
if (base == top)
return true;
else
return false;
}

bool isFull(int *top0, int *top1)
{
if (top0 > top1)
return true;
else
return false;
}

void push0(DblStack &s, int e)
{
*(s.top[0] ++) = e;                     // Since the top pointer always points to the next bit of data in the stack operation, it needs to be subtracted here
}

void push1(DblStack &s, int e)
{
*(s.top[1] --) = e;
}

void pop0(DblStack &s)
{
s.top[0] --;
cout << *(s.top[0]) << " ";
}

void pop1(DblStack &s)
{
s.top[1] ++;
cout << *(s.top[1]) << " ";
}

int main()
{
int n;
while (cin >> n, n)
{
DblStack s;
initStack(s, n);

int e0, e1, d0, d1;
cin >> e0 >> e1 >> d0 >> d1;
while (e0 --)
{
int e;
cin >> e;
push0(s, e);
}

while (e1 --)
{
int e;
cin >> e;
push1(s, e);
}

cout << isFull(s.top[0], s.top[1]) << endl;

while (d0 --)
pop0(s);

cout << !isEmpty0(s.base[0], s.top[0]) << endl;

while (d1 --)
pop1(s);

cout << !isEmpty1(s.base[1], s.top[1]) << endl;
}

return 0;
}
while (d0 --)
pop0(s);

cout << !isEmpty0(s.base[0], s.top[0]) << endl;

while (d1 --)
pop1(s);

cout << !isEmpty1(s.base[1], s.top[1]) << endl;
}

return 0;

}

Tags: C++ data structure

Posted on Thu, 11 Nov 2021 01:58:11 -0500 by ready2drum