From: "chris.danx" <chris.danx@ntlworld.com>
Subject: Help: my list adt is broken
Date: Sun, 11 Feb 2001 21:04:58 -0000
Date: 2001-02-11T21:04:58+00:00 [thread overview]
Message-ID: <79Dh6.10732$zz4.264993@news2-win.server.ntlworld.com> (raw)
Hi,
i seem to have a problem with my (doubly) linked list ADT, and i can't
figure out what's wrong.
here is the output of the test program
> -- display list before it get a choppin'
> display a list of 5 elements
> 5 4 3 2 1
> -- remove head
> display a list of 4 elements
> 4 3 2 1
> -- remove an arbitary element
> display a list of 3 elements
> 4 2 1
> -- remove tail
> display a list of 2 elements
> 4 2 1
> -- remove head again
> display a list of 1 elements
> 2 1
> -- remove last element
> no elements
i think the problem occurs when removing the tail. it doesn't seem to be
erased but it should be. i know
it's removed one from the size variable, so the code should be executed in
full.
i think the errror lies in the following routine, but i can't see it.
-- remove item at current position from list;
-- position will no longer be valid after execution;
--
-- will raise
-- 1. empty_list_error if list empty;
-- 2. position_error if position invalid;
--
procedure remove (l : in out list;
p : in out list_position) is
t : list_position;
begin
if is_empty(l) then
raise empty_list_error;
end if;
if is_invalid(p) then
raise position_error;
end if;
if head(l) = p then
l.head := p.next;
l.size := l.size - 1;
delete_node(p);
elsif tail(l) = p then
l.tail := p.prev;
l.size := l.size - 1;
delete_node(p);
else
t := p;
-- dereference p;
t.prev.next := p.next;
t.next.prev := p.prev;
-- delete p;
delete_node(p);
l.size := l.size - 1;
end if;
end remove;
i've also included the delete_node routine
-- delete a node and give its' space back to pool;
--
procedure delete_node (n : in out list_position) is
procedure free
is new ada.unchecked_deallocation
(node, list_position);
begin
free (n);
end delete_node;
if it's any help.
list_position is simply a pointer to a node which contains
1. a pointer to the next and previous nodes
2. the data
oh, i almost forgot, the list type maintains a pointer to the head and tail,
as well as the size of the list.
i think i've been staring at it too long!!! i'll let someone else takeover.
Thanks in advance,
Chris Campbell
next reply other threads:[~2001-02-11 21:04 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-02-11 21:04 chris.danx [this message]
2001-02-11 23:01 ` Help: my list adt is broken Larry Hazel
2001-02-12 0:29 ` tmoran
2001-02-12 0:29 ` Chad R. Meiners
2001-02-12 12:17 ` chris.danx
2001-02-12 12:50 ` chris.danx
2001-02-12 14:23 ` John English
2001-02-12 15:10 ` chris.danx
2001-02-12 17:33 ` chris.danx
2001-02-12 20:00 ` tmoran
-- strict thread matches above, loose matches on Subject: below --
2001-02-11 21:05 chris.danx
2001-02-11 21:08 ` chris.danx
2001-02-12 14:55 ` Ted Dennison
2001-02-12 21:14 schwarza
2001-02-12 23:09 ` chris.danx
2001-02-13 1:42 ` Jeffrey Carter
2001-02-13 15:35 schwarza
2001-02-14 11:47 ` John English
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox