The definition and basic operation of the stack

1. The definition, nature and storage of the stack

1. Definition: A stack is a linear table that is only limited to one end of the table.
2. Nature: First in
, last out (last in, first out) 3. Stack storage: sequential storage and chain storageInsert picture description here

Two, the basic operation of the sequential stack

Insert picture description here
Type definition of sequential stack

typedef struct{
    
    
  ElemType ST[maxsize];
  int top;
}stack;

Push operation of the stack
(1) First judge whether the stack is full, if the stack is full, output "stack full overflow";
(2) Pointer top++ first, then push the element value into the stack;

Push(&S,e)
{
    
    
 if(S.top==maxsize-1)
 cout<<"栈满溢出"<<endl;
 else
 {
    
    
  S.tpo++;
  S.St[S.top]=e;
 }
 return S;
}

Pop-up operation of the stack
(1) Determine whether the stack is empty, and if so, return "stack empty underflow";
(2) Take the top element of the stack, and the pointer top- -;

POP(&S,&e)
{
    
    
if(S.top==0)
cout<<"栈空下溢"<<endl;
else
 {
    
    
  e=S.ST[S.top];
  S.top--;
 }
 return S;
}

Read the top element of
the stack When the stack is not empty, read and return the top element of the stack (the stack pointer remains unchanged for this operation)

Gettop(S,&e)
{
    
    
  if(S.top!=0)
  e=S.ST[S.top];
  return e;

Guess you like

Origin blog.csdn.net/gets_s/article/details/104946838