Code used by two stacks to share an ordered Bucket

Source: Internet
Author: User

# Include <iostream. h>
# Define STACK_INIT_SIZE 10 // initial storage allocation

Struct Dblstack {
Int * base1;
Int * base2;
Int * top1;
Int * top2;
Int stacksize; // The currently allocated storage space, in the unit of Elements
};


Int Initstack (Dblstack & s)
{// Construct an empty sequential stack s
S. base1 = new int [STACK_INIT_SIZE];
If (! S. base1) {cout <"memory application failed! N "; return 0 ;}
S. top1 = s. base1 + 1;
S. stacksize = STACK_INIT_SIZE;
S. base2 = s. base1 + s. stacksize;
S. top2 = s. base2-1;
Return 1;
}

Int emptystack (Dblstack & s, int I)
{// Judge whether the stack is empty. 1 is returned; otherwise, 0 is returned.
If (I = 1 & s. top1 = s. base1 + 1 | I = 2 & s. top2 = s. base2-1)
Return 1;
Else return 0;
}

Int fullstack (Dblstack & s)
{// Check whether the stack is full. If the stack is full, 1 is returned. Otherwise, 0 is returned.
If (s. top1 = s. top2) return 1;
Else return 0;
}

Int push (Dblstack & s, int I, int x)
{// Insert element x as the new top element of the stack
If (fullstack (s) {cout <"stack full n"; return 0 ;}
Else {
If (I = 1) {cout <"the insert operation is successful! Nn "; * s. top1 ++ = x ;}
If (I = 2) {cout <"the insert operation is successful! Nn "; * s. top2 -- = x ;}
} Return 1;
}

Int pop (Dblstack & s, int I, int & x)
{// If the stack is not empty, the top element of stack in stack I is deleted and x is returned. Otherwise, 0 is returned.
If (emptystack (s, I) return 0;
Else {
If (I = 1) {cout <"Stack output successful! The output stack element is: "; x = * -- s. top1 ;}
If (I = 2) {cout <"Stack output successful! The output stack element is: "; x = * ++ s. top2 ;}
} Return 1;
}

Int gettop (Dblstack & s, int I, int & x)
{// If the stack is not empty, use x to return the top element of the stack and the true element; otherwise, return 0.
If (emptystack (s, I) return 0;
Else {
If (I = 1) {x = * (s. top1-1); cout <"n out stack successful! Stack top :";}
If (I = 2) {x = * (s. top2 + 1); cout <"n stack output successful! Stack top :";}
} Return 1;
}

Int clearstack (Dblstack & s)
{// If the stack is empty, 1 is returned. Otherwise, 1 is returned after the stack is empty.
If (emptystack (s, 1) & emptystack (s,

<

Related Article

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.