home *** CD-ROM | disk | FTP | other *** search
- // This may look like C code, but it is really -*- C++ -*-
- /*
- Copyright (C) 1988 Free Software Foundation
- written by Doug Lea (dl@rocky.oswego.edu)
-
- This file is part of GNU CC.
-
- GNU CC is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY. No author or distributor
- accepts responsibility to anyone for the consequences of using it
- or for whether it serves any particular purpose or works at all,
- unless he says so in writing. Refer to the GNU CC General Public
- License for full details.
-
- Everyone is granted permission to copy, modify and redistribute
- GNU CC, but only under the conditions described in the
- GNU CC General Public License. A copy of this license is
- supposed to have been given to you along with GNU CC so you
- can know your rights and responsibilities. It should be in a
- file named COPYING. Among other things, the copyright notice
- and this notice must be preserved on all copies.
- */
-
- #include <stream.h>
- #include "<T>SLList.h"
-
- // error handling
-
-
- void default_<T>SLList_error_handler(char* msg)
- {
- cerr << "Fatal <T>SLList error. " << msg << "\n";
- exit(1);
- }
-
- one_arg_error_handler_t <T>SLList_error_handler = default_<T>SLList_error_handler;
-
- one_arg_error_handler_t set_<T>SLList_error_handler(one_arg_error_handler_t f)
- {
- one_arg_error_handler_t old = <T>SLList_error_handler;
- <T>SLList_error_handler = f;
- return old;
- }
-
- void <T>SLList::error(const char* msg)
- {
- (*<T>SLList_error_handler)(msg);
- }
-
- <T>SLList::<T>SLList(<T>SLList& a)
- {
- if (a.last == 0)
- last = 0;
- else
- {
- <T>SLListNode* p = a.last->tl;
- <T>SLListNode* h = new <T>SLListNode(p->hd);
- last = h;
- p = p->tl;
- for (;;)
- {
- <T>SLListNode* n = new <T>SLListNode(p->hd);
- last->tl = n;
- last = n;
- if (p == a.last)
- {
- last->tl = h;
- return;
- }
- else
- p = p->tl;
- }
- }
- }
-
- inline <T>SLList& <T>SLList::operator = (<T>SLList& a)
- {
- if (last == a.last)
- return *this;
- else
- {
- clear();
- if (a.last != 0)
- {
- <T>SLListNode* p = a.last->tl;
- <T>SLListNode* h = new <T>SLListNode(p->hd);
- last = h;
- p = p->tl;
- for (;;)
- {
- <T>SLListNode* n = new <T>SLListNode(p->hd);
- last->tl = n;
- last = n;
- if (p == a.last)
- {
- last->tl = h;
- return *this;
- }
- else
- p = p->tl;
- }
- }
- }
- }
- void <T>SLList::clear()
- {
- if (last == 0)
- return;
-
- <T>SLListNode* p = last->tl;
- last->tl = 0;
- last = 0;
-
- while (p != 0)
- {
- <T>SLListNode* nxt = p->tl;
- delete(p);
- p = nxt;
- }
- }
-
-
- void <T>SLList::prepend(<T&> item)
- {
- <T>SLListNode* t = new <T>SLListNode(item);
- if (last == 0)
- t->tl = last = t;
- else
- {
- t->tl = last->tl;
- last->tl = t;
- }
- }
-
- void <T>SLList::append(<T&> item)
- {
- <T>SLListNode* t = new <T>SLListNode(item);
- if (last == 0)
- t->tl = last = t;
- else
- {
- t->tl = last->tl;
- last = last->tl = t;
- }
- }
-
-
- void <T>SLListTrav::insert_after(<T&> item)
- {
- <T>SLListNode* t = new <T>SLListNode(item);
- if (L->last == 0)
- t->tl = L->last = current = t;
- else if (current == 0)
- {
- t->tl = L->last->tl;
- L->last = t;
- }
- else if (L->last == current)
- {
- t->tl = current->tl;
- current->tl = L->last = t;
- }
- else
- {
- t->tl = current->tl;
- current->tl = t;
- }
- }
-
-
- <T> <T>SLList::remove_front()
- {
- if (last == 0)
- error("remove_front of empty list");
- <T>SLListNode* t = last->tl;
- <T> res = t->hd;
- if (t == last)
- last = 0;
- else
- last->tl = t->tl;
- delete t;
- return res;
- }
-
- int <T>SLList::remove_front(<T>& x)
- {
- if (last == 0)
- return 0;
- else
- {
- <T>SLListNode* t = last->tl;
- x = t->hd;
- if (t == last)
- last = 0;
- else
- last->tl = t->tl;
- delete t;
- return 1;
- }
- }
-
- void <T>SLList::del_front()
- {
- if (last == 0)
- error("del_front of empty list");
- <T>SLListNode* t = last->tl;
- if (t == last)
- last = 0;
- else
- last->tl = t->tl;
- delete t;
- }
-
-