Data Structure Basics (4) C-language implementation stack--chained storage (dynamic stack)

Source: Internet
Author: User

When writing a program, we often say that the underlying type variable exists in the stack memory, and that the variable (object, array) of the reference type has heap memory. Now let's see how this data structure of the stack is implemented.

Defined

A kind of storage structure that can realize "advanced out"

The stack is similar to putting clothes on the box, first put the last take

Classification of Stacks:

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

Dynamic stacks: Implemented in a linked list, deleted and inserted from the head

Algorithm: Out of Stack (POP), into the stack (push) application:

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

Execute to call the G function position, first the G function executed after the address of the next sentence and the variable pressure stack, execute g function, execute to call K function position, then the K function executes the next sentence stack, and then execute the K function, after executing the top element of the stack, assigned to the CPU, execute the G function called K function after the next sentence

2. Interrupts

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

4. Memory allocation: Press the function parameter into the stack

5. Buffer Handling

6. Walk the Maze


C Language implementation:

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





#include <stdio.h> #include <malloc.h> #include the <stdbool.h>//stack has a head node that does not hold valid data, and Pbotom always points to the head nodes. /**** defines the structure of a node */typedef struct node{int data;//data domain struct node * pnext;//pointer field} node,*pnext;/**** define the structure of the stack */typedef struc T Stack {Pnext top; Pnext Bottom;}    stack,* pstack;/**** Initialization Stack */void init (pstack pstack) {//Create a head node that does not have any finite elements, as a stack-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 \ n");    return;    } Pnext pt=ps->top; while (Pt!=ps->bottom)//Cannot change PT to Ps->top so the list is modified.    Embarrassed..       {printf ("%d", pt->data);    Pt=pt->pnext;    } printf ("\ n"); return;}   /**** into the 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, cannotComplete the stack operation \ n ");  Return  Pnext temp=ps->top;//introduces auxiliary variables to free up 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);p ush (&stack,6);p ush (&stack,7);p ush (&stack,8); Traverse ( &stack); Pop (&stack); traverse (&stack); clear (&stack); Traverse (&stack);p ush (&stack,7); Traverse (& stack); return 0;}




Data Structure Basics (4) C-language implementation stack--chained storage (dynamic 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.