Data Structure Algorithm - Stack and Queue

Stacks and queues are easier to understand in data structure and algorithm learning. But sometimes I feel that I have mastered the stack and the queue, but when I encounter different situations when I write, I will not deal with it, so I still want to review it again.

Stack:
is a linear table with one end limited and one end allowed to operate. That is: first take the first take, then put the first take. It is what we usually call "advanced out" (FILO). There are two most common storage structures: one is sequential storage and the other is chained storage. The sequential storage is the array that was said before, and the chain storage is the linked list.

queue:
Like a stack, a queue is also a linear table whose characteristics are “first in, first out” (FIFO), inserted at one end and deleted at the other end. Just like queuing, the person who just came in is going to be at the end of the team. Every time the pop is the leader of the team.

The specific usage scenarios are still quite a few such as: hex conversion, infix and suffix expressions, maze solution, text editor, binary tree traversal, etc., the solution needs to use the idea of ​​stack and queue. Let's write the implementation of the stack and queue.

/**
   * Determine if the stack is empty
 */
template<class E>
bool Stack<E>::isEmpty() {
    return top == -1;
}

/**
   * Top element bomb stack
 */
template<class E>
E Stack<E>::pop() {
    assert(top >= 0);
    return array[top--];
}

/**
   * Get the top element of the stack
 */
template<class E>
E Stack<E>::peek() {
    assert(top >= 0);
    return array[top];
}

/**
   * Element stack
 */
template<class E>
void Stack<E>::push(E e) {
    if (top + 1 == size) {
        growArray();
    }
    array[++top] = e;
}

/**
   * Expansion array
 */
template<class E>
void Stack<E>::growArray() {
    size += size >> 1;
    array = (E *) realloc(array, size * sizeof(E));
}

Using arrays to implement a stack is pretty straightforward. If you use arrays to implement a queue? The queue is FIFO, so we need to remove the first element each time we queue, because the storage structure is an array, the following elements need to be anteriorly traversed, the time complexity is O(n). At this time, we might as well think that there is no way to convert the time complexity to O(1) level without using the logic element. the answer is:Bidirectional array

template<class E>
ArrayQueue<E>::ArrayQueue(int size) {
    / / Make sure the length of the array is a power of 2
    int init_size = 8;
    if (size >= init_size) {
        init_size = size;
        init_size |= init_size >> 1;
        init_size |= init_size >> 2;
        init_size |= init_size >> 4;
        init_size |= init_size >> 8;
        init_size |= init_size >> 16;
        init_size += 1;
    }
    array = (E *) malloc(sizeof(E) * init_size);
    this->size = init_size;
}

template<class E>
void ArrayQueue<E>::push(E e) {
    head = (head - 1) & (size - 1);
    array[head] = e;
    if (head == tail) {
        growArray();
    }
}

template<class E>
E ArrayQueue<E>::pop() {
    tail = (tail - 1) & (size - 1);
    return array[tail];
}

template<class E>
E ArrayQueue<E>::peek() {
    return array[(tail - 1) & (size - 1)];
}

template<class E>
bool ArrayQueue<E>::isEmpty() {
    return tail == head;
}

template<class E>
ArrayQueue<E>::~ArrayQueue() {
    delete[] array;
}
/**
 * Open up new arrays and adjust element order
**/
template<class E>
void ArrayQueue<E>::growArray() {
    int new_size = size << 1;
    E *new_array = (E *) malloc(new_size * sizeof(E));

         // Align the elements behind the array to the front
    int r = size - tail;
    copyArrayElement(array, head, new_array, 0, r);
         // Align the elements in front of the array to the back
    copyArrayElement(array, 0, new_array, r, head);
         // release the memory
    free(array);
    array = new_array;
         // Change the pointer again
    head = 0;
    tail = size;
    size = new_size;
}

Video link:
Video password: bmmt

Intelligent Recommendation

Data structure and algorithm stack and queue

Stack Stack application Stack interface design Effective braces (Leetcode 20 questions) Original method User stack solution Use stack + HashMap solution queue Queue interface design Use stack to achie...

Data Structure and Algorithm - Stack and Queue

Stack and queue 1. Design a stack that get the smallest value of the current stack 1. Design a stack that get the smallest value of the current stack Design a stack that can get the smallest value of ...

Data structure and algorithm _ stack and queue

Stack concept As mentioned in the title, the stack is a constrained linear structure. We gave a regulation in a linear structure: the first one in the first one. This is like a book is placed on the g...

Data Structure and Algorithm - [Stack and Queue]

Introduction Stack is linear form that defines only inserted and deleted operations at the end of the tail. The queue is only allowed to perform a linear table for deleting operations on the other end...

Data structure and algorithm. Stack, queue

Stack, queue One, stack Second, the queue One, stack Definition: A limited linear structure Features: 1 The data in the stack complies with 'advanced first "principle 2 can only operate data on t...

More Recommendation

Data structure and algorithm [stack and queue]

Table of contents 1. Stack structure and queue structure 1. What are stacks and queues? 2. The structure of the stack and queue 3. The implementation of the array stack and linked list stack (Java lan...

Data structure and algorithm-stack, queue (queue)

First, give two common examples in life. I believe everyone will encounter this kind of phenomenon when using computers for work and entertainment. When we click on the program or perform other operat...

Data structure and algorithm linear structure stack and queue

Data structure and algorithm linear structure stack and queue About the stack and queue, its characteristics are, stacks: Backward first out, queue: advanced first out; through Java code is implemente...

Data structure and algorithm stack (stack), queue (queue), package (bag)

@TOC Data structure and algorithm stack (stack), queue (queue), package (bag) First, the data structure (1) Package: The package can be regarded as a bag. It is thrown into the stone when it meets on ...

[Data Structure and Algorithm 02] Stack and Queue

We know that in the array, if you know the subscript of the data item, you can immediately access the data item, or by searching the data item sequentially, accessing each data item in the array. But ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top