关于抽象数据类型的问题
今天看《C语言程序设计现代方法》中的大型程序设计,有很多小问题,请教各位高手
问题1.上面说
struct node{ int data; struct node *next;};struct stack_type{ struct node *top;};#include<stdio.h>#ifdef STACKADT_H#define STACKADT_Htypedef 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,int i);int pop(Stack s);#endif
#include<stdio.h>#include<stdlib.h>#include"stackADT.h"struct node{ int data; struct node *next;};struct stack_type{ struct node *top;};static void terminate(const char *message){ printf("%s\n",message); exit(EXIT_FAILURE);}Stack create(void){ Stack s = malloc(sizeof(struct stack_type)); if(s == NULL) terminate("Error ... "); s -> top = NULL; retutn s; }void destroy(Stack s){ make_empty(s); free(s);}void make_empty(Stack s){ while(!is_empty(s)) pop(s);}int is_empty(Stack s){ return s -> top == NULL;}int is_full(Stack s){ return false;}void push(Stack s,int i){ struct node * new_node = malloc(sizeof(struct node)); if(new_node == NULL) terminate("Error ..."); new_node -> data = i; new_node -> next = s -> top; s -> top = new_node;}int pop(Stack s){ struct node * old_top; int i; if(is_empty(s)) terminate("Error ..."); old_top = s -> top; i = old_top -> data; s -> top = old_top -> next; free(old_top); return i;}#include<stdio.h>#include"stackADT.h"int main(){ Stack s1,s2; int n; s1 = create(); s2 = create(); 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); make_empty(s2); if(is_empty(s2)) printf("s2 si empty\n"); else printf("s2 is not empty\n"); destroy(s2); return 0;}