linked_20lists_20using_20structures
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
linked_20lists_20using_20structures [2018/03/31 13:19] – external edit 127.0.0.1 | linked_20lists_20using_20structures [2024/01/05 00:22] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
=====Linked lists using structures===== | =====Linked lists using structures===== | ||
- | //by Richard Russell, May 2006//\\ \\ Structures provide a convenient means to create and manipulate linked lists, because they allow the content of each //node// in the list to be clearly described. | + | //by Richard Russell, May 2006//\\ \\ Structures provide a convenient means to create and manipulate linked lists, because they allow the content of each //node// in the list to be clearly described.\\ \\ I will illustrate the steps required to create, search and read a linked list of structures using the example of an //insertion sort//. Here, the data items that you wish to sort are added one at a time to a list, in the correct place to keep the list sorted. A linked list is particularly suited to this because you can insert a new item anywhere in the list just by manipulating links, without having to move any of the existing items around.\\ \\ We start by deciding the structure of each node in the list. In this example each node will contain a string **item$** and an associated numeric value **misc**. Because it is a linked list each node must also contain a link **link%** which points to the next item in the list (the last item in the list has a link value of zero). Hence we can define a suitable structure for each node in the list as follows:\\ \\ |
- | DIM node{item$, misc, link%} | + | <code bb4w> |
+ | DIM node{item$, misc, link%%} | ||
+ | </ | ||
The structure could contain any number of additional members, so long as there is always a link member.\\ \\ We keep track of the start of the linked list by defining a variable **first%** which points to the first node in the list. Initially the list is empty, so we set this variable to zero:\\ \\ | The structure could contain any number of additional members, so long as there is always a link member.\\ \\ We keep track of the start of the linked list by defining a variable **first%** which points to the first node in the list. Initially the list is empty, so we set this variable to zero:\\ \\ | ||
- | | + | <code bb4w> |
+ | | ||
+ | </ | ||
Next we start the insertion sort proper. For convenience we will read each data item in turn from a " | Next we start the insertion sort proper. For convenience we will read each data item in turn from a " | ||
+ | <code bb4w> | ||
DATA The, quick, brown, fox, jumps, over, the, lazy, dog. | DATA The, quick, brown, fox, jumps, over, the, lazy, dog. | ||
FOR n% = 1 TO 9 | FOR n% = 1 TO 9 | ||
READ word$ | READ word$ | ||
+ | </ | ||
For each word we need to find where in the list it needs to be inserted to keep the list sorted into an ascending alphabetical sequence. This we do by following the links from the beginning until we either get to the end of the list, or we get to an entry which is //later// in the alphabet than our word:\\ \\ | For each word we need to find where in the list it needs to be inserted to keep the list sorted into an ascending alphabetical sequence. This we do by following the links from the beginning until we either get to the end of the list, or we get to an entry which is //later// in the alphabet than our word:\\ \\ | ||
- | | + | <code bb4w> |
- | this% = first% | + | |
- | WHILE (this% <> 0) AND (word$ > FNgetitem(node{}, | + | this%% = first%% |
- | prev% = this% | + | WHILE (this%% <> 0) AND (word$ > FNgetitem(node{}, |
- | this% = node.link% | + | prev%% = this%% |
+ | this%% = node.link%% | ||
ENDWHILE | ENDWHILE | ||
+ | </ | ||
A little explanation is probably necessary at this point. We define two new variables **this%** and **prev%** to point to the node in which we are currently interested, and the // | A little explanation is probably necessary at this point. We define two new variables **this%** and **prev%** to point to the node in which we are currently interested, and the // | ||
- | | + | <code bb4w> |
+ | | ||
node.item$ = word$ | node.item$ = word$ | ||
node.misc = n% | node.misc = n% | ||
+ | </ | ||
The function **FNnewnode** creates a new node and returns a pointer to it. It also sets the structure **node{}** to correspond to this node for the convenience of setting its members. For the purposes of the example we set the **misc** element to the value of the loop counter **n%**.\\ \\ We must now link this new node into the correct place in the list:\\ \\ | The function **FNnewnode** creates a new node and returns a pointer to it. It also sets the structure **node{}** to correspond to this node for the convenience of setting its members. For the purposes of the example we set the **misc** element to the value of the loop counter **n%**.\\ \\ We must now link this new node into the correct place in the list:\\ \\ | ||
- | | + | <code bb4w> |
- | IF prev% THEN | + | |
- | PROCsetlink(node{}, | + | IF prev%% THEN |
+ | PROCsetlink(node{}, | ||
ELSE | ELSE | ||
- | first% = new% | + | first%% = new%% |
ENDIF | ENDIF | ||
NEXT n% | NEXT n% | ||
+ | </ | ||
Here the procedure **PROCsetlink** sets the link member of node **prev%** to point to node **new%**. If we need to insert the new node at the start of the list (hence **prev%** is zero) we simply set **first%** to point to the new node.\\ \\ Now the insertion sort is complete, and all we need to do is print out the contents of the list in sequence to see if they are correct:\\ \\ | Here the procedure **PROCsetlink** sets the link member of node **prev%** to point to node **new%**. If we need to insert the new node at the start of the list (hence **prev%** is zero) we simply set **first%** to point to the new node.\\ \\ Now the insertion sort is complete, and all we need to do is print out the contents of the list in sequence to see if they are correct:\\ \\ | ||
- | | + | <code bb4w> |
- | WHILE this% <> 0 | + | |
- | word$ = FNgetitem(node{}, | + | WHILE this%% <> 0 |
+ | word$ = FNgetitem(node{}, | ||
misc = node.misc | misc = node.misc | ||
PRINT word$, misc | PRINT word$, misc | ||
- | this% = node.link% | + | this%% = node.link%% |
ENDWHILE | ENDWHILE | ||
END | END | ||
- | If all is well this should print:\\ \\ "1 The\\ 3 brown\\ 9 dog.\\ 4 fox\\ 5 jumps\\ 8 lazy\\ 6 over\\ 2 quick\\ 7 the"\\ \\ All that remains is to list the procedures and functions, which contain the ' | + | </ |
+ | If all is well this should print:\\ \\ | ||
+ | < | ||
+ | | ||
+ | 3 brown | ||
+ | 9 dog. | ||
+ | 4 fox | ||
+ | 5 jumps | ||
+ | 8 lazy | ||
+ | 6 over | ||
+ | 2 quick | ||
+ | 7 the | ||
+ | </ | ||
+ | <code bb4w> | ||
DEF FNnewnode(RETURN n{}) | DEF FNnewnode(RETURN n{}) | ||
- | LOCAL P% | + | LOCAL p%% |
- | DIM P% DIM(n{})-1 | + | DIM p%% DIM(n{})-1 |
- | | + | |
- | = P% | + | = p%% |
- | DEF FNgetitem(RETURN n{}, n%) | + | DEF FNgetitem(RETURN n{}, n%%) |
- | IF n% = 0 THEN = "" | + | IF n%% = 0 THEN = "" |
- | | + | |
= n.item$ | = n.item$ | ||
- | DEF PROCsetlink(n{}, | + | DEF PROCsetlink(n{}, |
- | | + | |
- | n.link% = l% | + | n.link%% = l%% |
ENDPROC | ENDPROC | ||
+ | </ |
linked_20lists_20using_20structures.1522502366.txt.gz · Last modified: 2024/01/05 00:17 (external edit)