Stack C language implementation of data structure

Source: Internet
Author: User

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-&GT;TOP;STA-&GT;ARRAY[STA-&GT;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 (&AMP;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

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.