Some basic terms of the data structure:
Data: Symbolic representation of objective things
Data element: An individual in a data set
Data items make up data elements
A data object is a subset of data composed of data elements of the same nature
Data structure: A collection of elements with structures
Data structures can be represented by a four-tuple (D,l,s,o)
Data, logical structrue, storage struction operation operation
l--set, linear, tree, graphic
s--storage structure, sequential, chained, hashed
Linear table-linear structure, a one-to-one relationship between data elements,
n a finite sequence of data elements of the same type
L= (A1,a2,.... an)
Direct precursor, direct successor
Data structures are stored in sequential structure and chained storage
Chained--storing data elements with a set of arbitrary discontinuous storage units
A linked list is determined by a unique head pointer, and the data elements in the linked list are called nodes.
Single Linked list: data fields and Node domains
Stack and queue logical structure are the same as linear tables
If the number of elements in the stack varies greatly or the number of stack elements is not clear, the chain storage structure should be considered. A stack represented by a chained storage structure is called a "chain stack." A chain stack is usually represented by a single-linked list of headless nodes. :
The operation of the stack is a special case of linear table operations.
Queue is also an operation constrained linear table, its operation limit is different from the stack, there are restrictions on both ends, the insertion can only be done at one end of the table (only not in), and the deletion can only be done at the other end of the table (only out), allowing the deletion of one end called the tail (rear), allowing the end of the Front)
, the operation principle of the queue is FIFO, so the queue is also known as the FIFO table (first
What about "false overflow"?
Because here, our queue is stored in a vector space, in this contiguous storage space, by a queue head pointer and a tail pointer to represent the queue, the leader pointer and the tail pointer to the same position, the queue is empty, that is, the queue is composed of two pointers in the middle of the elements. In the queue, and the team is not the same as the reality, the elements move forward, the end is not, but the pointer in the movement, when the team operation, the head pointer forward (that is, the tail of the vector space) to add a position, the queue, the tail of the pointer forward to add a position, in some cases, such as into a Two pointers continue to move forward, until the queue where the tail of the vector space, then the team, the tail pointer will run to the outside of the vector space, only when the entire vector space is empty, the queue is empty, but produced an "overflow" phenomenon, which is false overflow.
In order to overcome the space waste caused by this phenomenon, we introduce the concept of cyclic vectors, like a vector space bent to form a tail-to-toe ring, so that when the queue in which the tail-end pointer moves to the upper bound (tail) of the vector space, plus 1 of the operation (queue or out of the team) to the pointer to the lower bound That is, starting from scratch. The queue at this point is called the loop queue.
Most of our applications are circular queues. Because of the loop, the end-of-light is overlapping, and we can't tell if the queue is empty or full, then we need to deal with some boundary conditions to distinguish whether the queue is empty or full. There are at least three methods, one is another Boolean variable to judge (is to ask others to look at, is empty or full of his decision), the second is to use less an element space, when the queue, first test the queue rear pointer is not equal to the head pointer, if equal even if the team is full, not allowed to queue. The third is to use a counter to record the total number of elements in the queue, so that you can always know the length of the queue, as long as the number of elements in the queue is equal to the length of the vector space, is the team full.
PHP's Hanoi algorithm
- <?php
- function Hanoi ($n, $x, $y, $z) {
- if ($n ==1) {
- Move ($x, 1, $z);
- }else{
- Hanoi ($n-1, $x, $z, $y);
- Move ($x, $n, $z);
- Hanoi ($n-1, $y, $x, $z);
- }
- }
- function Move ($x, $n, $z) {
- echo ' Move disk '. $n. ' From ' $x. ' to ' $z. ' <br/> ';
- }
- Hanoi (, ' x ', ' y ', ' z ');
- ?>
Data structure and algorithm review first day-basic concept, linear table