码迷,mamicode.com
首页 > 其他好文 > 详细

简单数据结构之栈模拟

时间:2014-05-01 12:39:59      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   javascript   color   get   int   2014   文件   

mamicode.com,码迷
  1 /************************************************************************************** 
  2 * Function     : 模拟栈
  3 * Create Date  : 2014/04/23
  4 * Author       : NTSK13 
  5 * Email        : beijiwei@qq.com 
  6 * Copyright    : 欢迎大家和我一起交流学习,转载请保持源文件的完整性。 
  7 *                任何单位和个人不经本人允许不得用于商业用途 
  8 * Version      : V0.1                    
  9 ***************************************************************************************
 10 题目:简单数据结构之栈模拟
 11      
 12 **************************************************************************************/  
 13 #include<stdio.h>  
 14             
 18 #define M 10 //栈最大高度
 19 
 20 typedef struct stack
 21 {
 22     int data[M];
 23     int high;//栈当前高度
 24 }Stack;
 25 
 26 int sample[M];
 27 int result[M];
 28 
 29 void init_stack(Stack *a);//栈初始化
 30 int  push_stack(Stack *a,int value);//进栈
 31 int  pop_stack(Stack *a);//出栈
 32 int  get_top_element(Stack *a);//获取栈顶的元素
 33 
 34 // The first method:
 35 int main()  
 36 {  
 37     int i=0;
 38     Stack sq;
 39     init_stack(&sq);
 40 
 41     freopen("input.txt","r",stdin);
 42 
 43     for(i=0;i<M;i++) 
 44         scanf("%d",&sample[i]);  // get input data
 45     pop_stack(&sq);
 46     get_top_element(&sq);
 47            
 48     for(i=0;i<M;i++)
 49         push_stack(&sq,sample[i]);
 50     push_stack(&sq,100);
 51 
 52     for(i=0;i<M;i++)
 53         result[i]=pop_stack(&sq);
 54 
 55     for(i=0;i<M;i++)
 56     {
 57         printf("%d\t",result[i]);
 58         fflush(stdout);//修复Eclipse printf()不能显示的小bug
 59     }
 60 
 61     printf("\n");
 62     fflush(stdout);
 63 
 64     return (0);
 65 }  
 66 
 67 void init_stack(Stack *a)//栈初始化
 68 {
 69     (*a).high=0;
 70 }
 71 
 72 int  push_stack(Stack *a,int value)//进栈
 73 {
 74     if((*a).high==M)
 75     {
 76         printf("Stack is full, can not push !!! \n");
 77         fflush(stdout);
 78 
 79         return (-1);
 80     }
 81     else
 82     {
 83         if( (*a).high==0 )
 84         {
 85             (*a).data[0]=value;
 86             (*a).high++;
 87             return (1);
 88         }
 89         else
 90         {
 91             (*a).data[ (*a).high ]=value;
 92             (*a).high++;
 93             return (1);
 94         }
 95     }
 96 }
 97 
 98 int  pop_stack(Stack *a)//出栈
 99 {
100     int tmp=0;
101     if((*a).high==0)
102     {
103         printf("Stack is empty, can not pop !!! \n");
104         fflush(stdout);
105         return (-1);
106     }
107     else
108     {
109         tmp=(*a).data[ (*a).high -1];
110         (*a).high--;
111 
112         return (tmp);
113     }
114 
115 }
116 int get_top_element(Stack *a)//获取栈顶的元素
117 {
118 
119     int tmp=0;
120     if((*a).high==0)
121     {
122         printf("Stack is empty, can not get top element !!! \n");
123         fflush(stdout);
124         return (-1);
125     }
126     else
127     {
128         tmp=(*a).data[ (*a).high -1];
129         return (tmp);
130     }
131 
132 }
133     
13
mamicode.com,码迷

 

简单数据结构之栈模拟,码迷,mamicode.com

简单数据结构之栈模拟

标签:style   blog   class   code   java   javascript   color   get   int   2014   文件   

原文地址:http://www.cnblogs.com/ntsk13/p/3701420.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!