Dual-end Stack Based on arrays, Arrays

Source: Internet
Author: User

Dual-end Stack Based on arrays, Arrays

The bottom of the stack is at both ends of the array, and the specified array size is 128, int type.

You can perform the following operations: NULL verification, full verification, adding elements to the two stacks, deleting the top elements of the stack, and displaying all elements in the stack respectively.

The Code is as follows:

# Include <iostream>

Using namespace std;

Typedef int StackElement;

Const int stack_cap acity = 128;

Class Stack

{

Public:

Stack ();

Int size1 () const;

Int size2 () const;

Bool empty1 () const;

Bool empty2 () const;

Bool full () const;

Void push1 (const StackElement & item );

Void push2 (const StackElement & item );

Void display1 ();

Void display2 ();

Void pop1 ();

Void pop2 ();

Int capacity () const;

StackElement top1 () const;

StackElement top2 () const;

Private:

StackElement myArray [STACK_CAPACITY];

Int myTop1;

Int myTop2;

};

Stack: Stack (): myTop1 (-1), myTop2 (-1)

{}

//-----------------------------------------------

Bool Stack: empty1 () const

{

Return (myTop1 =-1 );

}

//-----------------------------------------------

Bool Stack: empty2 () const

{

Return (myTop2 =-1 );

}

//-----------------------------------------------

Int Stack: size1 () const

{

Return myTop1;

}

//-----------------------------------------------

Int Stack: size2 () const

{

Return myTop2;

}

//-----------------------------------------------

Int Stack: capacity () const

{

Return STACK_CAPACITY;

}

//-----------------------------------------------

Void Stack: push1 (const StackElement & item)

{

If (myTop1 + myTop2> = capacity ()-2)

{

Cerr <"*** Full ***" <endl;

Exit (1 );

}

If (myTop1 <STACK_CAPACITY-1)

{

++ MyTop1;

MyArray [myTop1] = item;

}

Else

{

Cerr <"*** Error ***" <endl;

Exit (1 );

}

}

//-----------------------------------------------

Void Stack: push2 (const StackElement & item)

{

If (myTop1 + myTop2> = capacity ()-2)

{

Cerr <"*** Full ***" <endl;

Exit (1 );

}

If (myTop2 <STACK_CAPACITY-1)

{

++ MyTop2;

MyArray [STACK_CAPACITY-1-myTop2] = item;

}

Else

{

Cerr <"*** Error ***" <endl;

Exit (1 );

}

}

//-----------------------------------------------

Void Stack: display1 ()

{

For (int I = myTop1; I> = 0; I --)

Cout <myArray [I] <"";

Cout <endl;

}

//-----------------------------------------------

Void Stack: display2 ()

{

For (int I = myTop2; I> = 0; I --)

Cout <myArray [STACK_CAPACITY-1-i] <"";

Cout <endl;

}

//-----------------------------------------------

Void Stack: pop1 ()

{

If (! Empty1 ())

MyTop1 --;

Else

Cerr <"*** no element ***" <endl;

}

//-----------------------------------------------

Void Stack: pop2 ()

{

If (! Empty2 ())

MyTop2 --;

Else

Cerr <"*** no element ***" <endl;

}

//-----------------------------------------------

StackElement Stack: top1 () const

{

If (! Empty1 ())

Return (myArray [myTop1]);

Else

{

Cerr <"*** Stack is empty -- returning gabage value ***" <endl;

StackElement garbage;

Return garbage;

}

}

//-----------------------------------------------

StackElement Stack: top2 () const

{

If (! Empty2 ())

Return (myArray [STACK_CAPACITY-1-myTop2]);

Else

{

Cerr <"*** Stack is empty -- returning gabage value ***" <endl;

StackElement garbage;

Return garbage;

}

}

//-----------------------------------------------

Bool Stack: full () const

{

Return (capacity () <= myTop1 + myTop2 + 2 );

}

//-----------------------------------------------

Int main ()

{

Stack s;

Cout <"Two Stack created. Stack 1 Empty? "<Boolalpha <s. empty1 () <endl;

Cout <"Stack 2 Empty? "<Boolalpha <s. empty2 () <endl;

Cout <"please input a number to fill in Stack 1 :";

Int numItems1;

Cin> numItems1;

For (int I = 1; I <= numItems1; I ++)

S. push1 (I );

Cout <"please input a number to fill in Stack 2 :";

Int numItems2;

Cin> numItems2;

For (int I = 1; I <= numItems2; I ++)

S. push2 (I );

Cout <"Stack 1 contents:" <endl;

S. display1 ();

Cout <"Stack 2 contents:" <endl;

S. display2 ();

 

Cout <"Stack 1 empty? "<Boolalpha <s. empty1 () <endl;

Cout <"Stack 2 empty? "<Boolalpha <s. empty2 () <endl;

 

Cout <"Stack 1's Top value is:" <s. top1 () <endl;

Cout <"Stack 2's Top value is:" <s. top2 () <endl;

Cout <endl;

Cout <"The all Stack is full? "<S. full () <endl;

 

S. pop1 ();

Cout <"delete Stack 1 a value, then the top value is:" <s. top1 () <endl;

S. pop2 ();

Cout <"delete Stack 2 a value, then the top value is:" <s. top2 () <endl;

}

 

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.