|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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 |
|