Stack and queue, queue
I. Stack
1. Stack Definition
Stack is a linear table and an abstract data type. It allows insert or delete operations only at one end. It is also called the LIFO linear table.
The basic stack operations include push and pop. The top of the stack refers to the end of the operation ., Only the top elements of the stack can be accessed. The stack order is a1, a2, a3, a4, and a5. The stack order is a5, a4, a3, a2, and a1.
2. stack implementation
Stack is a restricted linear table, so any method to implement the table can implement the stack, mainlySequential stack and chain stack.
2.1 sequential Stack
Use an array to store the data elements of the stack. A header pointer is provided to top the execution stack and top = 0 at the beginning. The implementation code of the sequential stack is as follows:
Stack entry:First put in top + 1
public void push(T item){ if(top >= capacity) return; items[top] = item; top ++;}
Output Stack:First top-1 and then get
public T pop(){ if(top < 0) return null; return (T) items[--top];}
2.2 chain Stack
Generally, a single linked list is used. All operations are performed on the header. Stack node:
private class Node{ T item; Node next;}
The implementation code of the chain Stack has a head pointer top, which can only be used to execute push and pop (insert and delete) with the top pointer)
Stack entry:Header Insertion Method
public void push(T item){ Node old = top; top = new Node(); top.item = item; top.next = old; count++;}
Output Stack:You only need to point the top node to the next node.
public T pop(){ T item = top.item; top = top.next; count--; return item;}
Ii. Queue 1. Queue
A linear table that is inserted at one end and deleted at the other end is similar to a regular queue and features FIFO. There are two pointers, head and tail, which are inserted in tail and read in head.
2. Queue implementation 2.1 sequential Storage
When an array is used, head = tail = 0 in the initial state. When tail = MaxSize, it does not indicate that the queue is full. When the head pointer is the previous position of tail, at this time, there is only one element in the queue, but it is not full. In this case, a false overflow occurs when you join the queue.
2.1.1Cyclic queue
This is also called a ring queue and a ring buffer. It creates a ring space. When the first or first pointer of the queue reaches the maximum value, it is reset to zero. This can be achieved through the remainder operation, (The remainder is also not required ).
Remainder operation:
Initially, head = tail = 0; head = 1, head = (head + 1) % MaxSize; head plus 1, tail = (tail + 1) % MaxSize; queue Length, (tail + MaxSize-head) % MaxSize;
Do not use the remainder:
Take the RingBuffer ring buffer as an example. The specific code is on GitHub. The code for queuing and queuing is as follows. The modulo operation is not used. (for computers, adding 1 minus 1 is easier than modulo)
Join:
public boolean put(T item){ int next = tail + 1; if(next >= bufferSize){ next = 0; } if(next == head){ lost++; return false; } rBuffer[next] = item; tail = next; return true;}
Departure:
public T get(){ if(head == tail){ return null; } @SuppressWarnings("unchecked") T item = (T) rBuffer[head]; rBuffer[head] = null; if(++head >= bufferSize){ head = 0; } return item;}
2.2 chain Storage
A chain queue uses a single-chain table with the head and tail pointers of the team head, reads the table in the head, and inserts the table in the tail. The node types are as follows:
Private Node head; // private Node tail of the team head; // private class Node of the team end {// Node T item; Node next ;}
Join:
public void enqueue(T item){ Node old = tail; tail = new Node(); tail.item = item; tail.next = null; if(isEmpty()){ head = tail; } else { old.next = tail; } count++;}
Departure:
public T dequeue(){ T res = head.item; head = head.next; if(isEmpty()){ tail = null; } count--; return res;}
Source Code address: https://github.com/cyhe/algorithm/tree/master/src/algo/linearlist/stack