4, Stack

Source: Internet
Author: User

1, the definition of the stack

A stack is a linear table of last-in-first-out (OUT,LIFO), which requires that only deletions and insertions be done at the end of the table.

2, the characteristics of the stack:

(1) The elements of the stack must be "LIFO".

(2) The operation of the stack can only be performed at the end of this linear table.

(3) Note: For the stack, the end of the table is called the stack Top (top), the corresponding table header is called the bottom of the stack (bottom).

3. Insert and delete operations for stacks

The insert operation of the Stack (Push), called the Stack, is also called the stack, into the stack. Like a bullet in a magazine.

The delete operation (POP) of the stack, called the stack, is also called the stack. Like a bullet in a magazine clip.

4, the first stack does not contain any data, called the empty stack, when the top of the stack is the bottom of the stack. Then the data is entered from the top of the stack, the stack top stack is separated, and the current capacity of the stack becomes larger. The data is ejected from the top of the stack when it is out of the stack, the stack top is down, and the current capacity of the stack becomes smaller.

5. Sequential storage structure of stacks

typedef struct

{

Elemtype *base;//Stack Bottom

Elemtype *top;//Stack Top

int stacksize;//Stack size

}sqstack;

6. Create a stack

#define Stack_init_size Initstack (Sqstack *s)

{

S->base = (Elemtype *) malloc (stack_init_size * sizeof (elemtype));

if (!s->base)

Exit (0);

S->top = s->base; At first, the top of the stack is the bottom

S->stacksize = stack_init_size;

}

7, into the stack operation

The stack operation is called the stack operation, which is to store the data in the stack.

1 #defineStackincrement 10//Space2 3Push (Sqstack *s, Elemtype e)//*s Pointer to the top of the stack, E is the value of the top node of the stack .4 {5     if(s->top-s-Base>= s->stackSize)6     {7S->Base= (elemtype*)realloc(s->Base, (s->stacksize+stackincrent) *sizeof(Elemtype));//re-adjust memory space8 9         if(!s->Base)TenExit0); One  As->top=s-Base+s->stacksize;//set top of Stack -S->stacksize=s->stacksize + stackincrement;//set the maximum capacity of the stack -     } the  -* (S->top) =e;//Stack Top Assignment -s->top++;//Stack Top Shift -}
View Code

The stack operation is done at the top of the stack, and each time a data is pressed into the stack, the top pointer will be +1, knowing that the stack is full.

1 Pop (sqstack *s, Elemtype *e)2{3if(s->top = = s->  Base )  //  stack already empty is also 4return; 5 *e = *--(s->top); 6 }
View Code

8. Empty a stack

The so-called empty stack is to invalidate all the elements in the stack, but the physical space of the stack itself is not changed (not destroyed).

As long as the content of the S->top is assigned to S->base, so s->base equals S->top, it indicates that the stack is empty.

1 clearstack (Sqstack *s)2{3 s->top = s->base; 4 }
View Code

9. Destroy a stack

Destroying a stack is different from emptying a stack, destroying a stack to free up the physical memory space occupied by that stack.

1Destroystack (Sqstack *s)2 {3 intI, Len;4Len = s->stackSize;5  for(i=0; i < Len; i++ ){6  Free(s->Base );7S->Base++;8 }9S->Base= S->top =NULL;TenS->stacksize =0; One}
View Code

10, calculate the current capacity of the stack

(1) The current capacity of the calculation stack is the number of elements in the calculation stack, so just return s.top-s.base.

(2) The maximum capacity of the stack is that the stack occupies the size of the memory space, the value is s.stacksize, it and the current capacity of the stack is not a concept oh.

1 int Stacklen (sqstack s) 2 {3return(s.top–s.base);  // beginners need to focus on explaining 4 }
View Code

4, Stack

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.