Data Structures and Algorithms - Data Structure Knowledge induction

C ++, memory is divided into five zones: heap, stack, free store, global / static storage area and the constant storage area.

  • Stack: automatically allocated by the compiler when necessary, automatically clear the variable storage area when not needed. Typically store local variables, function parameters.
  • Heap: new block of memory is allocated, is released by the programmer (regardless of the compiler), a generally corresponds to a new delete, a new [] and a delete [] correspond. If a programmer is not freed, the operating system will automatically recover resources at the end of the program.
  • Free store: a block of memory allocated by malloc and the like, and is very similar to the stack, with free to release.
  • Global / static storage area: Global and static variables are assigned to the same block of memory (in C, global variables is divided into initialized and uninitialized, C ++ is not that distinction).
  • Constant storage area: This is a special storage area inside the store constants can not be modified.

(Note: The heap and the free store but it is in fact the same area, new underlying implementation code calls malloc, new advanced version of malloc can be seen as intelligent)

 

A heap and stack

The difference between (1) the heap and stack

  1.  Management: the resource stack controlled by the programmer (prone memory leak), the stack is automatically managed by the resource compiler, without manual control.
  2.  System Action: For a heap, the system should be aware there is a linked list of free memory address of record, when the system receives the application program, traverse the linked list, the first node to find a stack space is larger than the application space, the free node list to delete the the node and assign the node to the space programs (most systems of this size will be recorded in the first allocation of this memory address space, so delete the right to release this memory space, in addition to the excess part of the system will re-release into the free list). For the stack, the stack as long as space is greater than the remaining space applications, the system provides memory for the program, or report an exception in the stack.
  3.  Space: discontinuous heap memory area (because the system is used to store the linked list of free memory address, of course, not continuous), the computer system is limited by the size of the heap in a virtual memory efficient (32bit system theoretically 4G), so heap space is relatively flexible, relatively large. The stack is a contiguous memory area, the predetermined size is a good operating system, the windows stack size is 2M (there is 1M, determined at compile time, VC may be provided).
  4.  Debris problem: For a heap, frequent new / delete will cause a lot of debris, so reducing the efficiency of the procedure. For the stack, it is after a last-out queue, and out of correspondence, no debris.
  5.  Growth direction: pile up, up to the higher addresses. Down the stack, up to the lower addresses.
  6.  Distribution: heap is allocated dynamically (not statically allocated heap). Stack static allocation and dynamic allocation, static allocation by the compiler is completed (e.g., local variables assigned), assigned dynamically allocated by alloca function, but the dynamic allocation of the resource stack to be released by the compiler, the programmer need not achieved.
  7.  Allocative efficiency: Heap provided by the C / C ++ libraries, the mechanism is very complex. So heap efficiency is much lower than the stack. System extremely stack data structure is provided, at the bottom of the stack to provide computer support, specifically allocated stack address storage register, a dedicated stack operation instruction.

 (2) Program Operation

  • Stack (Stack) : last-out (FILO)
stack<int> stk;
stk.empty()     如果栈为空返回true,否则返回false;
stk.size()     返回栈内元素的大小;
stk.pop()      从栈顶弹出一个成员;
stk.push()     向栈内压入一个成员;
stk.top()      返回栈顶,但不删除成员;
  • Queue (Queue) : First In First Out (FIFO)
queue<int> que;
que.empty()      如果队列为空返回true,否则返回false;
que.size()       返回队列内元素的大小;
que.pop()       从队列弹出一个成员;
que.push()       向队列压入一个成员;
que.front()      返回到队首,但不删除成员;
que.back()       返回到队尾,但不删除成员;

       Tree is a collection of nodes, edges between nodes with links between nodes can not have a loop. The upper node is called a parent node, lower node is called a child node. Topmost node is called the root node.

      Binary Tree is a special tree. For each node, a child node directly connected therewith not more than two (can be 0). The left child node called the left subtree, the child node on the right called the right subtree. A binary tree is shown below:

Some concepts related to tree:

  •  Leaf (leaf node): no nodes any child nodes.
  • Depth: For any node N, the depth refers to a unique path from the root node N of length. Depths of 0. The depth of the deepest leaf node of the tree depth. It can be understood as: a root entry is, the farther away from the roots, deeper. As Figure: depth A, B, C is 1, D, E is a depth of 2.
  • High: For any node N, to a length N from the farthest leaves of the path as the N height. (Only from top to bottom you can not pass from the lower to the node.) High leaves is 0. High tree root is high. As shown above, the height of the root of 2. A 1 is the height, the height of the other nodes 0.
#include <algorithm>
make_heap: 根据指定的迭代器区间以及一个可选的比较函数,来创建一个heap. O(N)
push_heap: 把指定区间的最后一个元素插入到heap中. O(logN)
pop_heap: 弹出heap顶元素, 将其放置于区间末尾. O(logN)
sort_heap:堆排序算法,通常通过反复调用pop_heap来实现. N*O(logN)针。

c++11
is_heap: 判断给定区间是否是一个heap. O(N)
is_heap_until: 找出区间中第一个不满足heap条件的位置. O(N)

 

Second, the list

      List storage unit is a physically non-contiguous, non-sequential storage structure, the logical order of the data elements is achieved by the link pointer in the linked list order. Chain by a series of nodes (each node element is called a linked list), with the node can be dynamically generated at runtime. The different nodes can be divided into unidirectional list comprising contents and doubly linked lists.

Way linked list of nodes:

  1. Data field: a value for storing the data elements.
  2. Pointer field (domain chain): for storing a next node address or point of its direct successor node pointers. 
 struct Node{
     int value;
     Node * next;
 };

Doubly linked list of nodes:

  1. Data field: a value for storing the data elements.
  2. The left pointer field (left chain domain): a node address for the storage or directly pointing a pointer to a predecessor node.
  3. The right pointer field (right field chains): for storing a next node address or point of its direct successor node pointers.
 struct DNode{
     int value;
     DNode * left;
     DNode * right;
 };

Data field: a value for storing the data elements.
Pointer field (domain chain): for storing a next node address or point of its direct successor node pointers. Common operations:

//插入节点(单向链表)
//p节点后插入值为i的节点
void insertNode(Node *p, int i){
    Node* node = new Node;
    node->value = i;
    node->next = p->next;
    p->next = node;
}

//删除节点(单向链表)
void deleteNode(Node *p){
    p->value = p->next->value;
    p->next = p->next->next;
}
  • List of library implementation using stl list
//创建链表
list<int> lst1;                          //创建空list
list<int> lst2(5);                       //创建含有5个元素的list
list<int> lst3(lst2);                    //使用lst2初始化
list<int> lst4(lst2.begin(),lst2.end()); //使用lst2初始化

//基本操作
lst1.assign() 给list赋值 
lst1.back() 返回最后一个元素 
lst1.begin() 返回指向第一个元素的迭代器 
lst1.clear() 删除所有元素 
lst1.empty() 如果list是空的则返回true 
lst1.end() 返回末尾的迭代器 
lst1.erase() 删除一个元素 
lst1.front() 返回第一个元素 
lst1.get_allocator() 返回list的配置器 
lst1.insert() 插入一个元素到list中 
lst1.max_size() 返回list能容纳的最大元素数量 
lst1.merge() 合并两个list 
lst1.pop_back() 删除最后一个元素 
lst1.pop_front() 删除第一个元素 
lst1.push_back() 在list的末尾添加一个元素 
lst1.push_front() 在list的头部添加一个元素 
lst1.rbegin() 返回指向第一个元素的逆向迭代器 
lst1.remove() 从list删除元素 
lst1.remove_if() 按指定条件删除元素 
lst1.rend() 指向list末尾的逆向迭代器 
lst1.resize() 改变list的大小 
lst1.reverse() 把list的元素倒转 
lst1.size() 返回list中的元素个数 
lst1.sort() 给list排序 
lst1.splice() 合并两个list 
lst1.swap() 交换两个list 
lst1.unique() 删除list中重复的元素

 

reference

https://blog.csdn.net/u013846293/article/details/79410293?utm_source=blogxgwz1

Heap and stack reference: https://blog.csdn.net/yangyong0717/article/details/78001609

Reference list: https://www.cnblogs.com/byonecry/p/4458821.html

Binary Reference: https://blog.csdn.net/u014182411/article/details/69831492/

                     https://blog.csdn.net/flyyufenfei/article/details/78175511

Published 53 original articles · won praise 186 · views 180 000 +

Guess you like

Origin blog.csdn.net/Kalenee/article/details/83180311