This blog is intended to consolidate the basics, master please bypass. Section code and content reference Min People's post and telecommunications publishing house "data Structure"
Stacks and queues
Stack: a linear table that restricts insertion or deletion only at one end
Top: One end of the INSERT, delete operation is allowed, also known as the footer.
Stack Bottom (Bottom): Is a fixed end, also known as a table header.
Empty stack: When there are no elements in the table, it is called an empty stack.
In the stack is first put in the stack of elements at the bottom of the stack, the last element placed on the top of the stack, and the deletion of the element is just the opposite, the last element is deleted first, the first element is deleted.
Features: LIFO LIFO (last on First out)
Application of Stacks
Test for matching parentheses
It is assumed that two types of parentheses are allowed in the expression: parentheses and brackets, which are nested in a random order, i.e. ([] ()) or [([]]) and so on in the correct format, [(]) or ([()) or ()]) are in an incorrect format.
Algorithm idea: Set a stack, when reading the opening parenthesis, the left parenthesis into the stack. When the closing parenthesis is read, an element is popped from the stack, matched to the left parenthesis read, and continued to be read if the match succeeds, otherwise the match fails and returns flase.
Algorithm description
#define TRUE 0#define flase -1sqstack S; S=init_stack (); /* Stack initialization */int match_brackets () { char ch, x; scanf ("%c", &ch), while (ASC (CH)!=13) { if ((ch== ' (') | | (ch== ' [')] Push (S, CH); else if (ch== '] ') { x=pop (S); if (x!= ' [') { printf ("' [' parenthesis Mismatch"); return flase ; } } else if (ch== ') ') { x=pop (S); if (x!= ' (') {printf ("' (' parenthesis mismatch '); return flase ;} } }if (s.top!=0) { printf ("The number of parentheses does not match!") "); return flase ;} else return TRUE ;}
Queue
Queue: a linear table that restricts the insertion at one end and the deletion at the other end.
Team header: The one end of the allowed delete is called the team header (front)
End of line: one end of the allowed insert is called the tail of the queue (rear)
Empty queue: An empty queue when there are no elements in the queue
Features: FIFO First Out
Chain representation and implementation of queues
A chained storage structure for a queue is simply a chain queue, which is a single linked list that restricts the insertion of a table header to a delete operation and footer. Two different types of nodes are required: Data element node, queue's first pointer, and tail pointer node.
Chain operations and pointer changes
The operation of the chain is actually a single-linked list operation, except that it is deleted at the table header and inserted at the end of the table. The different pointers are modified separately when inserting or deleting.
Data Structure Learning Experience Series (ii)