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.
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.
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
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
> 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