Sequential stack C ++ implementation and linear sequence implementation for linear tables
Sequential stack of linear tables
Stack is a linear table that is only inserted and deleted at the end of the table (the top of the stack). FILO: Advanced and later
I. Sequence stack header file: SeqStack. h
// Sequence stack header file
# Include <iostream>
Using namespace std;
// Set the size of the sequential Stack
Const int StackSize = 10;
Template <class DataType>
// Define the template type of the sequence Stack
Class SeqStack {
Public:
// The No-argument constructor initializes the top pointer of the stack.
SeqStack () {top =-1 ;}
// The Destructor is empty.
~ SeqStack (){}
// Add elements to the stack
Void Push (DataType x );
// Stack exit operation. The top element of the stack is popped up.
DataType Pop ();
// Retrieve the top element of the stack. Do not delete the top element of the stack (that is, the retrieved element is still in the stack)
DataType GetTop ();
// Judge whether the stack is empty
Int IsEmpty ();
Private:
// Pointer for storing station Elements
DataType data [StackSize];
// Stack top pointer: subscript of the stack top element in the array
Int top;
};
// Implement sequential stack-to-stack operations
Template <class DataType>
Void SeqStack <DataType>: Push (DataType x)
{
// Determine whether the current sequence stack is full
If (top = StackSize-1)
{
Throw "stack full ";
}
Else
{
// If the current sequence stack is not satisfied, the stack top value stack moves one unit up
Top ++;
// Assign a value to the top element of the new stack
Data [top] = x;
}
}
// Implement sequential stack output and bring up the top elements of the stack
Template <class DataType>
DataType SeqStack <DataType>: Pop ()
{
// First determine whether the stack is empty
If (top =-1)
{
Throw "Stack is empty stack ";
}
Else
{
// If the stack is not empty, the top element of the stack is taken out and the top element of the stack is deleted.
DataType x = data [top];
// Move a single element down the top pointer of the stack
Top --;
// Return the value of the top element of the stack.
Return x;
}
}
// Extracts the top element of the stack, but does not delete the elements.
Template <class DataType>
DataType SeqStack <DataType >:: GetTop ()
{
// First determine whether the stack is empty
If (top =-1)
{
Throw "Stack is empty stack ";
}
Else
{
// If the stack is not empty, the value of the top element of the stack is taken out, but the top element of the stack is not deleted.
DataType x = data [top];
// Return the value of the top element of the stack.
Return x;
}
}
// Determine whether the sequence stack is empty. If the stack is empty, 1 is returned. Otherwise, 0 is returned.
Template <class DataType>
Int SeqStack <DataType >:: IsEmpty ()
{
If (top =-1)
{
Return 1;
}
Else
{
Return 0;
}
}
Ii. source file of the test sequence Stack: TestSeqStack. cpp
// Test the sequence Stack
# Include <iostream>
// Introduce the sequence stack header file
# Include "SeqStack. h"
Using namespace std;
Int main ()
{
// Create an ordered Stack
SeqStack <int> seqStack = SeqStack <int> ();
// Determine whether the stack is empty
Cout <"Whether the stack is empty at this time:" <seqStack. IsEmpty () <endl;
// Stack entry
SeqStack. Push (1 );
Cout <"1 into the stack" <endl;
// Determine whether the stack is empty
Cout <"Whether the stack is empty at this time:" <seqStack. IsEmpty () <endl;
// Retrieve the top element of the stack
Cout <"the top element of the stack is:" <seqStack. GetTop () <endl;
// Output Stack
SeqStack. Push (3 );
Cout <"3 into the stack" <endl;
// Retrieve the top element of the stack
Cout <"the top element of the stack is:" <seqStack. GetTop () <endl;
// Output Stack
SeqStack. Push (7 );
Cout <"7 inbound stack" <endl;
// Retrieve the top element of the stack
Cout <"the top element of the stack is:" <seqStack. GetTop () <endl;
// Output Stack
SeqStack. Push (5 );
Cout <"5 into the stack" <endl;
// Retrieve the top element of the stack
Cout <"the top element of the stack is:" <seqStack. GetTop () <endl;
// Output Stack
Cout <"top stack element" <seqStack. Pop () <"out of stack" <endl;
// Retrieve the top element of the stack
Cout <"the top element of the stack is:" <seqStack. GetTop () <endl;
// Output Stack
Return 0;
}
Iii. Running example results: