Learning data structure-Chapter 3: Stack and Queue (basic operation of stack)

Chapter 3: Stacks and Queues

1. The basic concept of the stack

Stack (Stack) only at one end (this end is called the top of the stack) insertion or deletion operation the linear form

The element that enters the stack first in the stack will be popped out later: 先进后出(LIFO)

Insert picture description here

2. The basic operation of the stack

InitStack(&s)Initialize an empty stack S to
StackEmpty(S)determine whether a stack is empty, if the stack is empty, return true, otherwise return false to
Push(&S,x)enter the stack, if the stack S is not full, it will be added to make it the top of the new stack.
Pop(&S,&x)Pop the stack, if the stack is not empty, pop the top element of the stack and return with x.
GetTop(S,&x)Read the top element of the stack, if the stack is not empty, use x to return the top element of the stack.
ClearStack(&S)Destroy the stack and release the memory space occupied by S.

We found here: Popand GetTopthese two functions can return the top element, except that Popremoves the top element, and GetTopwill only get the value of the top element, but not the stack.

3. The sequential storage structure of the stack

Sequential stack: a stack that uses sequential storage

#define MaxSize 50
typedef struct{
    
    
     ElemType date[MaxSize];
     int top; //数组的下标
}SqStack;

Insert picture description here

3.1 Determine the stack is empty & full & length

Stack empty condition:, S.top==-1when there is no content in the stack, top represents the subscript of the array and the value is -1 at this time;
the length of the stack:, S.top+1top represents the data subscript, so the length of the stack is equal to the array subscript + 1; the
stack full condition : S.top==MaxSize-1, If the top subscript of the stack is array length -1, the stack is full.

Insert picture description here

3.2 The basic operation of the stack

3.2.1 Initialization

void InitStack(SqStack &S){
    
    
    S.top == -1;
}

3.2.2 Determine the stack is empty

boll StackEmpty(SqStack &S){
    
    
    if(S.top == -1){
    
    
        return true;
    }else{
    
    
        return false;
    }
}

3.2.3 Push into the stack

boll Push(SqStack &S,ElemType x){
    
    
    if(S.top == MaxSize-1){
    
     //栈满
        return fase;
    }
    S.data[++S.top] = x;
    return true;
}

3.2.4 Pop

boll Pop(SqStack &S,ElemType &x){
    
    
    if(S.top == -1){
    
     //栈空
        return fase;
    }
    x = S.data[S.top--]; 
    return true;
}

3.2.5 Get the top element of the stack

boll GetTop(SqStack S,ElemType &x){
    
    
    if(S.top == -1){
    
     //栈空
        return fase;
    }
    x = S.data[S.top] //只需读取,不需移动下标
    return true; 
}

3.3. Shared stack

Shared stack: Set the bottom of the two stacks at both ends of the shared space, and the top of the stack extends to the middle of the space;

Empty:

  • Stack 0 top==-1
  • 1号栈 top==MaxSize

Insert picture description here
Full sentence: tpp1-top0==1

The advantages of the shared stack : the access time complexity is still O(1), and the space utilization is more efficient.

4. Chained storage structure of the stack

Chain stack A stack using chain storage

There is no need first node, where a node provides the first node of the stack so that: 所有的操作都要在表头进行.
Insert picture description here

4.1 Basic operation of chain stack

Because it is equivalent to a singly-linked list, the length of the stack is basically the same as the operation of the singly-linked list.

Welcome to public concern number 理木客more exciting waiting for you to discover

Guess you like

Origin blog.csdn.net/qq_41941875/article/details/106221080
Recommended