?~~ 2016-10-11 11:57 采纳率: 75%
浏览 1377
已采纳

c语言程序 ———栈的实现

#include
#include
#define STACK_INIT_SIZE 10
#define STACKINCREMENT 2
struct Stack
{
char base;
char *top;
int stacksize;
};
struct Stack *InitStack(struct Stack *S) //创建空栈
{
S->base=(char *)malloc(STACK_INIT_SIZE * sizeof(char));
if(!S->base) {printf("error!"); return 0;}
S->top=S->base;
S->stacksize=STACK_INIT_SIZE ;
return S;
}
struct Stack *Push(struct Stack *S,char e) //向栈中插入元素
{
if(S->top-S->base==S->stacksize)
{
S->base=(char *)realloc(S->base,(S->stacksize+STACKINCREMENT * sizeof(char)));
if(!S->base) {printf("分配空间失败"); return 0;}
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*(S->top)=e;
S->top++;
return S;
}
void Pop(struct Stack *S,char e) //删除栈顶元素,并且返回其值
{
if(S->top==S->base) {printf("栈为空,无法删除栈顶元素"); return 0;}
e=
(--S->top);
printf("删除的栈顶元素为:");
printf("%c\n",e);
}
int main()
{
struct Stack *S;
int e;
char i;
S=(struct Stack *)malloc(STACK_INIT_SIZE * sizeof(char));
if( !S->base) printf("error!");
InitStack(S);
printf("输入要插入的元素e:");
scanf("%c",&e);
Push(S,&e);
Pop(S,&i);
return 0;

}

以上是我写的c语言程序,运行的时候不管输入什么,输出的都是x,求哪位大神讲解下为什么,实在是百思不得其解

  • 写回答

2条回答 默认 最新

  • threenewbee 2016-10-11 12:04
    关注

    你的代码都不能编译

     #include<stdio.h>
    #include<stdlib.h>
    #define STACK_INIT_SIZE 10
    #define STACKINCREMENT 2
    struct  Stack
    {
        char *base;
        char *top;
        int stacksize;
    };
    struct  Stack *InitStack(struct Stack *S)         //创建空栈
    {
        S->base=(char *)malloc(STACK_INIT_SIZE * sizeof(char));
        if(!S->base)  {printf("error!");  return 0;}
        S->top=S->base;
        S->stacksize=STACK_INIT_SIZE ;
        return S;
    }
    struct Stack *Push(struct Stack *S,char e)       //向栈中插入元素
    {
        if(S->top-S->base==S->stacksize)
        {
            S->base=(char *)realloc(S->base,(S->stacksize+STACKINCREMENT * sizeof(char)));
            if(!S->base) {printf("分配空间失败"); return 0;}
            S->top=S->base+S->stacksize;
            S->stacksize+=STACKINCREMENT;
        }
        *(S->top)=e;
        S->top++;
        return S;
    }
    void Pop(struct Stack *S,char e)      //删除栈顶元素,并且返回其值
    {
        if(S->top==S->base)  {printf("栈为空,无法删除栈顶元素"); return; } //void函数不能return 0;
        e=*(--S->top);
        printf("删除的栈顶元素为:");
        printf("%c\n",e);
    }
    int main()
    {
        struct Stack *S;
        char e; // 不能是int
        char i;
        S=(struct Stack *)malloc(STACK_INIT_SIZE * sizeof(char));
        if( !S->base) printf("error!");
        InitStack(S);
        printf("输入要插入的元素e:");
        scanf("%c",&e);
        Push(S,e); //函数里要求char,这里不能取地址
        Pop(S,i); // 同上
        return 0;
    
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题