Implementation of stack and queue in java

Source: Internet
Author: User

For example, if you want to use the breadth-first method to traverse Binary Trees, you need a queue to store nodes. For example, if you want to implement Recursive Computing on your own, you need a stack. Stack and queue are both linear tables with weak functions. Logically, stacks and queues are actually developed from ordinary linear tables, and some special restrictions are added to ordinary linear tables to get stacks and queues. In terms of functions, the stack and queue functions are relatively weaker than ordinary linear tables, but in some special cases, the use of stacks and queues is more advantageous. For example, the stack is used to store power outages when the compiler implements function calls, And the stack is also used to store recursive algorithms. Stack's English word is stack, which represents a special linear table. This linear table can be inserted or deleted only at a fixed end (usually considered as the end of a linear table. For a stack, one end that allows insertion or deletion is called the top, while the other is called the bottom ). Stack is a restricted linear table. Generally, the following methods in a linear table are not provided. Obtains the elements at the specified index, finds the location of data elements by value, inserts data elements at the specified index, and deletes the data elements at the specified index. From the above method, we can see that the stack should not provide methods to access elements from any center. That is to say, the stack can only insert or delete elements at the top of the stack. Common stack operations are as follows: initialization: A constructor is usually used to create an empty stack. Return stack length: This method is used to return the number of data elements in the stack. STACK: Insert a data element to the top of the stack. The stack length is + 1. Out-of-Stack: deletes a data element from the top of the stack. The linear table length is-1. This method usually returns the deleted element. Access stack top element: return the data element at the top of the stack, but do not delete the standing element. Judge whether the stack is empty: This method checks whether the stack is empty. If the stack is empty, true is returned; otherwise, false is returned. Empty Stack: Empty stack. The stack of the sequential storage structure is called the sequential stack. It uses a set of sequential storage units to store data elements from the bottom of the stack to the top of the stack. The position of the bottom stack remains unchanged. Its top stack elements can be directly accessed through the array element arr [size-1] of the underlying arrays of the sequential stack. Similarly, the program can use a single linked list to save all elements in the stack. the stack of this chain structure is also called a chain stack. For a chain stack, the top elements of the stack are constantly changing. The program only needs to use a top reference to record the top elements of the current stack. The top referenced variable always references the top element of the stack, and then uses a size variable to record the number of elements contained in the current stack. For the stack import operation, the program only needs to do the following two things: Let the top reference point to the newly added element, and the next reference of the new element points to the original stack top element; let the size variable + 1 of the number of elements in the record stack. For the stack exit operation, the top element of the stack needs to pop up the stack. The program needs to do two things: Let the top reference point to the next element at the top of the original stack, and release the original stack top element; let the size variable of the number of elements in the record stack-1. Java collections actually provide the following two stacks for developers. Java. util. stack: it is the most common ordered stack, which is implemented based on arrays at the underlying layer. This stack class is thread-safe and can be safely used in a multi-threaded environment. Java. util. using list: Using list is a two-way linked list, but if you view the APIs of this class, you will find that it also provides push, pop, peek, and other methods, this indicates that the consumer list can be used as a stack. The consumer list indicates the stack chain implementation, but it is thread-insecure. If you need to use it in a multi-threaded environment, the collections class tool should be used to "transform" it into a thread-safe class. A queue is another type of restricted linear table. It uses a fixed end to insert data elements, and the other end is only used to delete elements. That is to say, the moving direction of elements in the queue is always fixed, just like queuing for shopping: customers who enter the queue first get the service, and customers in the queue always move in the fixed direction, the current customer will only obtain the service after all the customers in front of the queue receive the service. A queue is a special linear table. It can be deleted only at the front end of the table and inserted only at the back end of the table (rear. The end of the insert operation is called the end of the team, and the end of the delete operation is called the opposite. Ordered storage structure of the queue: The system uses a set of sequential storage units to store all data elements from the rear end to the front end in sequence, the program only needs two integer variables, front and rear, to record the element indexes at the front end of the queue and the element indexes at the rear end. To reuse the space occupied by deleted elements in the underlying array of the ordered queue and clear the possible "false full" phenomenon, you can improve the ordered queue to a cyclic queue. A two-way Queue (Deque) represents a special queue, which can be inserted and deleted at both ends at the same time. JDK provides an old stack class, but we do not recommend developers to use this old "stack" implementation. Instead, we recommend using the deque implementation class as the "stack" implementation. JDK provides three implementation classes for deque: arrayDeque, javasblockingdeque, and javaslist. Here, arrayDeque represents the two-way queue of the sequential storage structure, while the queue list represents the two-way queue of the chained storage structure. The parallel blockingdeque is actually a thread-safe, chain-structured two-way queue. The queue list represents a bidirectional cyclic linear table with a chained storage structure. Here we mention that the queue list represents a thread-safe, chained two-way queue. It can be seen that the queue list is a very powerful collection class. In fact, the writable list is almost the class with the most methods in the java Collection framework. However, in most cases, arraylist and arrayDeque have better performance than javaslist.

Related Article

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.