Seqential stack is a stack stored in sequential storage mode. The class definitions of the ordered stack below are stored in arrays. Use stacka [maxsize] for storage. maxsize is the maximum number of elements that can be stored. The top variable indicates the position of the element at the top of the stack. The initial value is-1, indicating that the stack is empty.
First, we will describe the abstract data type of the ordered stack, and then give the class definition and implementation of the ordered stack. The following describes the abstract data types of the ordered stack.
ADT seqstack is
Data
The top of the stack indicates the maximum number of elements stored in the stack element group and stack.
Seqstack
Input: Maximum number of stack Elements
~ Seqstack
Delete an array of storage stack Elements
Push
Input: item to be added to the stack
Prerequisites: The end of the stack is full.
Action: Push the item to the top of the stack
Output: None
Post condition: a new element is added to the top of the stack, and 1 is added to the top of the stack.
Pop
Input: None
Prerequisites: the stack is not empty.
Action: The top element of the stack is displayed.
Output: return the value of the top element of the stack.
Post condition: Delete the top element of the stack. The top element of the stack indicates that the value is reduced by 1.
Gettop
Input: None
Prerequisites: the stack is not empty.
Action: Take the top element value of the stack.
Output: return the top data element of the stack.
Post condition: None
Empty
Input: None
Prerequisites: None
Action: Check whether the stack top indicator is equal to-1
Output: If the stack is empty, 1 is returned; otherwise, 0 is returned.
Post condition: None
Full
Input: None
Prerequisites: None
Action: Check whether stack top indication is equal to maxsize-1
Output: 1 is returned when the stack is full; otherwise, 0 is returned.
Post condition: None
Clear
Input: None
Prerequisites: None
Action: Clear Stack
Output: None
Post condition: the stack top indication is-1
End ADT seqstack
//////////////////////////////////////// //////////////
/*
** ===================================================== =
** Seqstack. h sequence stack template class definition and implementation
** ===================================================== =
*/
# Include <iostream. h>
# Include <stdlib. h>
//////////////////////////////////////// //////////////////////////////////////// /////
# Define maxsize 0 xFFFF
Template <class type>
Class seqstack
{
Int top; // stack top indication
Type * stacka; // array name
Int maxsize; // The maximum number of elements that a stack can accommodate.
Public:
Seqstack ();
Seqstack (INT size );
Seqstack (type data [], int size );
Virtual ~ Seqstack ()
{
Delete [] stacka;
}
Void push (const type & item); // element item pressure Stack
Type POP (); // data element output stack, returned
Type gettop (); // read the data element at the top of the stack and return it
Int empty () const
{
Return Top =-1;
} // Judge whether the stack is empty
Int full () const
{
Return Top = maxsize-1;
} // Determine whether the stack is full
Void clear ()
{
Top =-1;
} // Clear the stack
};
Template <class type>
Seqstack <type >:: seqstack ():
Top (-1), maxsize (maxsize)
{
Stacka = new type [maxsize];
If (stacka = NULL ){
Cerr <"Dynamic Storage Allocation failed! "<Endl;
Exit (1 );
}
}
Template <class type>
Seqstack <type >:: seqstack (INT size ):
Top (-1), maxsize (size)
{
Stacka = new type [maxsize]; // create an array of storage stacks
If (stacka = NULL) {// allocation failed
Cerr <"Dynamic Storage Allocation failed! "<Endl;
Exit (1 );
}
}
Template <class type>
Seqstack <type >:: seqstack (type data [], int size ):
Top (-1), maxsize (size)
{
Stacka = new type [maxsize]; // create an array of storage stacks
If (stacka = NULL) {// allocation failed
Cerr <"Dynamic Storage Allocation failed! "<Endl;
Exit (1 );
}
For (INT I = 0; I <maxsize; I ++ ){
Stacka [I] = data [I];
}
Top + = maxsize;
}
Template <class type>
Void seqstack <type >:: push (const type & item)
{
// If the stack is full, handle the error; otherwise, press the element item on the stack.
If (full ()){
Cerr <"the stack is full and cannot be pressed! "<Endl;
Exit (1 );
}
// Here we move the pointer first and then operate the element.
Top ++;
Stacka [Top] = item;
}
Template <class type>
Type seqstack <type >:: POP ()
{
If (empty ()){
Cerr <"Stack is empty! "<Endl;
Exit (1 );
}
// The stack is not empty. The top element of the stack is used.
Type data = stacka [Top];
Top --;
// Return the top element of the stack
Return data;
}
Template <class type>
Type seqstack <type >:: gettop ()
{
// If the stack is not empty, return the value of the top element of the stack.
If (empty ()){
Cerr <"Stack empty! "<Endl;
Exit (1 );
}
// Return the value of the top element of the stack.
Return stacka [Top];
}
//////////////////////////////////////// //////////////////////////////
/*
** ===================================================== =
** Main. h mainProgram
** ===================================================== =
*/
# Include "seqstack. H"
Int main ()
{
Int data [10] = {1, 2, 4, 5, 6, 7, 8, 9, 10 };
Seqstack <int> stack (data, 10 );
While (! Stack. Empty ()){
Cout <stack. Pop () <"";
}
Cout <Endl;
Return 0;
}
//////////////////////////////////////// //////////////////////////////////////// ////////////
Output result:
10 9 8 7 6 5 4 3 2 1
Press any key to continue