# Data structure of C + + implementation of the two stacks of shared storage space

Source: Internet
Author: User

The array has two endpoints, two stacks have two stack bottom, let one stack bottom of the stack to the beginning of the array, that is, subscript 0, the other stack is the end of the stack, that is, subscript the length of the array

N-1 Office. In this way, if two stacks add elements, the ends point extends toward the middle. When Top1 + 1 = top2 when the stack is full.

Sample code: (Adapted from the "Liar Data Structure")

```#include &lt;iostream&gt; using namespace std;
#define MAXSIZE typedef int ELEMTYPE;
typedef struct {elemtype data[maxsize]; int top1; Stack 1 stack top pointer int top2;

Stack 2 stack top pointer} sqstack;
/* Constructs an empty stack */bool Initstack (Sqstack *sq) {cout &lt;&lt; "Init stack ..." &lt;&lt; Endl; SQ-&GT;TOP1 =-1;

Represents an empty stack sq-&gt;top2 = MAXSIZE;
return true;
/* Set to empty stack/bool Clearstack (Sqstack *sq) {cout &lt;&lt; "clear stack ..." &lt;&lt; Endl;
SQ-&GT;TOP1 =-1;
SQ-&GT;TOP2 = MAXSIZE;
return true;
BOOL Stackempty (Sqstack Sq) {if Sq.top1 = 1 &amp;&amp; sq.top2 = = MAXSIZE) return true;
else return false;
int Stacklength (Sqstack Sq) {cout &lt;&lt; "Stack Length:";
return SQ.TOP1 + 1 + maxsize-sq.top2; /* Returns the top element of the stack/bool GetTop (Sqstack Sq, elemtype *ptr, int stacknum) {if (Stacknum = 1) {if (Sq.top1!=
-1) {*ptr = Sq.data[sq.top1]; cout &lt;&lt; "GeT Top1 Item "&lt;&lt; *ptr &lt;&lt; Endl;
return true;
return false;
else if (Stacknum = = 2) {if (sq.top2!= MAXSIZE) {*ptr = Sq.data[sq.top2];
cout &lt;&lt; "Get Top2 Item" &lt;&lt; *ptr &lt;&lt; Endl;
return true;
return false;
else {cout &lt;&lt; "Stack Num must be 1 or 2!" &lt;&lt; Endl;
return false;  }/* Press stack */bool Push (Sqstack *sq, elemtype Elem, int stacknum) {if (Stacknum = 1) {cout &lt;&lt;
"Push Item to Stack1:" &lt;&lt; Elem &lt;&lt; Endl;
if (sq-&gt;top1 + 1 = sq-&gt;top2)//stack full return false;
SQ-&GT;DATA[++SQ-&GT;TOP1] = Elem;
return true;
else if (Stacknum = 2) {cout &lt;&lt; "Push Item to Stack2:" &lt;&lt; Elem &lt;&lt; Endl;
if (sq-&gt;top1 + 1 = sq-&gt;top2) return false; Sq-&gt;data[--sq-&gt;toP2] = Elem;
return true;
else {cout &lt;&lt; "Stack Num must be 1 or 2!" &lt;&lt; Endl;
return false; }/* out stack/bool Pop (Sqstack *sq, elemtype *ptr, int stacknum) {if (Stacknum = 1) {if (Sq-&gt;top1 = =

-1) return false;
*ptr = sq-&gt;data[sq-&gt;top1--];
cout &lt;&lt; "Pop Item from Stack1:" &lt;&lt; *ptr &lt;&lt; Endl;
return true;
else if (Stacknum = 2) {if (Sq-&gt;top2 = = MAXSIZE) return false;
*ptr = sq-&gt;data[sq-&gt;top2++];
cout &lt;&lt; "Pop Item from Stack2:" &lt;&lt; *ptr &lt;&lt; Endl;
return true;
else {cout &lt;&lt; "Stack Num must be 1 or 2!" &lt;&lt; Endl;
return false;
} bool Stacktraverse (Sqstack Sq) {cout &lt;&lt; "Traverse Stack ..." &lt;&lt; Endl;
if (Stackempty (SQ)) return false;
cout &lt;&lt; "Stack1:"; for (int i = 0; i&lt;= Sq.top1;
i++) cout &lt;&lt; sq.data[i] &lt;&lt; ";
cout &lt;&lt; Endl;
cout &lt;&lt; "Stack2:";
for (int i = MAXSIZE-1 i &gt;= sq.top2 i--) cout &lt;&lt; sq.data[i] &lt;&lt; ";

cout &lt;&lt; Endl;
return true;
int main (void) {Sqstack Sq;
Initstack (&AMP;SQ);
for (int i = 0; i &lt; 5; i++) Push (&AMP;SQ, I, 1);
for (int i = 5; i &lt; i++) Push (&AMP;SQ, I, 2);
Stacktraverse (SQ);
int result;
POPs (&AMP;SQ, &amp;result, 1);
POPs (&AMP;SQ, &amp;result, 2);
Stacktraverse (SQ);
GetTop (Sq, &amp;result, 1);
GetTop (Sq, &amp;result, 2); if (!

Stackempty (SQ)) cout &lt;&lt; stacklength (sq) &lt;&lt; Endl;

Clearstack (&AMP;SQ);
return 0; }```

The output is:

In fact, using such a data structure is usually when the space requirements of the two stacks are thought-related, that is, when one stack grows and the other stack is shortened.

Also note that it must be the same data type stack, otherwise not only can not better solve the problem, but will make the problem more complex.

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.