C # Data Structure Stack,
Stack and queue are two very important data structures. Stack and queue are also linear structures. The logical relationships between data elements and elements are the same for linear tables, stacks, and queues.
The difference is that the operations on Linear tables are unrestricted, and stack and queue operations are limited (following certain principles). Therefore, stacks and queues are also called restricted linear tables.
Stack definition: operate a linear table at the end of the table, TOP of the stack, Bottom of the stack: Bottom. No data in the Stack: Empty Stack
Representation: S = (a1, a2, a3, a4 ........ An) a1 is the element at the bottom of the stack, and an is the element at the top of the stack. These N pieces of data are inserted into the stack in sequence, and the opposite is found in the stack.
Follow the principle (Last In First Out is LIFO) or (First In Last Out is FILO)
There are also many columns in real life: washing dishes and using dishes
C #2.0 and earlier versions only provide non-generic Stack classes that inherit the ICollection, IEnumerable, and ICloneable interfaces. C #2.0 provides generic
Stack <T> class, which inherits the IEnumerable <T>, ICollection, and IEnumerable interfaces.
Stack storage and implementation
1. Ordered Stack: uses a continuous storage space to store the elements in the stack. The continuous space is represented by an array.
2. Chain Stack: operations are performed on the basis of a Linear Linked List, that is, the storage structure adopts the form of a linked list and the operation adopts the FILO method.
Examples in life
Is there any other way to use it besides washing dishes?
1. Numeric conversion is to convert a non-negative decimal number to another hexadecimal number. The general solution is to divide the conversion phase. For example: decimal 5142
Octal:
As shown in the figure, (5142) 10 = (12026) 8
Conversion ideas:
1. Judge that N is not 0 N % 8 and press it into the stack.
2. Judge that N is not 0 N % 8 and press it into the stack.
The last N is 0, so that all the data in the stack is POP one by one to get the octal value.
2. Common Problems in programming: parentheses matching. to simplify it, there are only two matching brackets: () and []. The nesting order is arbitrary.
([] () Matching [() [()] [] matching [(]) does not match. How can we judge if a bunch of such matching symbols are added?
Idea: 1. If the stack is empty, PUSH
2. If the brackets match the brackets on the top of the stack, POP the brackets on the top of the stack.
3. If the brackets do not match the brackets at the top of the stack, PUSH the brackets.
4. Check whether the stack is empty at the end of the process. If it is empty, the stack matches. If it is not empty, the stack does not match.
3. Common arithmetic expressions ..... Think for yourself ......
To be continued .........