#!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # 7_7.h 7_7_dlink.h 7_7ahere.c 7_7append.c 7_7dlist.h 7_7getnext.c 7_7getprev.c 7_7ihere.c 7_7insert.c 7_7next.c 7_7prev.c all.c makefile tst.c tst.cmp # # 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:10:58 EDT 1990 # echo x - 7_7.h sed 's/^X//' > 7_7.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // an iterator for a doubly-linked list class dlist_iterator { dlink *curr; dlist *curr_dlist; public: dlist_iterator(dlist &d) { curr_dlist = &d; curr = 0; } // set current pointer to the ends of the list void reset() { curr = 0; } // move around the list, leaving the links there int next(ent &e); int prev(ent &e); // move around the list, removing the links int getnext(ent &e); int getprev(ent &e); // manipulate around the beginning // and end of the list void insert(ent e); void append(ent e); // manipulate around the current entry void inserthere(ent e); void appendhere(ent e); }; !EOF! ls -l 7_7.h echo x - 7_7_dlink.h sed 's/^X//' > 7_7_dlink.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ class dlink { friend class dlist; friend class dlist_iterator; dlink *next; dlink *prev; ent e; dlink(ent a, dlink *n = 0, dlink *p = 0) { e = a; next = n; prev = p; } void insert(dlink *n); void append(dlink *n); void remove(); ~dlink() { remove(); } }; !EOF! ls -l 7_7_dlink.h echo x - 7_7ahere.c sed 's/^X//' > 7_7ahere.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Append an entry after the // current location of the list. // If curr is not set, append at // the end of the list. void dlist_iterator::appendhere(ent a) { if (curr) curr->append(new dlink(a)); else this->append(a); } !EOF! ls -l 7_7ahere.c echo x - 7_7append.c sed 's/^X//' > 7_7append.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Append an entry at the end of the list. // This is the same as dlist_iterator::insert() // except that dlist_iterator::curr_dlist->last // is adjusted afterwards. void dlist_iterator::append(ent a) { this->insert(a); curr_dlist->last = curr_dlist->last->next; } !EOF! ls -l 7_7append.c echo x - 7_7dlist.h sed 's/^X//' > 7_7dlist.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ class dlist { friend dlist_iterator; dlink *last; public: dlist() { last = 0; } dlist(ent a) { last = new dlink(a); last->next = last->prev = last; } int isempty() { return last != 0; } void clear(); ~dlist() { clear(); } }; !EOF! ls -l 7_7dlist.h echo x - 7_7getnext.c sed 's/^X//' > 7_7getnext.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Get the next entry from a list, // removing it afterwards. // curr is left alone. int dlist_iterator::getnext(ent &a) { if (!curr_dlist->last) return 0; // choose the link to remove dlink *f; if (curr) if (curr == curr_dlist->last) { curr = 0; return 0; } else f = curr->next; else f = curr_dlist->last->next; a = f->e; // remove f from the list if (f == curr_dlist->last) curr_dlist->last = 0; else f->remove(); delete f; return 1; } !EOF! ls -l 7_7getnext.c echo x - 7_7getprev.c sed 's/^X//' > 7_7getprev.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Get the previous entry from a list, // removing it afterwards int dlist_iterator::getprev(ent &a) { if (!curr_dlist->last) return 0; // choose the link to remove dlink *f; if (curr) if (curr == curr_dlist->last->prev) { curr = 0; return 0; } else f = curr->prev; else curr = curr_dlist->last; a = f->e; // remove f from the list if (f == curr_dlist->last) curr_dlist->last = 0; else f->remove(); delete f; return 1; } !EOF! ls -l 7_7getprev.c echo x - 7_7ihere.c sed 's/^X//' > 7_7ihere.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Insert an entry before the // current location of the list. // If curr is not set, insert at // the beginning of the list. void dlist_iterator::inserthere(ent a) { if (curr) curr->insert(new dlink(a)); else this->insert(a); } !EOF! ls -l 7_7ihere.c echo x - 7_7insert.c sed 's/^X//' > 7_7insert.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // insert an entry at the beginning of the list void dlist_iterator::insert(ent a) { if (curr_dlist->last) curr_dlist->last->next->insert(new dlink(a)); else { curr_dlist->last = new dlink(a); curr_dlist->last->next = curr_dlist->last->prev = curr_dlist->last; } } !EOF! ls -l 7_7insert.c echo x - 7_7next.c sed 's/^X//' > 7_7next.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Get the next entry from a list, // moving curr forward. int dlist_iterator::next(ent &a) { if (!curr_dlist->last) return 0; // move curr forward, possibly to // the beginning or end of the list. if (curr) if (curr == curr_dlist->last) { curr = 0; return 0; } else curr = curr->next; else curr = curr_dlist->last->next; a = curr->e; return 1; } !EOF! ls -l 7_7next.c echo x - 7_7prev.c sed 's/^X//' > 7_7prev.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Get the previous entry from a list, // moving curr backwards. int dlist_iterator::prev(ent &a) { if (!curr_dlist->last) return 0; // move curr backwards, possibly to // the beginning or end of the list. if (curr) if (curr == curr_dlist->last->next) { curr = 0; return 0; } else curr = curr->prev; else curr = curr_dlist->last; a = curr->e; return 1; } !EOF! ls -l 7_7prev.c echo x - all.c sed 's/^X//' > all.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include "7_6a.h" #include "7_7_dlink.h" #include "7_7.h" #include "7_7dlist.h" #include "7_6c.c" #include "7_6d.c" #include "7_6e.c" #include "7_6_clear.c" #include "7_7ahere.c" #include "7_7append.c" #include "7_7getnext.c" #include "7_7getprev.c" #include "7_7ihere.c" #include "7_7insert.c" #include "7_7next.c" #include "7_7prev.c" !EOF! ls -l all.c echo x - makefile sed 's/^X//' > makefile << '!EOF!' CC= CC -I. -I../7.6dir -I../../CC CFLAGS= all: tst tst: tst.o all.o $(CC) -o tst tst.o all.o tst.o: tst.c ../7.6dir/7_6a.h 7_7_dlink.h 7_7dlist.h 7_7.h $(CC) -c tst.c all.o: all.c ../7.6dir/7_6a.h 7_7_dlink.h \ ../7.6dir/7_6c.c ../7.6dir/7_6d.c ../7.6dir/7_6e.c \ ../7.6dir/7_6_clear.c \ 7_7ahere.c 7_7append.c 7_7getnext.c 7_7getprev.c \ 7_7ihere.c 7_7insert.c 7_7next.c 7_7prev.c $(CC) -c all.c CMP= tst.cmp OUT= tst.out tst.out: tst ; tst > tst.out test: all $(CMP) $(OUT) cmp tst.out tst.cmp @echo test done !EOF! ls -l makefile echo x - tst.c sed 's/^X//' > tst.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include "7_6a.h" #include "7_7_dlink.h" #include "7_7.h" #include "7_7dlist.h" #include main() { dlist odlist; dlist_iterator xdlist(odlist); xdlist.append("hello"); xdlist.append("there"); xdlist.append("how"); xdlist.append("are"); xdlist.append("you"); xdlist.append("today"); void *x; cout << "forwards\n"; while (xdlist.next(x)) cout << "'" << (char*)x << "'\n"; cout << "\nbackwards\n"; while (xdlist.prev(x)) cout << "'" << (char*)x << "'\n"; xdlist.next(x); xdlist.next(x); xdlist.inserthere("Beep"); xdlist.appendhere("Boop"); xdlist.reset(); cout << "\nforwards, removing\n"; while (xdlist.getnext(x)) cout << "'" << (char*)x << "'\n"; cout << "\nbackwards\n"; while (xdlist.getprev(x)) cout << "'" << (char*)x << "'\n"; return 0; } !EOF! ls -l tst.c echo x - tst.cmp sed 's/^X//' > tst.cmp << '!EOF!' forwards 'hello' 'there' 'how' 'are' 'you' 'today' backwards 'today' 'you' 'are' 'how' 'there' 'hello' forwards, removing 'hello' 'Beep' 'there' 'Boop' 'how' 'are' 'you' 'today' backwards !EOF! ls -l tst.cmp # The following exit is to ensure that extra garbage # after the end of the shar file will be ignored. exit 0