Data structure 11: Stack concept and application and C language implementation

Source: Internet
Author: User

Stack, a special storage structure for linear tables. The difference with a learned linear table is that the stack can only insert and delete data from the fixed end of the table, and the other end is sealed.

Figure 1 Stack structure because only one side of the stack to access data, called the end of the opening is "stack top", the end of the seal is "bottom" (similar to the bucket of water, from where in the end to come out). Stack of "advanced out" principle uses the stack to store data elements, the data elements of the "save" and "fetch" have strict rules: data in a certain order stored in the stack, when the need to adjust the stack of a data element, the data element needs to be in the stack after the first-out stack, the data element can be extracted from the stack.

1, the stack contains 4 data elements, the order of the stack is a advanced stack, then B into, then C in, the last D into the stack, when the need to tune A, first d out of the stack, then C out of the stack, then B out of the stack, and finally a can be called out of the stack.

Just like a garage with only one door (only one car at a time), each car is like a data element, only the car closest to the door opens first, inside the car can come out, the most inside car is the first to open, destined to finally come out. Stack operation data element method stack operation data element there are only two kinds of actions:
    1. Data elements are stored in the stack's data structure, called "into the stack", also called "stack".
    2. Data elements need to be extracted from the stack structure for some reason, called "Out-of-stack", also called "stack".
Since the stack is also a linear table, there are two representations of the linear table: the sequential stack and the chain stack (the "Chain Stack").
The difference between the two is whether the stored data elements are physically adjacent to each other. The sequential stack storage element is pre-applied to successive storage units, and the link stack needs to be applied, and the data elements are not immediately next to each other. "Overflow" and "underflow" of stacks

When the stack storage structure is picking up data elements in the stack, avoid "overflow" and "underflow" situations:

Overflow: If the stack is already full of data elements, the stack store will be faulted if it continues to deposit data into the stack.

"Underflow": In the case of an empty stack, an error occurs if the stack continues to fetch data.

Stack of "Overflow" and "underflow", can be summed up as: The stack will be "overflow", stack empty and then will be "underflow".

For the two representations of the stack, there is a possibility that both of the sequential stacks can occur, and the chain stack is not "overflow" due to the storage structure of "ready to apply space at any time".

The implementation of the sequential stack sequence is based on an array.

Set a variable in the sequence stack at any time to point to the top of the stack (generally named top), when the value of top is 1, there is no data in the array, that is, there is no data element in the stack, as long as the data elements into the stack, top plus 1, the data elements out of the stack, top minus 1.

For example, the storage structure using the sequential stack would stack (' a ', ' B ', ' C ', ' d ') four elements and output the top elements of the stack one after the other.

Implementation code:
#include <stdio.h>
//element Elem into the stackintPushChar*a,intTopCharelem)
{a[++top] =Elem; returntop;}
//data element out of stackintPopChar*a,inttop)
{ if(Top = =-1)
{printf ("Empty Stack"); return-1; } printf ("stack elements:%c\n", A[top]); Top--; returntop;}
intMain ()
{ Chara[ -]; inttop =-1; Top=push (A, top,'a'); Top=push (A, top,'b'); Top=push (A, top,'C'); Top=push (A, top,'D'); Top=Pop (A, top); Top=Pop (A, top); Top=Pop (A, top); Top=Pop (A, top); Top=Pop (A, top); return 0;}
Output Result:
Stack element: D-Stack element: C-Stack element: B-Stack element: a empty stack
Link stacks

Chain Stack, which is realized by the chain storage structure of linear table.

The chain stack generally does not need to create the head node, the head node will increase the complexity of the program, just need to create a head pointer on it.

When using a linked list to represent a stack, using one end of the chain header node as the top of the stack, the benefit is that when the data element is stacked or stacked, the direct use of the head pointer can be done without additional pointers being added.

For example, using the chain stack implementation will (' A ', ' B ', ' C ', ' d ') four data elements are stacked, then reload:

#include <stdio.h>#include<stdlib.h>
typedefstructLinestack
{ Chardata; structLinestack *Next;} Linestack;
Linestack* PUSH (Linestack * stack,Chara)
{Linestack*line = (linestack*)malloc(sizeof(Linestack)); Line->data =A; Line->next =Stack; Stack=Line ; returnStack;}
Linestack*pop (Linestack *stack)
{ if(Stack)
{Linestack*p =Stack; Stack= stack->Next; printf ("stack element:%c", p->data); if(Stack)
{printf ("stack top element:%c\n", stack->data); }
Else
{printf ("stack is empty \ n"); } Free(P); }
Else
{printf ("There are no elements in the stack"); returnStack; }
returnStack;}
intMain ()
{Linestack*stack =NULL; Stack= Push (Stack,'a'); Stack= Push (Stack,'b'); Stack= Push (Stack,'C'); Stack= Push (Stack,'D'); Stack=pop (stack); Stack=pop (stack); Stack=pop (stack); Stack=pop (stack); Stack=pop (stack);
return 0;}
Output: Stack elements: D stack top elements: C stack elements: C Stacks top elements: B-Stack elements: B Stacks top elements: A stack element: a stacks no elements in the empty stack
Summarize

Real life in the use of mobile phones, screen pages of the jump using the stack structure (jump page, the previous page will be stored in the stack, the fallback operation will go back to the previous page, which is the effect of the stack page out). In addition to seeking n! , it can be implemented by the recursive return of the function, the bottom of the process is used in the stack structure.

In addition, the numbering conversion and the parentheses matching problem can also be solved with a stack (the following section describes).

Data structure 11: Stack concept and application and C language implementation

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.