Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simple way of doing a linked list in tcl?

551 views
Skip to first unread message

Wayne Paul SCHULLER

unread,
Jan 3, 1995, 12:52:04 AM1/3/95
to
hi,

I really need a linked list data structure.

I want to store a dynamic number of window names in a list, and for each
name there are some other attributes to go with.

I think a linked list would be best as I need to delete elements anywhere
in the list and I need to be able to traverse the list easily.

I'm thinking it can be done with arrays -- but how? is there a simple
concise method?

please help,
wayne.

Ioi Kim Lam

unread,
Jan 3, 1995, 1:07:25 AM1/3/95
to
Wayne Paul SCHULLER (schu...@mundil.cs.mu.OZ.AU) wrote:
: hi,

set list(head) bar
set list(bar,next) foo
set list(bar,data) bardata
set list(foo,next) {}
set list(foo,data) foodata

So you can perform dynamic insert/delete link in C. Of course it is
very slow (compared to C).

Ioi.

Brian Cooper

unread,
Jan 3, 1995, 2:08:49 AM1/3/95
to
Ioi Kim Lam (i...@blue.seas.upenn.edu) wrote:

It seems like this should be doable with the list operations -- and
maybe a little simpler, or faster?

set mylist ""
lappend mylist [list $bardata]
lappend mylist [list $foodata]
lappend mylist [list $bazdata1 $bazdata2 $bazdata3]

You can traverse, sort, insert, and delete elements from the list with
the built-in list functions. I would expect this to be faster
than the array solution, but YMMV.

Brian Cooper

Michael Salmon

unread,
Jan 3, 1995, 3:19:33 AM1/3/95
to
In article <950031...@mulga.cs.mu.OZ.AU>

I would probably do something like this:

set windowList {{.w1 att1} {.w2 att2} etc ad nauseum}

It isn't that easy to remove though not that hard either and adddition
is extremely easy. If you have a very dynamic list then just use an
array:

set windowAttr(".w1") att1
set windowAttr(".w2") att2
etc ad nauseum

The linking is achieved by using the array commands, you don't actually
need a linked list if all you want to do is visit every element.

--

Michael Salmon

#include <standard.disclaimer>
#include <witty.saying>
#include <fancy.pseudo.graphics>

Ericsson Telecom AB
Stockholm

Adam M. Costello

unread,
Jan 3, 1995, 6:50:42 PM1/3/95
to
In article <950031...@mulga.cs.mu.OZ.AU>, Wayne Paul SCHULLER
<schu...@mundil.cs.mu.OZ.AU> wrote:

> I really need a linked list data structure.
>
> I want to store a dynamic number of window names in a list, and for
> each name there are some other attributes to go with.
>
> I think a linked list would be best as I need to delete elements
> anywhere in the list and I need to be able to traverse the list
> easily.

While you could implement a linked list, I'm not sure that's what you
really want. How about this:

Let winfo be an array whose indices are window names, and whose values
are unique global array names. Those arrays take field names as
indices.

So if .foo.bar is a window name, you can do

upvar 0 wattr $winfo(.foo.bar)

and wattr becomes an alias for the array containing the attributes of
.foo.bar. You can then refer to $wattr(width) and $wattr(height), or
whatever. You can delete a particular attribute, like this:

unset wattr(width)

Or you can delete all info concerning .foo.bar, like this:

unset wattr
unset $winfo(.foo.bar)

There still remains the question of how to add a window. You need a
procedure which always returns a new global unique variable name. Then
you can create a new window entry like this:

set winfo(.foo.goo) [new]

Here's what I use for the procedure new:

set ## 0
proc new {} {
global ##
incr ##
return #${##}
}

It will not interfere with other global variables, as long as those
others don't begin with a pound sign.

(If your program is so long-lived that ## might wrap around, you can put
in a while loop that keeps incrementing ## until #${##} does not exist.)

AMC

0 new messages