| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 435 人关注过本帖
标题:栈里函数的怎么调用
只看楼主 加入收藏
小小小丹丹
Rank: 2
等 级:论坛游民
帖 子:26
专家分:13
注 册:2010-4-6
结帖率:62.5%
收藏
已结贴  问题点数:20 回复次数:1 
栈里函数的怎么调用
#include"stdio.h"
#include"stdlib.h"
#include"malloc.h"

#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;

typedef struct
{
    int *base;
    int *top;
    int stacksize;
}SqStack;

typedef  int ElemType;

int InitStack(SqStack **S) //为栈S分配存储空间,并置S为空栈
{
    int size = STACK_INIT_SIZE;
    (*S) = (SqStack *)malloc(sizeof(SqStack));
    (*S)->base=(int *)malloc(size*sizeof(ElemType));
    if(!(*S)->base) return 0;
    (*S)->top=(*S)->base; //置栈S为空栈
    (*S)->stacksize=STACK_INIT_SIZE;
    return 1;
}
nt  GetTop(SqStack *S,int *e)  //若栈不空,则用e返回S的栈顶元素
{
    if(S->top==S->base) return 0;
    *e=*(S->top-1);
    return 1;
}
                                                                                                                  
int  Push(SqStack *S, int e) /*进栈函数,将e插入栈S中,并使之成为栈顶元素*/                                         
{                                                                                                                 
    if(S->top-S->base>=S->stacksize) /*栈满,追加存储空间*/                                                      
    {                                                                                                            
        int stackinvrement = STACKINCREMENT;                                                                     
        S->base=(ElemType *) realloc(S->base,                                                                     
            (S->stacksize+stackinvrement)*sizeof(ElemType));                                                      
        if(!S->base) return 0; /*存储分配失败*/                                                                  
        S->stacksize+=STACKINCREMENT;                                                                             
    }                                                                                                            
    *S->top++=e;                                                                                                  
    return 1;                                                                                                     
}                                                                                                                 
                                                                                                                  
int  Pop(SqStack *S,int *e)/*出栈函数,若栈S不空,则删除S的栈顶元素,用e返回其值*/                                   
{                                                                                                                 
    if(S->top==S->base) return 0;                                                                                 
    *e=*--S->top;                                                                                                
    return 1;                                                                                                     
}  
void OutputStack(SqStack *S)                                                                                      
{                                                                                                                 
    int *q,i=0;                                                                                                   
    q=S->top-1;                                                                                                   
    for(i=0;i<S->top-S->base;i++)                                                                                 
    {                                                                                                            
        printf("%d ",*q);q--;                                                                                    
    }                                                                                                            
}                                                                                                                 
                                                                                                                  
int main()                                                                                                        
{                                                                                                                 
    int a,b,c,d,e;                                                                                                
    SqStack *h;                                                                                                   
    InitStack(&h);                                                                                                
    printf("你要输入几个元素:\n");                                                                                
    scanf("%d",&a);                                                                                               
    printf("请输入你要的元素:\n");                                                                                
    for(b=0;b<a;b++)                                                                                             
    {                                                                                                            
        scanf("%d",&c);                                                                                          
        Push(h,c);                                                                                                
    }                                                                                                            
    OutputStack(h);                                                                                               
    GetTop(h,&e);                                                                                                  
    Pop(h,&e);                                                                                                   
    printf("返回的栈顶元素:%d\n",e);                                                                              
}
大侠们还有push,pop,Output,GetTop等函数怎么调用啊帮帮忙啊我调用函数好多弄不明白呢
搜索更多相关主题的帖子: 函数 
2010-05-12 16:35
南国利剑
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:29
帖 子:1165
专家分:3536
注 册:2010-4-12
收藏
得分:20 
建议参考一下msdn

南国利剑
2010-05-13 13:02
快速回复:栈里函数的怎么调用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.060436 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved