|
|
|
Chapter 19Answers to Selected Exercises2. [was #2; modified] #include <stdio.h> #include <stdlib.h> #include "stack.h" #define PUBLIC /* empty */ #define PRIVATE static struct node { int data; struct node *next; }; PRIVATE struct node *top = NULL; PRIVATE void terminate(const char *message) { printf("%s\n", message); exit(EXIT_FAILURE); } PUBLIC void make_empty(void) { while (!is_empty()) pop(); } PUBLIC bool is_empty(void) { return top == NULL; } PUBLIC bool is_full(void) { return false; } PUBLIC void push(int i) { struct node *new_node = malloc(sizeof(struct node)); if (new_node == NULL) terminate("Error in push: stack is full."); new_node->data = i; new_node->next = top; top = new_node; } PUBLIC int pop(void) { struct node *old_top; int i; if (is_empty()) terminate("Error in pop: stack is empty."); old_top = top; i = top->data; top = top->next; free(old_top); return i; } 4. [was #4; modified]
(a) Contents of #include <stdio.h> #include <stdlib.h> #include "stack.h" static void terminate(const char *message) { printf("%s\n", message); exit(EXIT_FAILURE); } void make_empty(Stack *s) { s->top = 0; } bool is_empty(const Stack *s) { return s->top == 0; } bool is_full(const Stack *s) { return s->top == STACK_SIZE; } void push(Stack *s, int i) { if (is_full(s)) terminate("Error in push: stack is full."); s->contents[s->top++] = i; } int pop(Stack *s) { if (is_empty(s)) terminate("Error in pop: stack is empty."); return s->contents[--s->top]; }
(b) Contents of #ifndef STACK_H #define STACK_H #include <stdbool.h> /* C99 only */ struct node { int data; struct node *next; }; typedef struct node *Stack; void make_empty(Stack *s); bool is_empty(const Stack *s); bool is_full(const Stack *s); void push(Stack *s, int i); int pop(Stack *s); #endif
Contents of #include <stdio.h> #include <stdlib.h> #include "stack.h" static void terminate(const char *message) { printf("%s\n", message); exit(EXIT_FAILURE); } void make_empty(Stack *s) { while (!is_empty(s)) pop(s); } bool is_empty(const Stack *s) { return *s == NULL; } bool is_full(const Stack *s) { return false; } void push(Stack *s, int i) { struct node *new_node = malloc(sizeof(struct node)); if (new_node == NULL) terminate("Error in push: stack is full."); new_node->data = i; new_node->next = *s; *s = new_node; } int pop(Stack *s) { struct node *old_top; int i; if (is_empty(s)) terminate("Error in pop: stack is empty."); old_top = *s; i = (*s)->data; *s = (*s)->next; free(old_top); return i; } Copyright © 2008, 1996 W. W. Norton & Company, Inc. All rights reserved. |