Notes data structure (III) - stacks and queues

  1. The stack is defined only insertions and deletions in the end of the table (stack) the linear form. Again become the last in first out ( LIFO ) linear form.
    · Sequential storage stack structure: array-based (subscript 0 is a length of the bottom of the stack);
    Storage Structure · Stack: based on the list (a single head of the list on top of the stack; link stack usually do not need the head node ).

  2. STL stack container adapter
    Here Insert Picture Description
    template · stack container adapter has two parameters. The first parameter is stored in the object type, the second parameter is the type of underlying containers. The stack bottom container default deque container , thus actually template type stack <typename T, typename Container = deque>.
    * When creating the stack, is not used to initialize the object initialization list, but can be used to initialize another container , as long as the same type of container and the type of underlying stack containers.

std::list<double> values {1.414, 3.14159265, 2.71828};
std::stack<double,std::list<double>> my_stack (values);
  1. Basic operation STL stack
    · Top () : returns a reference to the top element of type T &.
    · Push (const & T obj) : a copy of the object may be pressed into the top of the stack, the bottom of the container by calling push_back () function is completed.
    · Push (obj && T) : a way of moving objects onto stack object, there push_back rvalue reference parameters by calling the bottom of the container () function is.
    · POP () : pop the top element.
    · Size () : Returns the number of elements in the stack.
    · Empty () : returns true no elements in the stack under the circumstances.
    · Emplace () : use the parameters passed in the constructor is called, the object is generated in the stack.
    · Swap (Stack & other_stack) : the current stack elements and elements from the parameters exchange. Stack it must contain the same type of parameter elements and the current. For the special case of a stack object has global function swap () can be used.

  2. Application
    · Base conversion : Given any decimal integer n, which is converted to a binary representation.
    Here Insert Picture Description
    · Stack shuffling : three stacks A, B, S (B, S is initially empty), the pop the top element is pressed into S A, S or pop the top element is pressed into B, until all the elements A transferred to B.
    (For <1,2,3, ..., n] shuffled stack, if and only if for any i <j, the stack does not contain shuffle pattern [..., j + 1 ,. ... , i, ..., j , ...> , the "312" mode)
    Here Insert Picture Description
    * Bracket matching : in case of push left parenthesis, right parenthesis case of the stack.
    Here Insert Picture Description
    · Infix expressions : sequentially push operands, operators need to compare the priority of the current top of the stack operators operator (low stack: push current; high stack: the stack results; equal: and to receive the next character in parentheses)
    Here Insert Picture Description
    * reverse Polish Notation (RPN; also known as postfix notation): operand stack; in case the number of operators required to operate first find out, after the operation stack.
    Here Insert Picture Description
    Inverse Polish expression: The expression in infix, operands each operator involved in parentheses, the operator moves to a corresponding bracket, and then to remove all brackets.

  3. Queue: insertion operation only at one end and at the other end of the linear table delete operation. A queue is a FIFO ( the FIFO ) in linear form, to allow the insertion end is called the tail, allowed to delete called queue head end.
    · Queue also has a storage order and chain store in two ways.

  4. Circular queues: sequential storage structure end to end. Order to solve the problem of false overflow storage (team head full of empty the tail).
    Suppose front pointer to the head elements, REAR pointer to the next position of the tail element . If the maximum queue size is QueueSize, then queue full condition is (+ REAR. 1) == Front QueueSize% ; universal calculating queue length of formula (rear- QueueSize Front +) QueueSize% .

  5. Chain queues: queue head pointer to the head node; the tail pointer to the end node.

  6. STL queue using
    Here Insert Picture Description
    · Front () : Returns the first element in the queue of reference.
    · The Back () : Returns the queue referenced in the last element.
    · The Push (const T & obj) : Add a copy of the elements in the tail of the queue.
    · The Push (T && obj) : mobile way to add elements to the end of the queue.
    · POP () : Removes the first element in the queue.
    · Size () : Returns the number of elements in the queue.
    · Empty () : If there are no elements in the queue, then returns true.
    · Emplace () : with a pass emplace () parameters T constructor calls, generating object at the end of the queue.
    · Swap (queue & other_q) : The current queue elements and parameters queue elements in the exchange.

Published 12 original articles · won praise 0 · Views 123

Guess you like

Origin blog.csdn.net/weixin_43279709/article/details/104789994