Chapter 4 stack and queue, Chapter 4 queue

Source: Internet
Author: User
Tags stack pop

Chapter 4 stack and queue, Chapter 4 queue
I. Stack Definition

Stack is limited to insert and delete operations at the end of the table.Linear table.

The end that is allowed to be inserted and deleted is the top stack, and the other end is the bottom stack. A stack without any data elements is called an empty stack. Stack is also called a linear table of the last-in-first-out (LIFO.

The following figure shows the stack-to-stack operations:

 

Ii. Abstract Data Types of stacks

Stack operations:

Since the stack itself is a linear table, the sequence storage and chain storage of linear tables are discussed in the previous chapter, which is also applicable to the stack.

Iii. Stack sequence storage structure and implementation

Let's take a look at the stack structure definition:

If the storage stack length is StackSize, the top position of the stack must be smaller than StackSize. When an element exists in the stack, top is equal to 0. Therefore, the condition for determining the empty stack is usually located. top is equal to-1.

Take a look:

The code for the push operation is as follows:

Out-of-stack pop, the Code is as follows:

Neither of them involves any loop statements, so the time complexity is O (1 ).

Iv. Stack chain storage structure and implementation

The chain storage structure of the stack, referred to as the chain stack.

The code of the chain stack structure is as follows:

Stack import:

Out-of-stack operations:

The preceding operations do not contain loops, so the time complexity is O (1 ).

For the choice of sequential stack and chain Stack: If element changes are unpredictable, sometimes small, or sometimes very large during the use of the stack, it is best to use the chain stack. Otherwise, if its changes are within the controllable range, it is recommended to use the sequential stack.

5. Stack application-recursion

The most typical example of recursion: (Fibonacci number series)

The Code is as follows:

Recursive definition: a recursive function is called to call itself directly or indirectly through a series of call statements.

The most terrible thing about recursive Programs is to fall into endless recursion that never ends. Therefore, each recursive definition must have at least one condition. If it meets the conditions, recursion will not proceed, that is, it will not reference itself but return value to exit.

6. Stack application-evaluate the four arithmetic expressions

We introduce a suffix expression that does not require parentheses. We call it inverse Polish (RPN ).

Let's take an example: for arithmetic operations: 9 + (3-1) * 3 + 10/2, the inverse polish expression is: 9 3 1-3*10 2/+ is an anti-human expression, but it is computer-compatible, so we can endure it!

Calculation rule: traverse each number and symbol of the expression from left to right. When a number is encountered, it is pushed to the stack. When a symbol is encountered, the two digits at the top of the stack are pushed out of the stack for calculation, repeat the calculation result until the result is obtained.

VII. Queue Definition

Definition: a queue is a linear table that can be inserted only in one segment and deleted at the other end.

A queue is a first-in-first-out (FIFO) linear table.

ABSTRACT Data Type of the queue:

Definition of cyclic queue: we call the sequential storage structure of the queue's head-end connection as a cyclic queue.

The condition for determining that the queue is full is: (rear + 1) % QueueSize = front

Formula for Calculating queue length: (rear-front + QueueSize) % QueueSize

 

The sequential Storage Structure Code of the cyclic queue is as follows:

The initialization code of the cyclic queue is as follows:

The code for calculating the queue length in cyclic queue is as follows:

The following code is used to queue a cyclic queue:

The code for running a cyclic queue is as follows:

 

Cyclic queues are faced with the possibility of array overflow. We also need to study the chained storage structure without worrying about queue length.

VIII. Queue chain storage structure and implementation

The chain storage structure of a queue is actually a single-chain table of a linear table, but it can only be output at the end. We call it a chain queue for short.

The blockchain queue structure is as follows:

Queue chain Storage Structure-queue operations

The Code is as follows:

Queue chain Storage Structure-team-out operations

The Code is as follows:

In general, we recommend that you use a cyclic queue when you can determine the maximum length of the queue. If you cannot predict the length of the queue, use a chain queue.

IX. Summary and review

Stack is a linear table that is only inserted and deleted at the end of the table.

A queue is a linear table that can be inserted only in one segment, but deleted at the other end.

They can all be implemented using the sequential storage structure of linear tables, but there are some disadvantages of sequential storage.

For queues, in order to avoid moving data during array insertion and deletion, a circular queue is introduced so that the headers and tails can change cyclically in the array. The time loss of mobile data is solved, so that the time complexity of originally inserted and deleted O (n) is programmed O (1 ).

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.