07. Stack (1) storage structure and operations of stacks, 07 storage structure operations

Source: Internet
Author: User

07. Stack (1) storage structure and operations of stacks, 07 storage structure operations
I. Stack1.Stack): It is a linear table that is only inserted and deleted at the end of the table. The end that allows insertion and deletion is called the top, the other is called the bottom, and the stack without any data elements is called the empty stack. Stack is also called a linear table of Last In First Out (LIFO. Stack insertion is performed on the stack, and stack deletion is performed on the outbound stack.2. Abstract Data Types of stacksThe ADT stack (stack) Data is a linear table. The element has the same type, and the adjacent element has the precursor and Successor Relationship. Operation InitStack (* S): Initialize the Operation to create an empty stack S. DestoryStack (* S): If the stack exists, destroy it. ClearStack (* S): clears the stack. StackEmpty (S): If the stack is empty, true is returned; otherwise, false is returned. GetTop (S, * e): If the stack exists and is not empty, use e to return the top element of S. Push (* S, e): If Stack S exists, insert the new element e to stack S, which is called Pop (* S, * e ): delete the top element of stack S and use e to return its value StackLength (S): return the number of elements of stack S. endADT
Ii. Stack Storage Structure1. Stack sequence storage structure and implementation (complexity is O (1 ))(1) stack Sequence Structure Definition# Define OK 1 # define ERROR 0 typedef int SElemType; // The SElemType type depends on the actual situation. It is assumed that it is inttypedef struct {SElemType data [MAXSIZE]; // stack storage space size MAXSIZE int top; // used for Stack top pointer} SqStack;(2) Stack-to-stack operation (insert an element from the top of the stack)Algorithm idea: a. Determine whether the stack is full; B. Add 1 to the top of the stack; c. Insert elements into implementation: insert element e into the new top element of the stack.

Status Push (SqStack * S, SElemType e) {if (S-> top = MAXSIZE-1) // stack top = MAXSIZE-1, that is, the last storage location of the array, note that the stack is full {return ERROR;} S-> top ++; // Add 1 S-> data [S-> top] = e; // Add the elements to the stack return OK ;}
(3) Out-of-stack operations (delete an element from the top of the stack)Algorithm idea:. determines whether the stack is empty. B. save the top element of the stack to the storage location pointed to by the pointer variable e; c. stack Top minus 1. implementation: If the stack is not empty, delete the stack top element of S, use e to return its value, and Return OK; otherwise, an ERROR is returned.
Status Pop (SqStack * S, SElemType * e) {if (S-> top =-1) // empty stack {return ERROR ;} * e = S-> data [S-> top]; S-> top --; return OK ;}
(4) Two-stack shared spaceThe sequential storage of stacks only allows the top of the stack to access elements. Therefore, a large number of elements need to be moved when a linear table is inserted or deleted. However, there is a defect that the size of the array storage space must be determined in advance. When the data types of the two stacks are the same, we can share the space opened up by the two stacks.
A. thought: place the bottom of stack 1, that is, the bottom of the shared space stack (subscript is 0 ); the bottom of another stack is the end of the shared stack (the subscript is the array length n-1), where top1 and top2 are the top pointers of stack 1 and stack 2. For a shared stack, if Stack 2 is an empty stack (top2 = n) and stack 1's top1 = n-1, the stack 1 is full; if Stack 1 is an empty stack (top =-1) and stack 2 is top2 = 0, it means stack 2 is full. That is, the full condition of the shared stack is: top1 + 1 = top2. B. shared stack space structure typedef struct {SElemType data [MAXSIZE]; // defines an array storage space. The size is MAXSIZE, that is, the stack size is int top1; // stack 1 top pointer int top2; // stack 2 top pointer} SqDoubleStack; C. idea of an algorithm for adding elements to the stack of a shared Stack:. first, judge whether the shared stack is full. B. the stackNumber parameter is used to determine which stack is inserted. If Stack 1 is inserted, the top of stack top1 is increased by 1, and Element e is added to the stack. If Stack 2 is inserted, the top of stack top2 is reduced by 1, add Element e to the stack. Implementation: insert element e into the new stack top element.
Status Push (SqDoubleStack * S, SElemType e, int stackNumber) {if (S-> top1 + 1 = S-> top2) // determine whether the shared stack is full (top1 + 1 = top2) {return ERROR;} if (stackNumber = 1) // stack 1 has elements into the stack, stack top top1 plus 1 S-> data [++ S-> top1] = e; else if (stackNumber = 2) s-> data [-- S-> top2] = e; // stack 2 has elements into the stack, and top of the stack top2 minus 1 return OK ;}
D. algorithm idea of stack element exit Operation:. judge which stack; B. if Stack 1 is used, the element is first assigned to the space position pointed to by the pointer variable e, and then top1 minus 1 on the top of the stack. If Stack 2 is used, first, assign the element to the space position pointed to by the pointer variable e, and then add 1 to the top of the stack top2. Implementation: If the stack is not empty, delete the stack top element of S, return the value with e and OK; otherwise, ERROR is returned.
Status Pop (SqDoubleStack * S, SElemType * e, int stackNumber) {if (stackNumber = 1) // if the deletion element belongs to stack 1 {if (S-> top1 =-1) // empty stack return ERROR; * e = S-> data [S-> top1 --]; // assign the element to e, and then subtract 1} else if (stackNumber = 2) from the top of the stack) // if the deletion element belongs to stack 1 {if (S-> top2 = MAXSIZE) // empty stack return ERROR; * e = S-> data [S-> top2 ++]; // assign the element to e and subtract 1 from the top of the stack.} return OK ;}
Note for sublimation. The stack sequence storage structure is implemented through arrays. The subscript 0 indicates the bottom of the stack, because the first element has the minimum variation of the bottom of the stack. When an element exists in the stack, the top of the stack is 0. If the stack is empty, top =-1. This is also a condition for determining whether the stack is empty. In addition, the storage stack length StackSize (MAXSIZE) and storage location must be different. 2. the two-stack shared space is only applicable to two stacks with the same element data type. In the stack, the top of the stack is modified before the elements are imported into the stack. In the stack, the top of the stack is modified after the elements are output.

2. Stack chain storage structure and implementation


(1) chain stack structure// Link node: contains the data field and pointer field typedef struct StackNode {SElemType data; // data field struct StackNode * next; // pointer field} StackNode, * LinkStackPtr; // chain stack structure typedef struct LinkStack {LinkStackPtr top; // chain stack header node int count; // number of nodes} LinkStack; (2) stack import operationsAlgorithm idea:. first, open up a space for the new node s, the size of the space is the size of the node structure; B. the element value e to be inserted is assigned to the data field of the new node s; c. assign the original Top element of the stack to the next d of the new node s. point the stack top pointer to the new node ee. add 1 to the trace stack element implementation: insert element e into the new stack top Element
Status Push (LinkStack * S, SElemType e) {LinkStackPtr s = (LinkStackPtr) malloc (sizeof (StackNode); // open up a new Node space, the size is the size of the structure StackNode s-> data = e; // assign the Element e to the data domain s-> next = s-> top of the new node S; // assign the current top element of the stack to the direct successor S of the new node> top = s; // assign the new node s to the top pointer S-> count ++; // Add 1 return OK to the number of Trace stack elements ;}
(3) The algorithm concept of stack Exit:. first, determine whether stack B is empty. assign the stack top pointer to the data field of the element to the ec. assign the element to which the stack top pointer originally points to pd. then point the stack top pointer to the next node to release the pe. number of Trace stack elements minus 1 Implementation: If the stack is not empty, delete the stack top element of S, use e to return its value, and Return OK 
Status Pop (LinkStack * S, SElemType * e) {LinkStackPtr p; if (StackEmty (* S) // return ERROR if the stack is empty; * e = S-> top-> data; // assign the top element data of the stack to the pointer variable e pointing to the space p = S-> top; // assign the node (the top element of the stack) pointed to by the top pointer of the stack to p S-> top = S-> top-> next; // move the top pointer of the stack to one place, that is, the top pointer of the stack points to the next node free (p); // release the node p S-> count; // The number of Trace stack elements minus 1 return OK ;}
3. performance analysis (1) time complexity: The Stack's sequential structure and chain structure are both O (1); (2) space complexity the stack's sequential structure is only a data domain, with little space complexity, however, a fixed length needs to be determined in advance, which may cause a waste of memory space. Each element of the stack's chained storage structure has a data domain and pointer domain, and the space complexity is slightly complicated, at the same time, some memory overhead is added, but the stack length is not limited. (3) If element changes are unpredictable, sometimes small, and sometimes very large during stack usage, it is best to use the chain stack; otherwise, if its changes are within a controllable range, we recommend that you use an ordered stack with higher performance.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.