KingExercises-ch19-21and23-24

KingExercises-ch19-21and23-24 - Answers to Book Exercises...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Answers to Book Exercises – Chapters 19-21, 23-24 Here are the answers to exercises from the second edition of C Programming: A Modern Approach . These answers do not necessarily conform to the coding standards for this course. Note: Some of these exercises were originally the even-numbered exercises in the first edition . For the benefit of those who have the first edition, the original exercise number is given in square brackets. For example, the notation [was #4] indicates that the number of the exercise was 4 in the first edition. If an answer is different because of second-edition changes, the word "modified" will appear inside the brackets: [was #4; modified]. Answers to Exercises (Chapter 19) 1. #ifndef QUEUE_H #define QUEUE_H #include <stdbool.h> /* C99 only */ typedef int Item; void make_empty(void); bool is_empty(void); bool is_full(void); void enqueue(Item i); Item dequeue(void); Item first(void); Item last(void); #endif 2. [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) {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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; } 3. (a) #include <stdio.h> #include <stdlib.h> #include "queue.h"
Background image of page 2
#define QUEUE_SIZE 100 static Item contents[QUEUE_SIZE]; static int head = 0, tail = 0, len = 0; static void terminate(const char *message) { printf("%s\n", message); exit(EXIT_FAILURE); } void make_empty(void) { head = tail = len = 0; } bool is_empty(void) { return len == 0; } bool is_full(void) { return len == QUEUE_SIZE; } void enqueue(Item i) { if (is_full()) terminate("Error in enqueue: queue is full."); contents[tail++] = i; if (tail == QUEUE_SIZE) tail = 0; len++; } Item dequeue(void) { Item i; if (is_empty()) terminate("Error in dequeue: queue is empty."); i = contents[head]; head = (head + 1) % QUEUE_SIZE; len--; return i; } Item first(void) {
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
if (is_empty()) terminate("Error in first: queue is empty."); return contents[head]; } Item last(void) { if (is_empty()) terminate("Error in last: queue is empty."); if (tail == 0) return contents[QUEUE_SIZE-1]; else return contents[tail-1]; } (b) #include <stdio.h> #include <stdlib.h> #include "queue.h" struct node { Item data; struct node *next; }; static struct node *head = NULL, *tail = NULL; static void terminate(const char *message) { printf("%s\n", message); exit(EXIT_FAILURE);
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/14/2010 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas.

Page1 / 20

KingExercises-ch19-21and23-24 - Answers to Book Exercises...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online