# 07. Stack (a) stack storage structure and operation

Source: Internet
Author: User

First, Stack 1. Stack: is a linear table that restricts insert and delete operations only at the end of a footer. Where one end of the allowed insertions and deletions is called the top of the stack , and the other end is called the bottom of the stack (bottom), the stack without any data elements is called an empty stack. The stack is also known as the LIFO (last in first out) linear table, abbreviated to the LIFO structure. the insert operation of the stack is in the stack, and the delete operation of the stack is the stack. 2. Abstract data types for stacksADT Stack (stack)DataThe same linear table. Elements have the same type, and adjacent elements have precursors and successors. OperationInitstack (*s): Initializes the operation, creating an empty stack of S. Destorystack (*s): If the stack exists, destroy it. Clearstack (*s): empties the stack. Stackempty (S): Returns True if the stack is empty, otherwise false. GetTop (s,*e): If the stack exists and is not empty, use E to return the stack top element of S. Push (*s,e): If stack s exists, insert a new element e into the stack s and called the top element of the stackPop (*s,*e): delete stack S top element and return its value with EStacklength (s): Returns the number of elements of the stack SEndadt
second, the storage structure of the stack 1. Stack of sequential storage structure and implementation (the complexity of all O (1)) (1) Stack order structure definition#define OK 1#define ERROR 0typedef int SELEMTYPE; The Selemtype type is based on the actual situation and is assumed to be an inttypedef struct{Selemtype Data[maxsize]; Stack storage space size maxsizeint top; For stack top pointers}sqstack; (2) in-stack operation (inserting an element from the top of the stack) Algorithm Ideas :A. Determine whether the stack is full;B. stack top plus 1;c. Inserting an element into theimplementation: Insert element E as the new stack top element
`Status Push (Sqstack *s,selemtype e) {    if (s->top==maxsize-1)    //Stack top =maxsize-1, which is the last storage location of the array, stating that the stack is full    {        return ERROR;    }    s->top++;    Stack top plus 1    s->data[s->top]=e;    Put the element in the stack    return OK;}`
(3) stack operation (delete an element from the top of the stack)Algorithm Ideas:A. Determine if 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 top element of S, return its value with E, and return OK;
`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 stacks of shared space    the order of the stack stores only the top of the stack in and out of the elements, so there is no linear table insertion and deletion need to move a large number of elements, but there is a flaw is the need to determine the size of the array storage space beforehand. When the two stack data types are the same, we can use the space shared by the two stacks.
A. Thought: The stack of stack 1 is the bottom of the stack (subscript 0), and the bottom of the other stack is the end of the shared stack (the subscript is an array of length n-1), where Top1 and Top2 are the stack top pointers of stack 1 and Stack 2. For the shared stack, if the stack 2 is empty stack (top2=n), stack 1 top1=n-1, the stack 1 is full, if the stack 1 is empty stack (top=-1), Stack 2 top2=0, the stack is 2 full. That is, shared stack full condition: top1+1==top2. B. Shared stack Structuretypedef struct{Selemtype Data[maxsize]; Defines an array storage space with a size of maxsize that is the stack sizeint top1; Stack 1 stack top pointerint top2; Stack 2 stack top pointer}sqdoublestack;c. Sharing stack elements into the stack operationAlgorithm Ideas:A. First determine whether the shared stack is full;B. Determine which stack to insert by Stacknumber parameterif the stack is 1, then the stack top TOP1 1, then the element e into the stack, if the stack 2, the top top2 minus 1, the element e into the stack. implementation: Insert element E as the new stack top element
`Status Push (sqdoublestack *s,selemtype e,int stacknumber) {    if (S->TOP1+1==S->TOP2)    //Determine if the shared stack is full (top1+1 =TOP2)    {        return ERROR;    }    if (stacknumber==1)        //stack 1 has elements into the 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, stack top top2 minus 1    return OK;}`
D. Sharing stack elements out-of-stack operationsalgorithm idea:A. Determine which stack;B. For stack 1, the element is first assigned to the space position where the pointer variable e is pointing, and the top top1 of the stack is reduced by 1;for Stack 2, the element is first assigned to the space position of the pointer variable e, and then the top top2 of the stack is increased by 1; implementation: If the stack is not empty, then delete the top element of S, return its value with E, and return OK;
`Status Pop (sqdoublestack *s,selemtype *e,int stacknumber) {    if (stacknumber==1)    //delete element belongs to stack 1    {        if (S- >TOP1==-1)     //empty stack                  return ERROR;        *e=s->data[s->top1--];    Assign the element to E, the top of the stack minus 1    }    else if (stacknumber==2)//If the delete element belongs to stack 1    {           if (s->top2==maxsize)     //empty stack                  return ERROR;        *e=s->data[s->top2++];    Assign the element to E, the top of the stack minus 1      }    return OK;`
sublimation Note 1:1. The sequential storage structure of the stack is implemented by an array, subscript 0 is the bottom of the stack, because the first element has the lowest change in the bottom of the stack. When there is an element in the stack, the top of the stack is 0, and if the empty stack is Top=-1, this is also the condition to determine whether the stack is an empty stack. Also, you want to distinguish the length of the storage stack stacksize (MAXSIZE) and the storage location. 2. Two stack space is only applicable to two stacks with the same element data type, and the stack is the first to modify the stack top elements into the stack, the stack is the first element out of the stack and then modify the top of the stack.

2. Stack's chain storage structure and implementation

(1) structure of the chain stack//Chain node: Contains data fields and pointer fieldstypedef struct STACKNODE{Selemtype data; Data fieldsstruct Stacknode *next; Pointer field}stacknode,*linkstackptr;//Chain stack structuretypedef struct LINKSTACK{linkstackptr top; Link Stack head nodeint count; Number of nodes.}linkstack; (2) The stack operation of the chain stackalgorithm idea:A. First to open a space for the new node s, the size of the space is the node structure size;B. Assign value E of the element to be inserted to the data field of the new node S;C. Assigning the original stack top element to the successor of the new node SD. Point the top of the stack pointer to the new node EE. Chain stack elements increased by 1implementation: Insert element E as the new stack top element
`Status Push (Linkstack *s,selemtype e) {    linkstackptr s= (linkstackptr) malloc (sizeof (Stacknode));    Opens up a new node space, the size of which is the structure stacknode size    s->data=e;        Assigns the element e to the data field of the new node S    s->next=s->top;//the current stack top element to the immediate successor of the new node    s->top=s;    Assigns a new node S value to the top of the stack pointer    s->count++;//the number of chain elements plus 1    return OK;}`
(3) The stack operation of the chain stackalgorithm idea:A. First determine if the link stack is an empty stackB. Assign the top pointer to the element data field data to EC. Assign the element that originally pointed to the top of the stack to Pd. Then point the top pointer to the next node and release PE. Number of link stack elements minus 1implementation: If the stack is not empty, then delete the top element of S, return its value with E, and return OK
`Status Pop (linkstack *s,selemtype *e) {    linkstackptr p;    if (Stackemty (*s))    //Jorian stack        return ERROR;    *e=s->top->data;    Assigns the stack top element data to the pointer variable e to the space    p=s->top;                Assigns the node to the top of the stack (the top element of the stack) to P s->top=s->top->next;.    Make the top pointer move down one bit, that is, the top pointer of the stack points to the next node free    (p);    Release node P    s->count;    The number of chain elements minus 1    return OK;}`
3. Performance Analysis(1) Time complexity: The sequential structure of the stack and the chain structure are O (1);(2) Complexity of spacethe sequential structure of the stack is only data domain, space complexity is small, but need to determine a fixed length beforehand, there may be a memory space waste problem;the chained storage structure of a stack each element has a data field and a pointer field, a slightly more complex spatial complexity, and a bit of memory overhead, but no limit to the length of the stack. (3) If the use of the stack element changes unpredictable, sometimes very small, sometimes very large, it is best to use a chain stack, conversely, if its change in the controllable range, it is recommended that the use of sequential stack performance will be higher.

07. Stack (a) stack storage structure and operation

Related Keywords:

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.