Data structure Base (4) C language implementation stack--chained storage (dynamic stack)

Source: Internet
Author: User

When writing a program, we often say that basic type variables exist in stack memory, and that variables (objects, arrays) of reference types have heap memory. Now let's look at how the stack is implemented with this data structure.

definition

A storage structure that can realize "advanced and Out"

Stack is similar to putting clothes on the box, first put the last stack of categories:

Static stack: Implemented as an array, delete when the largest subscript, inserted from the maximum subscript +1 insert

Dynamic stacks: Implemented as a linked list, delete and insert are all from the head algorithm: Out Stack (POP), into the stack (PUSH) application:

1. Function call: Call another G function in function f, call k function in G function

Execute to call the G function position, first the G function after the execution of the next sentence of the address and variable pressure stack, execute the G function, execute to call the K function position, then the K function after the next sentence of the stack, and then perform K function, take out the top of the stack element, assigned to the CPU, the G function in the call K function after the next sentence.

2. Interruption

3. Expression evaluation: 3*2+5/6-4 Two stack implementation, one drop operator, one put data

4. Memory allocation: The function parameter is pressed into the stack

5. Buffer Handling

6. Take the Maze


C language implementation:

The following is the C language implementation of the dynamic stack code, in order to facilitate the bottom of the stack to establish a head node, do not save valid data. First look at the picture:





#include <stdio.h> #include <malloc.h> #include <stdbool.h>//stack has a header node that does not hold valid data, and Pbotom always points to the head nodes.
/** * * Defines the structure of the node/typedef struct node{int data;//data domain struct node * pnext;//pointer domain} node,*pnext;
/** * * Defines the structure of the stack/typedef struct stack {pnext top;
Pnext Bottom;
}stack,* Pstack;
   /** * * Initialize stack */void init (Pstack pstack) {//Create a header node without any finite elements, as the bottom pstack->bottom=malloc (sizeof (Node));
   pstack->top=pstack->bottom;
pstack->top->pnext=null;
        /** * Traversal stack **/void traverse (Pstack Ps) {if (ps->bottom==ps->top) {printf ("stack is empty \ \");
    return;
    } Pnext pt=ps->top; while (Pt!=ps->bottom)//Can not change the PT to ps->top this modified the linked list.
    Embarrassed..
       {printf ("%d", pt->data);
    Pt=pt->pnext;
    printf ("\ n");

return;
   /** * * Enter stack/void push (Pstack pstack,int val) {pnext pnew=malloc (sizeof (node));//Generate a new node pnew->data=val;
   pnew->pnext=pstack->top;
pstack->top=pnew; /** * * out stack/void pop (PSTACK PS) {if (Ps->top==ps->bottom) {printf ("Stack is empty, unable to complete the stack operation \ n");
  Return
  Pnext temp=ps->top;//introduces auxiliary variables to free memory ps->top=ps->top->pnext;
  Free (temp);
Temp=null;
        /** * * Empty stack */void Clear (Pstack PS) {while (Ps->top!=ps->bottom) {Pnext temp=ps->top;
        ps->top=ps->top->pnext;
    Free (temp); int main () {stack stack; init (&stack); push (&stack,6); push (&stack,7); push (&stack,8); traverse
 ; stack);
Pop (&stack);
Traverse (&stack);
Clear (&stack);
Traverse (&stack);
Push (&stack,7);
Traverse (&stack);
return 0;
 }




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.