Data structure and algorithm analysis: Stacks and queues

Source: Internet
Author: User

Here are some summaries of the stacks and queues in the data structure:

First, stack

A stack is a special linear table that has the properties of a LIFO (last on first out, LIFO) and can only insert and delete elements from a segment of a linear table.

The common operations of the stack are: into the stack, out of the stack, take the top of the stack, empty the stack, determine whether the stack is empty, judge whether the stack is full, and so on.

Because the stack also belongs to the linear table, the storage structure of the linear table also applies to the stack, so the stack can be implemented using either an array or a unidirectional linked list. These two storage structures are different, so the way to implement the stack is different, the formation of the nature of the stack is different. A common difference is that there is the concept of "overflow" and "underflow" in the sequence stack, that is, when the stack is full and the data is called "Overflow", the stack is empty and the data operation becomes "Underflow". The chain stack does not have a "overflow" limit, since the new node can be freely added at the end of the operation without overflow.

1. Linear stack

Sequential stack, you need to design a specified size SIZE array to store the entire stack, the element labeled 0 is the bottom of the stack, with the top variable to indicate the top of the stack of the following table, because the size of the array SIZE , so 0 <= top < SIZE . When top = 0 the time indicates that the stack has only one element; When the stack is top = SIZE - 1 full, it is possible to make the stack top = -1 that represents the current empty.

2. Chain-Stack

The disadvantage of linear stack is obvious, because its size is fixed, so if the number of elements into the stack can not be estimated is prone to stack overflow situation, you should consider the use of chain storage structure, that is, chain-type stack. It is important to note that the chain stack does not need to attach the head node to the head, because the stack is operated on the head.

Second, the queue

Queue is also a special linear table, its limitations and stacks, there are restrictions at both ends of the table, the insertion can only be done at one end of the table, only to enter, and delete only at the other end of the table, only out. One end of the allowed insert data is called the team header (front), and one end of the allowable deletion is called the tail of the queue (rear). The nature of the queue is the opposite of the stack, which is FIFO (first-in-out, FIFO).

Unlike the stack, the usual operations of the queue are: Enter the team, out of the team, take the enemy data, set the queue empty, determine whether the queue is empty, determine whether the queue is full, and so on.

Similar to stacks, queues have both sequential and chained storage structures, called sequential queues and chain teams, respectively.

1. Sequential queue

To establish a sequential queue structure, you must statically allocate or dynamically request a contiguous amount of storage space and set up two pointers for management. One is the team head pointer front, which points to the team head element, the other is the tail pointer rear, which points to the location where the next queued element is stored.

When Front=rear, there are no elements in the queue, called empty queues. Each time an element is inserted at the end of the team, rear increases by 1, and each time an element is removed from the team header, front increases by 1. As the insert and delete operations proceed, the number of queue elements is constantly changing, and the storage space occupied by the queue is also moving in the contiguous space allocated for the queue structure. An example of an operation is as follows:

There may be "underflow", "true Overflow" or "false overflow" in the sequential queue, as explained below:

"underflow" phenomenon : When the queue is empty, make the overflow phenomenon caused by the team operation. "Underflow" is a normal phenomenon and is commonly used as a condition for program control transfer.

"true Overflow" phenomenon : When the queue is full, do the stack operation to create a space overflow phenomenon. A "true overflow" is an error state that should be managed to avoid.

As for the "false Overflow", back to the example above, you will find that the D step, when the rear increased to point to the contiguous allocation of space, the queue can no longer insert new elements, but at this time there is often a lot of free space is not occupied, which is already out of the queue elements have occupied the storage unit. This is known as a "false overflow" phenomenon.

To make the queue space reusable, often the use of the queue is slightly improved: regardless of the insertion or deletion, once the rear pointer 1 or the front pointer increases by 1 when the allocated queue space is exceeded, let it point to the starting position of this continuous space.

In order to overcome the space waste caused by this phenomenon, the use of the queue can be slightly improved: no matter the insertion or deletion, once the rear pointer 1 or the front pointer increases by 1 when the allocated queue space is exceeded, let it point to the starting position of this continuous space, This is like putting the vector space at the end of the line, forming a closed loop, when the queue becomes a circular queue.

The circular queue solves the problem of "false overflow", but brings a new problem. As mentioned above, in the loop queue, when the queue is empty, there is front=rear, and when all the queue space is full, there is also front=rear. It is therefore necessary to make some adjustments to the queue to differentiate between the two cases.

There are several ways to differentiate between the two cases, you can use a counter to record the total number of elements in the queue, so that you can always know the length of the queue, as long as the number of elements in the queue is equal to the length of the vector space, is the team full. Another approach is to specify that the loop queue can have up to MaxSize-1 queue elements, and when only one empty storage unit is left in the loop queue, the queue is full. Therefore, the queue is sentenced to empty conditions when front=rear, while the queue is full of conditions when front= (rear+1)%maxsize.

2. Chain-Queue

A chained queue is similar to a single-linked list of linear tables, except that chained queues allow only deletions from the head, and trailing inserts . You need to create a header node for the chained queue that includes two pointers, a pointer to the team head front and a pointer to the tail of the team rear. The queue is empty when two pointers are equal.

Resources:

The third edition of the Introduction to the algorithm (cormen,t.h), Coleman, Yingjianping
Http://blog.csdn.net/ns_code
Http://baike.baidu.com/link?url=9PVFeYIx1ZsbyihHAI5OWrJs2xdOs5eH0I3oZyCwkNG16XnigPbUozfYkRmic_ 66p8cgu8nehggufjxunhsrfmqsfsvg3nxjs3ceoqfjiai

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure and algorithm analysis: Stacks and queues

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.