What are the differences between "heap", "stack", "stack", "queue", and so on?

Source: Internet
Author: User
Tags garbage collection

Transferred from: http://jingyan.baidu.com/article/6c67b1d6a09f9a2786bb1e4a.html

Blogger Summary: stack = stack (alias of Stack). = Heap

Stack (stack): Advanced back-out

Queue: FIFO

Heap: two fork Tree


Heap: What is a heap. And how to understand it.

The ① heap is usually an array object that can be seen as a tree. The heap always satisfies the following properties:

• The value of a node in the heap is always not greater than or less than the value of its parent node;

• The heap is always a complete binary tree.

The largest heap of the root node is called the maximum heap or large heap, and the smallest heap of the root node is called the minimum heap or small Gan. The common heap has two forks, Fibonacci heaps, and so on.

The ② heap is a memory space that is requested for a size when the program is running, rather than when the program is compiled. That is, allocating memory dynamically, without distinction between access and access to general memory.

The ③ heap is the process by which the application requests the operating system to allocate its own memory when it is running.

The ④ heap refers to the dynamic memory that is applied while the program is running, and the stack refers only to a method that uses the heap (i.e., advanced post-out).



















Stack: What is a stack. And how to understand it.

① Stack, also known as stacks, is a linear table with limited operations. The limitation is that only one end of the table is allowed to insert and delete operations. This end is called the top of the stack, and the opposite end is called the bottom of the stack.

② Stack is a bucket, and then put it in the first to take out, it has something to wait until it comes out (advanced)

The ③ stack (stack) is the storage area that the operating system establishes for a process or thread (a thread in a multithreaded operating system) that has a FIFO feature that specifies the size of the stack that is required at compile time.











Stack: What is a stack. And how to understand it.

Note: In fact, the stack itself is a stack, just replaced by an abstract name.

Characteristics of the stack: the last object placed in the stack is always first taken out, which is often referred to as a LIFO queue. Some actions are defined in the stack. The two most important are push and pop. The push operation adds an element to the top of the stack. The pop operation, in contrast, removes an element at the top of the stack and cuts the stack size by one.











Heap, Stack difference summary:

1. Stack space allocation

① stack (operating system): automatically allocated by the operating system release, store the function parameter value, local variable value and so on. It operates in a manner similar to a stack in a data structure.

② Heap (operating system): Usually released by the programmer, if the programmer does not release, the end of the program may be recycled by the OS, distribution is similar to the list.

2. How the stack is cached

The ① stack uses a first-level cache, which is usually called when it is in storage, and is released immediately after the call.

The ② heap is stored in a level two cache, and the life cycle is determined by the garbage collection algorithm of the virtual machine (it is not possible to be recycled once it becomes an orphan object). So the speed of calling these objects is relatively low.

3. Stack data structure differences

① Heap (data structure): Heaps can be thought of as a tree, such as: heap sort.

② stack (data structure): An advanced post-out data architecture.






Queue: What is a queue. And how to understand it.

The ① queue is a special linear table, except that it allows for deletion only at the front end of the table (front), but in the back-end (rear) of the table, and, like the stack, the queue is a linear table of operations constrained. The end of the insert operation is called the tail of the queue, and the end of the delete operation is called the team header.

When there are no elements in the ② queue, it is called an empty queue.

③ The order queue structure must be statically allocated or dynamically applied for a contiguous amount of storage space, and set 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.

The ④ queue takes a FIFO (first in first out), and the new element (the element waiting to enter the queue) is always inserted at the end of the list, and reads always from the head of the linked list. Each time an element is read, an element is freed. The so-called dynamic creation, dynamic release. Therefore, there are no overflow problems. Because the list is indirectly formed by the structure, the traversal is also convenient. (Advanced first Out)










The difference between heaps, stacks, and queues is.

The ① heap is a memory space that is requested for a size when the program is running, rather than when the program is compiled. That is, allocating memory dynamically, without distinction between access and access to general memory.

② Stack is a bucket, and then put in the first to take out, it has something to wait until it comes out. (last in, first out)

③ queues can only be deleted at the head of the team, and insert operations at the end of the queue. The stack can only be inserted and deleted at the top of the stack. (Advanced first Out)


















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.