Stack is an advanced data structure, the common function calls in the computer use this structure, its commonly used operations are out of the stack, into the stack, such as, data always from the top of the stack, from the top of the stack:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/89/78/wKioL1gUkJrDy5CyAAAyhqrRbic994.png "title=" capture. PNG "alt=" Wkiol1gukjrdy5cyaaayhqrrbic994.png "/>
Next look at a simple program will press "ABCDEF" into the stack, and print out the stack order:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define stack_ size 16#define name_max_size 32#define err -1#define succ 0typedef struct stack {char * array;/* stack start address */int stack_size;/* stack size */int top;/* top of stack */char (* Pop) (Struct stack *sta);/* */int (*push) (Struct stack *sta, char data); /* into the stack */} stack_t;static int is_empty (stack_t *sta) {return (sta->top == -1);} Static int is_full (Stack_t *sta) {return (sta->top == sta->stack_size-1);} /* the stack top element out of the stack and returns */char pop_stack (Stack_t *sta) {char ch;if (Is_empty (STA)) { printf ("the stack is empty \n"); return err;} Ch = sta->arrAy[sta->top];--sta->top;return ch;} /* Insert Element */int push_stack (stack_t *sta, char data) {if (Is_full (STA)) at the top of the stack {printf ("the stack is full \n"); return err;} ++STA->TOP;STA->ARRAY[STA->TOP]&NBSP;=&NBSP;DATA;RETURN&NBSP;SUCC;} Void init_stack (Stack_t **sta) {*sta = (stack_t *) malloc (sizeof (stack_t));if (* STA) == null) {printf ("no mem \n"); return ;} (*sta)->top = -1; (*sta)->stack_size = stack_size; (*sta)->pop = pop_stack; (*sta)->push = push_stack; (*sta)->array = (char *) malloc (Stack_size);if ((*sta)->array == null) {printf ("no mem \n"); return ;}} Int main (int argc, char *argv[]) {Int size, ret, i;stack_t *sta_addr;char data[] = "abcdef"; Init_stack (&STA_ADDR);size = sizeof (data) / sizeof (Data[0]);for (i = 0; i < size; i++) {sta_addr->push (Sta_addr, data[i]);} while (1) { ret = sta_addr->pop (sta_addr); if (Ret != ERR) {printf ("%c,", ret); } else {break; }}return 0;}
This article is from the "12128867" blog, please be sure to keep this source http://12138867.blog.51cto.com/12128867/1867184
Stack C language implementation of data structure