| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1112 人关注过本帖
标题:栈的基本操作啊
只看楼主 加入收藏
小小小丹丹
Rank: 2
等 级:论坛游民
帖 子:26
专家分:13
注 册:2010-4-6
结帖率:62.5%
收藏
已结贴  问题点数:20 回复次数:6 
栈的基本操作啊
#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->base=(int *)malloc(size*sizeof(ElemType));

    if(!S->base) return 0;

    S->top=S->base; //置栈S为空栈

    S->stacksize=STACK_INIT_SIZE;

    return 1;

}

int  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--;}

}

void 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);
我的程序有什么错呢就是输出不了自己想要的结果啊请各位大侠帮忙啊
搜索更多相关主题的帖子: 空间 
2010-04-18 19:37
雨夜星光
Rank: 4
等 级:业余侠客
帖 子:66
专家分:230
注 册:2009-11-25
收藏
得分:0 
崩溃中……
你的程序我怎么感觉是逻辑上的错误呢?

C/C++交流群:65802335
2010-04-18 20:24
小小小丹丹
Rank: 2
等 级:论坛游民
帖 子:26
专家分:13
注 册:2010-4-6
收藏
得分:0 
什么逻辑错误啊具体指出一下啊我不知道啊就运行部出结果呢
2010-04-18 22:42
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
收藏
得分:20 
#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);                                                                              
}                                                   
算法没有仔细看,只是把一些内存上面的一些问题改了一下,楼主还要看一下函数形参与实参这方面的资料。。。希望有所帮助。。。

忘记说了,申请的空间记得释放,养成好的习惯,自己在末尾添上吧~~


[ 本帖最后由 ldg628 于 2010-4-19 09:18 编辑 ]
2010-04-19 09:13
小小小丹丹
Rank: 2
等 级:论坛游民
帖 子:26
专家分:13
注 册:2010-4-6
收藏
得分:0 
恩恩谢谢你啊有qq吗想跟你交流一下我qq1114702148我基础很差的呢
2010-04-19 13:27
小小小丹丹
Rank: 2
等 级:论坛游民
帖 子:26
专家分:13
注 册:2010-4-6
收藏
得分:0 
就是上面说的那里用&哪里用*我还是不怎么清楚啊能教教我么
2010-04-19 13:37
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
收藏
得分:0 
这个不用死记,它们都不是绝对的,只要知道&a是取a的地址,*a是取a的值所指向的地址的内容就行了,怎么方便就怎么用~

形参和实参的关系要看看,要建立一个空间的概念~
一步一步地来,基础一定要打牢~

2010-04-19 14:23
快速回复:栈的基本操作啊
数据加载中...
 
   



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

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