#!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # 7_6_app.c 7_6_apph.c 7_6_clear.c 7_6_cons1.c 7_6_cons2.c 7_6_dest.c 7_6_getn.c 7_6_getp.c 7_6_ins.c 7_6_insh.c 7_6_next.c 7_6_prev.c 7_6a.h 7_6b.h 7_6c.c 7_6d.c 7_6e.c 7_6f.h 7_6g1.gc 7_6g2.gc 7_6gI1.gc 7_6gI2.gc 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:43 EDT 1990 # echo x - 7_6_app.c sed 's/^X//' > 7_6_app.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::insert() except // that dlist::last is adjusted afterwards. void dlist::append(ent a) { this->insert(a); last = last->next; } !EOF! ls -l 7_6_app.c echo x - 7_6_apph.c sed 's/^X//' > 7_6_apph.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::appendhere(ent a) { if (curr) curr->append(new dlink(a)); else this->append(a); } !EOF! ls -l 7_6_apph.c echo x - 7_6_clear.c sed 's/^X//' > 7_6_clear.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // clear the list void dlist::clear() { dlink *l = last; if (!l) return; do { dlink *ll = l; l = l->next; delete ll; } while (l != last); last = 0; } !EOF! ls -l 7_6_clear.c echo x - 7_6_cons1.c sed 's/^X//' > 7_6_cons1.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ dlist::dlist() { last = curr = 0; } !EOF! ls -l 7_6_cons1.c echo x - 7_6_cons2.c sed 's/^X//' > 7_6_cons2.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ dlist::dlist(ent a) { curr = 0; last = new dlink(a); last->next = last->prev = last; } !EOF! ls -l 7_6_cons2.c echo x - 7_6_dest.c sed 's/^X//' > 7_6_dest.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // get rid of the list dlist::~dlist() { clear(); } !EOF! ls -l 7_6_dest.c echo x - 7_6_getn.c sed 's/^X//' > 7_6_getn.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::getnext(ent &a) { if (!last) return 0; // choose the link to remove dlink *f; if (curr) if (curr == last) { curr = 0; return 0; } else f = curr->next; else f = last->next; a = f->e; // remove f from the list if (f == last) last = 0; else f->remove(); delete f; return 1; } !EOF! ls -l 7_6_getn.c echo x - 7_6_getp.c sed 's/^X//' > 7_6_getp.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::getprev(ent &a) { if (!last) return 0; // choose the link to remove dlink *f; if (curr) if (curr == last->prev) { curr = 0; return 0; } else f = curr->prev; else f = curr = last; a = f->e; // remove f from the list if (f == last) last = 0; else f->remove(); delete f; return 1; } !EOF! ls -l 7_6_getp.c echo x - 7_6_ins.c sed 's/^X//' > 7_6_ins.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::insert(ent a) { if (last) last->next->insert(new dlink(a)); else { last = new dlink(a); last->next = last->prev = last; } } !EOF! ls -l 7_6_ins.c echo x - 7_6_insh.c sed 's/^X//' > 7_6_insh.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::inserthere(ent a) { if (curr) curr->insert(new dlink(a)); else this->insert(a); } !EOF! ls -l 7_6_insh.c echo x - 7_6_next.c sed 's/^X//' > 7_6_next.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::next(ent &a) { if (!last) return 0; // move curr forward, possibly to // the beginning or end of the list. if (curr) if (curr == last) { curr = 0; return 0; } else curr = curr->next; else curr = last->next; a = curr->e; return 1; } !EOF! ls -l 7_6_next.c echo x - 7_6_prev.c sed 's/^X//' > 7_6_prev.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::prev(ent &a) { if (!last) return 0; // move curr backwards, possibly to // the beginning or end of the list. if (curr) if (curr == last->next) { curr = 0; return 0; } else curr = curr->prev; else curr = last; a = curr->e; return 1; } !EOF! ls -l 7_6_prev.c echo x - 7_6a.h sed 's/^X//' > 7_6a.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ typedef void *ent; !EOF! ls -l 7_6a.h echo x - 7_6b.h sed 's/^X//' > 7_6b.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; 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_6b.h echo x - 7_6c.c sed 's/^X//' > 7_6c.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // insert a dlink in front of this one void dlink::insert(dlink *newlink) { if (prev) prev->next = newlink; newlink->next = this; newlink->prev = prev; prev = newlink; } !EOF! ls -l 7_6c.c echo x - 7_6d.c sed 's/^X//' > 7_6d.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // append a dlink after this one void dlink::append(dlink *newlink) { if (next) next->prev = newlink; newlink->next = next; newlink->prev = this; next = newlink; } !EOF! ls -l 7_6d.c echo x - 7_6e.c sed 's/^X//' > 7_6e.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // remove this link from the list void dlink::remove() { if (prev) prev->next = next; if (next) next->prev = prev; next = prev = 0; } !EOF! ls -l 7_6e.c echo x - 7_6f.h sed 's/^X//' > 7_6f.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // manage a doubly-linked list class dlist { dlink *last; dlink *curr; public: dlist(); dlist(ent e); ~dlist(); int isempty() { return last != 0; } void clear(); // 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_6f.h echo x - 7_6g1.gc sed 's/^X//' > 7_6g1.gc << '!EOF!' X.DS CB X.GS slist::last | v -------- -------- -------- -------- @->| next |--->| next |--->| next |---> ... --->| next |--@ | -------- -------- -------- -------- | | | ent | | ent | | ent | | ent | | | -------- -------- -------- -------- | | @ @--------------------------------------------------------- X.GE X.DE !EOF! ls -l 7_6g1.gc echo x - 7_6g2.gc sed 's/^X//' > 7_6g2.gc << '!EOF!' X.DS CB X.GS dlist::last | v -------- -------- -------- -------- @--->| next |--->| next |--->| next |---> ... --->| next |----@ | -------- -------- -------- -------- | | @--| prev |<---| prev |<---| prev |<--- ... <---| prev |<-@ | | | -------- -------- -------- -------- | | | | | ent | | ent | | ent | | ent | | | | | -------- -------- -------- -------- | | | | | | | @---------------------------------------------------------@ | @-------------------------------------------------------------@ X.GE X.DE !EOF! ls -l 7_6g2.gc echo x - 7_6gI1.gc sed 's/^X//' > 7_6gI1.gc << '!EOF!' X.DS CB X.GS X this | | v v -------- -------- | next |--->| next | -------- -------- | prev |<---| prev | -------- -------- | ent | | ent | -------- -------- X.GE X.DE !EOF! ls -l 7_6gI1.gc echo x - 7_6gI2.gc sed 's/^X//' > 7_6gI2.gc << '!EOF!' X.DS CB X.GS X new this | | | v v v -------- -------- -------- | next |--->| next |--->| next | -------- -------- -------- | prev |<---| prev |<---| prev | -------- -------- -------- | ent | | ent | | ent | -------- -------- -------- X.GE X.DE !EOF! ls -l 7_6gI2.gc 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_6b.h" #include "7_6f.h" #include "7_6_app.c" #include "7_6_apph.c" #include "7_6_clear.c" #include "7_6_cons1.c" #include "7_6_cons2.c" #include "7_6_getn.c" #include "7_6_getp.c" #include "7_6_ins.c" #include "7_6_insh.c" #include "7_6_next.c" #include "7_6_prev.c" #include "7_6c.c" #include "7_6d.c" #include "7_6e.c" #include "7_6_dest.c" !EOF! ls -l all.c echo x - makefile sed 's/^X//' > makefile << '!EOF!' CC= CC -I. -I../../CC all: tst tst: tst.o all.o $(CC) -o tst tst.o all.o tst.o: tst.c 7_6a.h 7_6b.h 7_6f.h $(CC) -c tst.c all.o: all.c 7_6a.h 7_6b.h 7_6f.h 7_6_app.c 7_6_apph.c 7_6_clear.c 7_6_cons1.c 7_6_cons2.c 7_6_getn.c 7_6_getp.c 7_6_ins.c 7_6_insh.c 7_6_next.c 7_6_prev.c 7_6_dest.c 7_6c.c 7_6d.c 7_6e.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_6b.h" #include "7_6f.h" #include main() { dlist xdlist("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