Programmer's Path to cultivation-(2) linear table (bottom): Stack, queue

Source: Internet
Author: User

2BASIC Data Structure

Arrays and linked lists are the cornerstones for implementing various data structures, and the three most basic data structures in this section can be implemented using arrays or linked lists.

2.1Stack

Using arrays to implement " stacks " is very simple. The following is an example of C + + to implement a simple fixed-size " stack ".

First, the interface API is defined as follows, and the core function is push () and pop ():


2.1.1Array implementations

The following array-style implementation of the "stack", of course, can also be used to implement the array, but generally although the stack will handle a lot of operations, but at any time the elements stored in the stack are not many, so using the array to achieve more efficient than linked list:


loiteringproblem

It is important to note that when we implement a data structure and manage a memory area ourselves (such as holding arrays or linked lists as member variables), be aware of memory leaks, or loitering problems . As in the above stack implementation,push () and pop () just move the variable N, but when an object is stored in the array,pop () causes the array to still hold a reference or pointer to the useless object. As a result, the pop () implementation in the algorithm takes this into account and joins the Resize () feature.


Dynamic Sizing

The above code also implements the resize () function, when the size of the stack is equal to the total capacity (push detection), or the size of the stack is equal to the total capacity of the line, the stack bottom of the array will expand or shrink a fold, This always keeps the array in half the free space. The simple method is to create a new array and copy the old elements past.

Of course, the jdk 's ArrayList design is more complex than this, and the implementation ofJDK 6 is oldcapacity*3/2+1. It is important to note that the remove () does not shrink the array. the JDK Stack is a Vector-based, implementation-like, but needs to provide a parameter in the construction method Capacityincrement explicitly specifies how much to grow.


2.2.2Linked list Implementation

It is important to note that when implemented with a linked list, there is no need to dynamically resize to avoid space shortages or waste, or to consider loitering issues , since it does not always hold a chunk of memory space. Directly manipulate the target node, releasing the node's space (C + +) or giving it directly to the GC(Java) when it is deleted.


2.2.3Iteration

In addition to the basic operational functions provided by the data structure, sometimes we often have to iterate through all the elements. At this point, the iterator is a very powerful tool that can pull the client out of the concrete implementation of the underlying data structure. "STL Source parsing" gave away: the main ideaofSTL is to separate the algorithms from the data structures, design each other independently, and finally bring them together with the binder of iterators . Not only Java(support for iterators is good, theJDK itself provides the iterable and Iterator interfaces, which can be automatically integrated with foreach after implementation Traversal), the iterator in C + + has a higher position in the STL :


2.2Queue

The queue is very easy to implement with a linked list, here's how to implement it with arrays. Because the " stack" is coming in and out of one end, N reaching the tail of the array means that the " stack " is full and needs resize. However, the queue is in and out of the two piles, even if the end of the array , the array head may still have the free space generated by the team, so we can use " wrap around " to solve the problem .

2.2.1Array implementations

In addition to using the first and last two subscript tags, be sure to save the current size, otherwise it is difficult to determine whether the queue is empty or full only from the first and last positions.


2.2.2 Ring Buffer

in the "LMAX High concurrency system architecture" , the high concurrency framework disruptors uses Ring buffer to save buffered data. The difference between a Ring Buffer and an array-implemented queue is that there is no trailing pointer, and the queued operation overwrites the existing data. Because the queue will cover the elements, it is up to the producers and consumers to control the speed and avoid the unhandled elements being overwritten.

Programmer's Path to cultivation-(2) linear table (bottom): Stack, queue

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.