Data structure (C implementation) ------- chain Stack
Description:
The chain stack is the chain storage structure of the stack. The chain stack is usually represented by a single-chain table with no leading node. Therefore, the structure of the node is the same as that of the single-chain table.
In a chain stack, the bottom of the stack is the last node of the linked list, and the top of the stack is always the first node of the linked list. Therefore, the elements of the new stack are newly added nodes using the header plug method in the linked list. A chain stack can be uniquely identified by the top pointer of the stack. When the top is NULL, it indicates that the stack is an empty chain stack.
Implementation:
Type description of the stack node:
typedef int ElemType;typedef struct node{ElemType data;struct node *next;}LinkNode,*LinkStack;
Basic operations
1. initialize the chain stack Init_LinkStack ()
The initialization operation of the chain stack is to create an empty single-chain table with no leading node, as follows:
// Initialize the chain stack LinkStack Init_LinkStack () {LinkStack S; S = NULL; return S ;}
2. Determine the empty chain stack IsEmpty_LinkStack (LinkStack top)
According to the definition, when the top pointer of the stack is NULL, it indicates that this node is an empty stack and 1 is returned. Otherwise, 0 is returned.
// Judge the empty chain stack int IsEmpty_LinkStack (LinkStack top) {if (top = NULL) return 1; elsereturn 0 ;}
3. Import chain stack Push_SqStack (SqStack * S, ElemType x)
Stack entry: inserts an element into the stack as the new top element of the stack. First, you must dynamically apply for a node as the storage space of the new element, then, write the new data element to the applied bucket, write the value of the top pointer of the stack to the pointer field in the new node, and finally point the top pointer of the stack to the newly inserted node. The Code is as follows:
// Void Push_SqStack (SqStack * S, ElemType x) {// when the stack is full, exit if (isFull_SqStack (S) {printf ("the stack is full! \ N "); exit (0) ;}else {S-> data [++ (S-> top)] = x ;}}
4. Output chain stack Pop_SqStack (SqStack * S, ElemType * x)
Output Stack: outputs an element from the stack and deletes it. The process is as follows: when the top element of the stack goes out of the stack, first judge whether the top pointer of the stack is null. If it is null, output the prompt information and exit. Otherwise, return the value of the top element of the stack, move the top pointer of the stack backward, and release the storage space of the top element of the deleted stack.
// Out-of-stack void Pop_SqStack (SqStack * S, ElemType * x) {// if the stack is empty, a prompt message is output and if (isEmpty_SqStack (S) is exited )) {printf ("Stack empty! \ N "); exit (0);} else * x = S-> data [S-> top --];}
5. Read the Top_SqStack (SqStack * S, ElemType * x) element at the top of the chain stack)
Reading the top element of the stack is different from that of the outbound stack. The difference between the two is that when reading the top element of the stack, the top pointer of the stack does not change and only gets the value of the top element of the stack; in addition, the top element of the stack needs to be deleted, and the top pointer of the stack also needs to change at this time. However, both must determine whether the stack is empty.
// Read the top element void Top_SqStack (SqStack * S, ElemType * x) {if (isEmpty_SqStack (S) {printf ("Stack empty! \ N "); exit (0);} else * x = S-> data [S-> top];}
6. Output all the elements in the stack Print_SqStack (SqStack * S)
Traverse the entire chain Stack from the top of the stack and output all elements. Similarly, You need to determine whether the chain stack is empty.
// Output the whole stack void Print_SqStack (SqStack * S) {if (isEmpty_SqStack (S) {printf ("Stack empty! \ N "); exit (0);} int length = S-> top; while (length>-1) printf (" % d \ t ", s-> data [length --]); printf ("\ n ");}
Note:
The above are only the most basic operations of the chain stack. Of course, in actual applications, operations such as inbound and outbound stacks are often involved, and access to elements not on the top of the stack is also required.
Comparison of sequential stack and chain Stack:
1. The sequence stack is easy to perform relative displacement based on the position of the top pointer of the stack to quickly locate and read the internal elements of the stack. Therefore, the sequence stack is more widely used than the chain stack.
2. the time complexity of reading internal elements from the sequential stack is O (1), and the time complexity of reading internal elements from the trace stack is O (n). Where, n is the length of the trace stack.