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 <iostream> 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 << "Init stack ..." << Endl; SQ->TOP1 =-1;
    
    Represents an empty stack sq->top2 = MAXSIZE;
return true;
    /* Set to empty stack/bool Clearstack (Sqstack *sq) {cout << "clear stack ..." << Endl;
    SQ->TOP1 =-1;
    SQ->TOP2 = MAXSIZE;
return true;
    BOOL Stackempty (Sqstack Sq) {if Sq.top1 = 1 && sq.top2 = = MAXSIZE) return true;
else return false;
    int Stacklength (Sqstack Sq) {cout << "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 << "GeT Top1 Item "<< *ptr << Endl;
        return true;
    return false;
            else if (Stacknum = = 2) {if (sq.top2!= MAXSIZE) {*ptr = Sq.data[sq.top2];
            cout << "Get Top2 Item" << *ptr << Endl;
        return true;
    return false;
        else {cout << "Stack Num must be 1 or 2!" << Endl;
    return false;  }/* Press stack */bool Push (Sqstack *sq, elemtype Elem, int stacknum) {if (Stacknum = 1) {cout <<
        "Push Item to Stack1:" << Elem << Endl;
        if (sq->top1 + 1 = sq->top2)//stack full return false;
        SQ->DATA[++SQ->TOP1] = Elem;
    return true;
        else if (Stacknum = 2) {cout << "Push Item to Stack2:" << Elem << Endl;
        if (sq->top1 + 1 = sq->top2) return false; Sq->data[--sq->toP2] = Elem;
    return true;
        else {cout << "Stack Num must be 1 or 2!" << Endl;
    return false; }/* out stack/bool Pop (Sqstack *sq, elemtype *ptr, int stacknum) {if (Stacknum = 1) {if (Sq->top1 = =
    
        -1) return false;
        *ptr = sq->data[sq->top1--];
        cout << "Pop Item from Stack1:" << *ptr << Endl;
    return true;
        else if (Stacknum = 2) {if (Sq->top2 = = MAXSIZE) return false;
        *ptr = sq->data[sq->top2++];
        cout << "Pop Item from Stack2:" << *ptr << Endl;
    return true;
        else {cout << "Stack Num must be 1 or 2!" << Endl;
    return false;
    } bool Stacktraverse (Sqstack Sq) {cout << "Traverse Stack ..." << Endl;
    if (Stackempty (SQ)) return false;
    cout << "Stack1:"; for (int i = 0; i<= Sq.top1;
    i++) cout << sq.data[i] << ";
    cout << Endl;
    cout << "Stack2:";
    for (int i = MAXSIZE-1 i >= sq.top2 i--) cout << sq.data[i] << ";
    
    cout << Endl;
return true;
    int main (void) {Sqstack Sq;
    Initstack (&SQ);
    for (int i = 0; i < 5; i++) Push (&SQ, I, 1);
    for (int i = 5; i < i++) Push (&SQ, I, 2);
    Stacktraverse (SQ);
    int result;
    POPs (&SQ, &result, 1);
    POPs (&SQ, &result, 2);
    Stacktraverse (SQ);
    GetTop (Sq, &result, 1);
    GetTop (Sq, &result, 2); if (!
    
    Stackempty (SQ)) cout << stacklength (sq) << Endl;
    
    Clearstack (&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.

Contact Us

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.