Next: Bsp. Linked-List als Template-Klasse
Up: Templates, Exceptions sowie Pogrammierung
Previous: Vorgehensweise bei der Implemenmtation
#include <iostream.h> #include <stream.h> #include <iomanip.h> #include <new.h> extern "C" { // for exit() #include <stdlib.h> // for strcpy() and strlen() #include <string.h> } // forward reference class List; class Element { public: friend class List; Element(char *data); ~Element(); void Print(); private: char* Get(); void Set(char*); char *data; Element *next; Element *prior; }; Element::~Element() { cerr << "Destructor for Element called: " << endl; if (this) { cerr << this->data << endl; if (this->data) delete this->data; } } Element::Element(char *data) { this->data = new char [strlen(data)+1]; strcpy(this->data, data); next = prior = NULL; } void Element::Set(char *data) { delete this->data; this->data = new char [strlen(data)+1]; strcpy(this->data, data); } char* Element::Get() { return data; } void Element::Print() { cout << data; } class List { public: List(char *data); ~List(); Element* First(); Element* Next(Element *p); Element* Prior(Element *p); int Put_Begin(char *data); int Insert(char *data, Element *p); void Delete(char *data); Element* Find(char *data); void Print(); private: Element *first; Element *last; int Equal(char *t, char *s); }; void List::Print() { Element *e = first; while(e) { cout << setiosflags(ios::showbase | ios::uppercase) << "Element at " << hex << e << " with value " << e->data << " next: " << e->next << " prior: " << e->prior << endl; e = e->next; } } void List::Delete(char *data) { Element *found = Find(data); if (found) { if ((found == first) && (found == last)) first = last = NULL; else if (found == first) { first = found->next; if (first) first->prior = NULL; } else if (found == last) { last = found->prior; last->next = NULL; } else { (found->next)->prior = found->prior; (found->prior)->next = found->next; } delete found; } } List::~List() { cerr << "Destructor for List called: " << endl; Element *tmp, *e = first; while (e) { tmp = e->next; delete e; e = tmp; } } List::List(char *data) { last = first = new Element(data); last->prior = last->next = NULL; } int List::Put_Begin(char *data) { Element *n = new Element(data); n->prior = NULL; if (first) first->prior = n; n->next = first; first = n; return 1; } int List::Equal(char *t, char *s) { return !strcmp(t, s); // return t == s; } Element* List::Find(char* data) { Element *e = first; while(e) { if (Equal(data, e->data)) return e; else e = e->next; } return NULL; } Element* List::First() { return first; } Element* List::Next(Element *p) { if (p) return p->next; else return p; } Element* List::Prior(Element *p) { if (p) return p->prior; else return p; } void NoSpace() { cerr << "New Failed" << endl; exit(1); } main() { // Rufe die Funktion NoSpace() auf // wenn new keinen freien Speicher // belegen kann set_new_handler(NoSpace); List *l1 = new List("first"); Element *e; char c, buf[100]; cout << "** Double Linked List Example **" <<endl; while(1) { cout << "1) enter new value;" << endl; cout << "2) find value;" << endl; cout << "3) display linked list;" << endl; cout << "4) delete element;" << endl; cout << "9) end program;" << endl; cin >>c; switch(c) { case '1': cout << "enter new value "; cin >> buf; l1->Put_Begin(buf); break; case '2': cout << "enter value "; cin >> buf; e = l1->Find(buf); if (e) { cout << "habe "; e->Print(); cout << " gefunden" << endl; } else cout << buf << " wurde nicht gefunden" << endl; break; case '3': l1->Print(); break; case '4': cout << "enter value "; cin >> buf; l1->Delete(buf); break; case '9': delete l1; exit(0); default: cout << "unkown command!" << endl; } } }
© 1997 Gottfried Rudorfer, C++-AG, Lehrveranstaltungen, Abteilung für Angewandte Informatik, Wirtschaftsuniversität Wien, 3/19/1998 |