鱼C论坛

 找回密码
 立即注册
查看: 4041|回复: 2

关于typedef int Item;输出结果

[复制链接]
发表于 2012-10-20 21:42:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
文件stackclient.c:
#include <stdio.h>
#include "stackADT.h"

int main()
{
        Stack s1,s2;
        Item n;
       
        s1 = creat();
        s2 = creat();
       
        push(s1,1);
        push(s1,2);
       
        n = pop(s1);
        printf("Popped %d from s1.\n",n);
        push(s2,n);
        n = pop(s1);
        printf("Popped %d from s1.\n",n);
        push(s2,n);
       
        destroy(s1);
       
        while(!is_empty(s2))
                printf("Popped %d from s2.\n",pop(s2));
       
        push(s2,3);
        printf("Popped %d from s2.\n",pop(s2));
       
        make_empty(s2);
        if(is_empty(s2))
                printf("s2 is empty.\n");
        else
                printf("s2 is not empty.\n");
       
        destroy(s2);
       
        return 0;       
}

文件stackADT.c:
#include <stdio.h>
#include <stdlib.h>
#include "stackADT.h"

struct stack_type{
        Item *contents;
        int top;
        int size;
};

static void terminate(const char *message)
{
        printf("%s\n",message);
        exit(EXIT_FAILURE);
}

Stack creat(int size)
{
        Stack s = malloc(sizeof(struct stack_type));
        if(s == NULL)
                printf("Error in create:stack could not be created.");
        s->contents = malloc(size*sizeof(Item));
        if (s->contents == NULL)
        {
                free(s);
                terminate("Error in create:stack could not be created.");
        }
        s->top = 0;
        s->size = size;
        return s;
}

文件stackADT.h:
#ifndef STACKADT_H
#define STACKADT_H

typedef enum{TRUE,FALSE} bool;

typedef int Item;

typedef struct stack_type *Stack;

Stack creat(void);
void destroy(Stack s);
void make_empty(Stack s);
bool is_empty(Stack s);
bool is_full(Stack s);
void push(Stack s,Item i);
Item pop(Stack s);

#endif

为什么输出结果是:Error in push:stack is full.
                           Press any key to continue

而不是:Popped 2 from s1.
            Popped 1 from s1.
            Popped 1 from s2.
            Popped 2 from s2.
            Popped 3 from s2.
            s2 is empty.
            Press any key to continue
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-10-20 22:49:04 | 显示全部楼层
create函数没传size?我猜的。这个直接在网上找个模板套套就行了。。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-10-21 11:46:56 | 显示全部楼层
Error in push:stack is full.
字面的意思是栈满了,

文件少了。push和pop的定义,把那个文件包含发出来看看。
Error in push:stack is full.这个错误也是这个文件里的,不是系统错误,c语言不可能真是汇编,只是用结构体模拟栈罢了
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-15 20:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表