First, stack
1. Definition of stacks
A stack is a linear table, an abstract data type that allows only one end of an insert or delete operation. Also known as the LIFO (LIFO) Linear table.
The basic operation of the stack is in the stack push and out of the stack pop, the top of the stack refers to the one end of the operation. , only the top elements of the stack can be accessed. The stacking order is A1, A2, A3, A4, A5, and the sequence is A5, A4, A3, A2, A1
2. Implementation of the Stack
The stack is a constrained linear table, so any method that implements the table can implement the stack, mainly the sequential stack and the chain stack.
2.1 Sequential Stacks
using an array of data elements to hold the stack, with a head pointer top stacks top, the initial top=0, the implementation code of the sequential stack:
into the stack: first placed in the top+1
Public void push (T item) { ifreturn; = item; ++;}
out Stack: first top-1 and then fetch
Public T Pop () { ifreturnnull; return (T) items[--top];}
2.2 Link Stacks
It is usually implemented with a single-linked list, and all operations are performed at the table header. The stack nodes are:
Private class node{ T item; Node next;}
The implementation code of the chain stack, with a head pointer top, can only be executed with the top pointer push, pop (insert delete)
into the stack: head interpolation method
Public void push (T item) { = top; New Node (); = item; = Old ; Count+ +;}
stack: simply point the top node to the next node when fetching
Public T Pop () { = top.item; = Top.next; Count--; return item;}
Two, queue 1. Queue
A linear table that is inserted only at one end and deleted at the other end, similar to the daily queueing, is characterized by FIFO. There are two pointers, head and tail pointers tail, tail inserted, read at head.
2. Implementation of queues 2.1 sequential storage
When using an array, the initial state, head=tail=0; When tail==maxsize, does not indicate that the queue is full, when the head pointer is the previous position of tail, when there is only one element in the queue, but not full, then queued overflow, then a false overflow.
2.1.1 Loop Queue
also known as the ring queue, ring buffer, figment a ring of space, when the team head or team first pointer reached the maximum, reset to zero, you can use the take-up operation to achieve, (also can not take redundancy).
Take the remainder operation:
Initially, head = Tail = 0= (head + 1)%= (tail + 1)%+ maxsize-head)% MaxSize;
Do not use the remainder:
Take the Ringbuffer ring buffer as an example, the specific code in GitHub, where the queue and the team out of the code as follows, not using modulo operation, (for the computer, plus 1 minus 1 more simple than the mold)
Team:
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 ;}
Out team:
Public T Get () { if(head = = tail) {returnnull; } @SuppressWarnings ("Unchecked") = (T) rbuffer[head]; NULL ; if (++head >= buffersize) { = 0; } return item;}
2.2 Chain-Store
Chain queue, using a single-linked list with head head and tail tail pointers, read at head, insert in tail. The node types are as follows:
Private // Team Head Private // End of Team Private class // node T Item; Node next;}
Team:
Public void Enqueue (T item) { = tail; New Node (); = item; NULL ; if (IsEmpty ()) { = tail; Else { = tail; } Count+ +;}
Out team:
Public T dequeue () { = head.item; = Head.next; if (IsEmpty ()) { null; } Count--; return Res;}
Source Address: Https://github.com/cyhe/algorithm
Stacks and queues