Data structure stack and queue (1)

1. The definition and characteristics of stacks and queues

  1. Stack: Stack (stack), also known as stack, is a linear table ( last in, first out ) limited to insert and delete operations at one end of the table . This end is called the top of the stack , while the other end is called the bottom of the stack . An empty list without elements is called an empty stack.

  2. Queue: In contrast to a stack, a queue is a first-in, first-out linear list. It only allows delete operations at the front end of the table, and insert operations at the back end of the table. The end that allows insertion is called rear, and the end that allows deletion is called front.

    Inserting new elements into a stack is also called pushing into the stack, and deleting elements from a stack is also called making a stack. Inserting a queue element into the queue is called enqueuing, and deleting a queue element from the queue is called dequeuing. Like linear tables, the storage structures of stacks and queues also include sequential and chained.

    Original link: https://blog.csdn.net/qq_41117236/article/details/80764231

Summary: Stacks and queues are essentially linear tables, but they are limited in operation.

2. The application of stack and queue

Case number one:

Conversion between bases: We often use the tossing and turns division method, such as converting decimal 161 to octalimage.png

Case 2. Bracket matching:

When we write code, we often use a lot of nested parentheses, and the computer uses the characteristics of the stack to match.image.png

Case 3. Expression value:

For example, an expression (a+b)*c+e/f

The computer also uses the stack to perform calculations, first create the operator stack OPTR operand stack OPND

*Scan the number and push it into the stack OPND;

If the operator is scanned, then:

  1. If the operator has a higher priority than the OPTR operator on the top of the stack, it will be pushed into the stack OPTR (when OPTR has no elements at the beginning, the operator will be pushed directly into the stack)

  2. If the operator has a lower priority than the OPTR stack top operator, pop two numbers from the OPND stack top for calculation, and push the calculation result into the OPND stack

*Continue to process the current character until the end character is encountered

Three. Circular queue

The circular queue is to wrap the last position of the queue storage space to the first position to form a logical ring space for the queue to recycle.

Here I only talk about the program implementation of the circular queue. As for other sequential stacks, chain stacks, etc., I have introduced them in the sequence list and linked list before, and there is little change.

The reason why there is a circular queue instead of a circular stack is that the queue is "first in, first out", so the space can be recycled. We assume that MAXQSIZE is the length of the queue, and Q.rear points to the next element of the last element. Q.front points to the starting element.

First create a queue

 #define MAXQSIZE 64
 struct data
{
        char name[];
        int age;
};
 struct  Com
 {
         struct data  arr[MAXQSIZE];
         int rear = 0;
        int front = 0;
         
 };
int main
{
        struct Com member;
        return 0;
}

Insert element

        void insert( struct Com member , struct data ele  )
        {
        if((member.rear+1)%MAXQSIZE==member.front)
                   return ;
         else        
                { 
                 * (member.arr[member.front])=ele;
                  member.rear=(member.rear+1)%MAXQSIZE;
                 }
        }
       // It can be seen that it is not difficult for a circular queue to master% operations.

Guess you like

Origin blog.51cto.com/14961637/2665769