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

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

int   InitStack(SqStack *S);
void  DestroyStack(SqStack *S);
void  ClearStack(SqStack *S);
int   StackEmpty(SqStack *S);
int   StackLength(SqStack *S);
int   GetTop(SqStack *S);
void  Push(SqStack *S,int e);
int  Pop(SqStack *S);
void  print(SqStack *S);

main(){
    SqStack S;
    char c;
    int i,k,choose,e,n,num,N;
    k=1;
    if(InitStack(&S)!=1) printf("存储分配失败!");
    printf("输入栈长:");
    scanf("%d",&n);
    printf("元素入栈:\n ");
    for(i=0;i<n;i++){
         scanf("%d",&e);
         Push(&S,e);
    }
    printf("\n\n\n\n");
    printf("\n\t\t\t     选择要执行的操作       ");
    printf("\n\t\t\t*******************************");
    printf("\n\t\t\t    1. 取栈顶元素       ");
    printf("\n\t\t\t    2.向栈插入一个元素   ");
    printf("\n\t\t\t    3.删除一栈元素       ");
    printf("\n\t\t\t    4.置栈空       ");
    printf("\n\t\t\t    5.测栈空  ");
    printf("\n\t\t\t    6.显示栈的长度       ");
    printf("\n\t\t\t    7.显示栈中元素 ");
    printf("\n\t\t\t*******************************\n");
    while(k){
        scanf("%d",&choose);
        switch(choose){
            case 1: if(GetTop(&S)==0) printf("该栈为空栈!\n");
                    else printf("栈顶为: %d\n",GetTop(&S));
                    break;
            case 2: printf("输入一个元素: ");
                    scanf("%d",&e);
                    Push(&S,e);
                    print(&S);
                    break;
            case 3: if(Pop(&S)==0)  printf("顺序栈为空,出栈失败!\n");
                    else print(&S);  
                    break;
            case 4: ClearStack(&S); break;
            case 5: if(StackEmpty(&S)==0) printf("栈已为空!\n");
                    else printf("该栈不为空!\n");
                    break;
            case 6: printf("栈长为: %d\n",StackLength(&S)); break;
            case 7: print(&S); break;
        
        getchar();
        printf("\nDo you want to continue?(Y/N)");
        scanf("%c",&c);
        if(c=='Y'||c=='y') k=1;
        else k=0;
    }
    DestroyStack(&S);
}

int InitStack(SqStack *S){
    S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
    if(S.base==NULL){
        printf("顺序栈初始存储空间分配失败!\n");
        exit(1);
    }
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    return 1;
}

int GetTop(SqStack *S){
    if(S->top==S->base) return 0 ;
    return *(S->top-1);
}

void Push(SqStack *S,int e){
    int length;
    length=S->top-S->base;
    if(length>= S->stacksize){
         S->base=(int *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(int));
         if(S->base==NULL) exit(1);
         S->top=S->base+length;
         S->stacksize+=STACKINCREMENT;
    }
    *(S->top++)=e;
}

int Pop(SqStack *S){
    int e;
    if((S->top - S->base)==0)
       return 0;
    else{
       e=*(--S->top);
       return e;
    }
}

int StackLength(SqStack *S){
    return S->top-S->base;
}

void print(SqStack *S){
    int i,length;
    length=S->top - S->base;
    printf("此时顺序栈从栈底到栈顶依次为:  ");
    for(i=0;i<length;i++){
        printf("%d   ",S->base[i]);
    }
}

void ClearStack(SqStack *S){
     S->top=S->base;
}

int StackEmpty(SqStack *S){
    if(S->top==S->base) return 0;
    else return 1;
}

void DestroyStack(SqStack *S){
    free(S->base);
    S->base=NULL;
    S->top=NULL;
    S->stacksize=0;
}
这个程序有个错误不知道怎么改啊
搜索更多相关主题的帖子: 改错 
2010-06-02 10:23
linjx0123
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:279
专家分:1362
注 册:2006-4-7
收藏
得分:9 
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100  
#define STACKINCREMENT 10

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

int   InitStack(SqStack *S);
void  DestroyStack(SqStack *S);
void  ClearStack(SqStack *S);
int   StackEmpty(SqStack *S);
int   StackLength(SqStack *S);
int   GetTop(SqStack *S);
void  Push(SqStack *S,int e);
int  Pop(SqStack *S);
void  print(SqStack *S);

main(){
    SqStack S;
    char c;
    int i,k,choose,e,n,num,N;
    k=1;
    if(InitStack(&S)!=1) printf("存储分配失败!");
    printf("输入栈长:");
    scanf("%d",&n);
    printf("元素入栈:\n ");
    for(i=0;i<n;i++){
         scanf("%d",&e);
         Push(&S,e);
    }
    printf("\n\n\n\n");
    printf("\n\t\t\t     选择要执行的操作       ");
    printf("\n\t\t\t*******************************");
    printf("\n\t\t\t    1. 取栈顶元素       ");
    printf("\n\t\t\t    2.向栈插入一个元素   ");
    printf("\n\t\t\t    3.删除一栈元素       ");
    printf("\n\t\t\t    4.置栈空       ");
    printf("\n\t\t\t    5.测栈空  ");
    printf("\n\t\t\t    6.显示栈的长度       ");
    printf("\n\t\t\t    7.显示栈中元素 ");
    printf("\n\t\t\t*******************************\n");
    while(k){
        scanf("%d",&choose);
        switch(choose){
            case 1: if(GetTop(&S)==0) printf("该栈为空栈!\n");
                    else printf("栈顶为: %d\n",GetTop(&S));
                    break;
            case 2: printf("输入一个元素: ");
                    scanf("%d",&e);
                    Push(&S,e);
                    print(&S);
                    break;
            case 3: if(Pop(&S)==0)  printf("顺序栈为空,出栈失败!\n");
                    else print(&S);  
                    break;
            case 4: ClearStack(&S); break;
            case 5: if(StackEmpty(&S)==0) printf("栈已为空!\n");
                    else printf("该栈不为空!\n");
                    break;
            case 6: printf("栈长为: %d\n",StackLength(&S)); break;
            case 7: print(&S); break;
        }
        getchar();
        printf("\nDo you want to continue?(Y/N)");
        scanf("%c",&c);
        if(c=='Y'||c=='y') k=1;
        else k=0;
    }
    DestroyStack(&S);
}

int InitStack(SqStack *S){
    S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
    if(S->base==NULL){
        printf("顺序栈初始存储空间分配失败!\n");
        exit(1);
    }
    S->top=S->base;
    S->stacksize=STACK_INIT_SIZE;

    return 1;
}

int GetTop(SqStack *S){
    if(S->top==S->base) return 0 ;
    return *(S->top-1);
}

void Push(SqStack *S,int e){
    int length;
    length=S->top-S->base;
    if(length>= S->stacksize){
         S->base=(int *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(int));
         if(S->base==NULL) exit(1);
         S->top=S->base+length;
         S->stacksize+=STACKINCREMENT;
    }
    *(S->top++)=e;
}

int Pop(SqStack *S){
    int e;
    if((S->top - S->base)==0)
       return 0;
    else{
       e=*(--S->top);
       return e;
    }
}

int StackLength(SqStack *S){
    return S->top-S->base;
}

void print(SqStack *S){
    int i,length;
    length=S->top - S->base;
    printf("此时顺序栈从栈底到栈顶依次为:  ");
    for(i=0;i<length;i++){
        printf("%d   ",S->base[i]);
    }
}

void ClearStack(SqStack *S){
     S->top=S->base;
}

int StackEmpty(SqStack *S){
    if(S->top==S->base) return 0;
    else return 1;
}

void DestroyStack(SqStack *S){
    free(S->base);
    S->base=NULL;
    S->top=NULL;
    S->stacksize=0;
}
2010-06-02 12:31
功夫熊猫
Rank: 2
等 级:论坛游民
帖 子:54
专家分:30
注 册:2010-7-14
收藏
得分:0 
楼上的那个程序真的可以运行么?
2010-07-18 14:08
zzz123890c
Rank: 1
等 级:新手上路
帖 子:5
专家分:3
注 册:2010-7-18
收藏
得分:0 
高难度 看不懂
2010-07-18 14:31
快速回复:栈问题改错
数据加载中...
 
   



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

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