基本栈c语言的实现

 1 /*初始栈顶为-1,栈满时栈顶指针为MAX_STACK_SIZE-1
 2 */ 
 3 
 4 #include<stdio.h> 
 5 #include<stdlib.h>
 6  
 7 #define MAX_STACK_SIZE 100        /*最大栈元素数*/
 8 typedef struct {
 9     int key  ;                                                        
10 } element;
11 
12 element stack[MAX_STACK_SIZE];
13 
14 bool IsFull(int *top );            
15 void  Add(int *top,element item);
16 bool IsEmpty(int *top );
17 element Delete(int *top);
18 
19 
20 int main()
21 {
22     int i;
23     int top=-1;
24     element j;
25     j.key=0;
26     
27     if(IsEmpty(&top))
28     { 
29         for( i = 0; i<102; i++){
30             if(!IsFull(&top))    
31                 Add(&top,j);    
32             else{ 
33                 printf("栈满\n");
34                 break;
35                 } 
36                 j.key++;
37             
38         } 
39     } 
40     while(!IsEmpty(&top))
41         printf("%d \n",Delete(&top).key);
42     if(IsEmpty(&top))    
43         printf("\n栈空,已输出全部元素!\n");
44     return 0;
45 }
46 
47 
48 bool IsFull(int * top )//1为满0为不满 
49 {
50     if(*top >=MAX_STACK_SIZE-1){
51         return 1;
52     }
53     else{
54         return 0;
55     }
56 }
57 
58 bool IsEmpty(int *top)//1为空  0为不空 
59 {
60     if(*top == -1){
61         return 1;
62     }
63     else{
64         return 0;
65     }
66 } 
67 void  Add(int *top,element item)
68 {
69     if(*top >=MAX_STACK_SIZE-1){
70         printf("栈满"); 
71         return ;
72     }
73     else
74         stack[++(*top)]=item;
75 }
76 element Delete(int *top)
77 {
78     if(*top == -1){
79         printf("栈空");
80     }
81     return stack[(*top)--]; 
82 }

 

转载于:https://www.cnblogs.com/hysz/p/7147104.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值