#!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # 5_6A.h 5_6B.h 5_6a.c 5_6a.cmp 5_6b.c 5_6b.cmp 5_6test.c char_queue.h makefile # # To extract the files from this shell archive file simply # create a directory for this file, move the archive file # to it and enter the command # # sh filename # # The files will be extracted automatically. # Note: Do not use csh. # # Archive created: Mon Jul 30 23:04:03 EDT 1990 # echo x - 5_6A.h sed 's/^X//' > 5_6A.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Exercise 5.6, part 1 // Implement a character queue using a linked list. // #ifndef CHAR_QUEUE_H # define CHAR_QUEUE_H class char_queue { struct cQtable { char c; struct cQtable *next; } *rear, *front; public: char_queue(int nummembers); ~char_queue(); void enqueue(char s); char dequeue(); int emptyQ() { return (front == 0); } void clearQ(); }; #endif /* CHAR_QUEUE_H */ !EOF! ls -l 5_6A.h echo x - 5_6B.h sed 's/^X//' > 5_6B.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Exercise 5.6, part 2 // Implement a character queue using a vector. // #ifndef CHAR_QUEUE_H # define CHAR_QUEUE_H class char_queue { char *base, *front, *rear, *end; public: char_queue(int nummembers); ~char_queue(); void enqueue(char s); char dequeue(); int emptyQ() { return (rear == front); } void clearQ(); }; #endif /* CHAR_QUEUE_H */ !EOF! ls -l 5_6B.h echo x - 5_6a.c sed 's/^X//' > 5_6a.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // implement a character queue using // a linked list #include #include char_queue:: char_queue(int) { rear = front = 0; } char_queue:: ~char_queue() { this->clearQ(); } void char_queue:: enqueue(char c) { if (rear) { rear->next = new cQtable; rear = rear->next; } else front = rear = new cQtable; rear->next = 0; rear->c = c; } char char_queue:: dequeue() { if (emptyQ()) error("underflow of character queue"); char c = front->c; if (front == rear) { delete front; rear = front = 0; } else { cQtable *old = front; front = front->next; delete old; } return c; } void char_queue:: clearQ() { while (front) { cQtable *old = front; front = front->next; delete old; } rear = 0; } !EOF! ls -l 5_6a.c echo x - 5_6a.cmp sed 's/^X//' > 5_6a.cmp << '!EOF!' adding t adding h adding i adding s adding adding i adding s adding adding a adding adding t adding e adding s adding t got t got h got i got s got got i got s got got a got got t got e got s got t !EOF! ls -l 5_6a.cmp echo x - 5_6b.c sed 's/^X//' > 5_6b.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // implement a character queue using // an array #include #include char_queue:: char_queue(int nummembers) { base = new char[nummembers]; end = &base[nummembers]; front = rear = base; } char_queue:: ~char_queue() { delete base; } void char_queue:: enqueue(char c) { *rear++ = c; if (rear == end) rear = base; if (rear == front) error("overflow of character queue"); } char char_queue:: dequeue() { if (emptyQ()) error("underflow of character queue"); char ch = *front++; if (front == end) front = base; return ch; } void char_queue:: clearQ() { front = rear = base; } !EOF! ls -l 5_6b.c echo x - 5_6b.cmp sed 's/^X//' > 5_6b.cmp << '!EOF!' adding t adding h adding i adding s adding adding i adding s adding adding a adding adding t adding e adding s adding t got t got h got i got s got got i got s got got a got got t got e got s got t !EOF! ls -l 5_6b.cmp echo x - 5_6test.c sed 's/^X//' > 5_6test.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include "char_queue.h" #include char array[] = "this is a test"; main() { char_queue c(100); for (char *a = array; *a; a++) { char x = *a; cout << "adding " << chr(x) << "\n"; c.enqueue(x); } while (!c.emptyQ()) { char x = c.dequeue(); cout << "got " << chr(x) << "\n"; } return 0; } !EOF! ls -l 5_6test.c echo x - char_queue.h sed 's/^X//' > char_queue.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #ifdef A #include "5_6A.h" #else #include "5_6B.h" #endif !EOF! ls -l char_queue.h echo x - makefile sed 's/^X//' > makefile << '!EOF!' CC= CC -I. -I../../CC ERROR= ../../error.o CFLAGS= -g -I. all: 5_6a 5_6b 5_6a: 5_6a.o 5_6test.c $(CC) $(CFLAGS) -DA 5_6a.o 5_6test.c -o 5_6a $(ERROR) 5_6b: 5_6b.o 5_6test.c $(CC) $(CFLAGS) -DB 5_6b.o 5_6test.c -o 5_6b $(ERROR) 5_6a.o: 5_6a.c 5_6A.h char_queue.h $(CC) $(CFLAGS) -DA -c 5_6a.c 5_6b.o: 5_6b.c 5_6B.h char_queue.h $(CC) $(CFLAGS) -DB -c 5_6b.c CMP= 5_6a.cmp 5_6b.cmp OUT= 5_6a.out 5_6b.out 5_6a.out: 5_6a ; 5_6a > 5_6a.out 5_6b.out: 5_6b ; 5_6b > 5_6b.out test: all $(OUT) $(CMP) cmp 5_6a.out 5_6a.cmp cmp 5_6b.out 5_6b.cmp echo tests done !EOF! ls -l makefile # The following exit is to ensure that extra garbage # after the end of the shar file will be ignored. exit 0