CS1003: Introduction to Computer Programming in C

yakconspiracyΛογισμικό & κατασκευή λογ/κού

14 Δεκ 2013 (πριν από 3 χρόνια και 10 μήνες)

99 εμφανίσεις

CS1003: Introduction to Computer Programming in C

Summer 2000

Lecturer: Carl Sable

Topic #17: Linked Lists


The next topic we will discuss is
linked lists
. Implementations of linked lists rely on
structures, pointers, and dynamic memory allocation combine
d.


When we first discussed arrays, we examined a program which allows the user to enter
10 numbers and prints them out in reverse order. When we first discussed dynamic
memory allocation using "malloc", we examined a program which allows the user to
spec
ify how many numbers he or she wants to enter, then to enter the numbers, and then
the program would print them out in reverse order. With arrays, the programmer had to
know in advance how many numbers would be entered. Using "malloc" and dynamic
memory
allocation, the user had to know in advance how many numbers would be
entered.


Many applications such as database systems, spreadsheets, and word processors allow
users to continuously enter new data as long as there is memory left in the computer. The
c
omputer can not just allocate enough memory for a document of maximum size, because
this would be extremely wasteful and possibly not leave enough memory for other things
running on the computer. What needs to be done is that the computer must allocate
me
mory for a small amount of data at a time, and when it runs out, it must allocate more.
One aspect of this which makes things confusing is that the allocated memory will not
likely be contiguous; in other words, you will be storing data in various locatio
ns of
physical memory.


Let's look at a simplified example. Let's say you wish to write a program which allows
the user to type in integers one at a time until he or she signals EOF (on most Unix
systems, this is signaled by pressing Ctrl
-
D). You don't k
now in advance how many
integers will be entered, and neither do they. When EOF is encountered, the program
should print out all integers entered in reverse order.


The general idea is this: Define a structure with two fields. One field will contain an
integer that the user enters, and the other field will contain a pointer to a structure of the
same type (the one that contained the previously entered integer). Each time the user
enters an integer, allocate enough memory to hold one structure. Fill in
the integer, and
fill in the pointer to point to the previous structure. If this is the first structure, its pointer
should be a NULL pointer. After all integers have been entered, start at one structure,
print out the integer stored there, and then walk

to the next structure and do the same, etc.
These pointers from one structure to another are referred to as links, and the set of all
structures together is referred to as a linked list. The beginning of a linked list is a
pointer to the first structure
.


We will define the following structure to use for this program:


typedef struct node

{


int x;


struct node *next;

} NODE;


typedef NODE *PNODE;


It is common to refer to the individual structures in a linked list as nodes of the linked
list. Above, we

are saying that each "NODE" will contain an integer (the field "x") and a
pointer to the next node. Often, programmers choose to call the field containing the
pointer link to the next structure either "next" or "link". We also define "PNODE" to be a
poi
nter to a node.


The fact that we actually want to display the integers entered in reverse order will
actually make things a little easier for us! We can just insert each new structure at the
start of the list so far.


Here is the code:


#include <stdio.h
>


typedef struct node

{


int x;


struct node *next;

} NODE;


typedef NODE *PNODE;


int main(void)

{


int x;


PNODE pList = NULL, pTemp;



printf("Enter numbers one at a time, Ctrl
-
D to stop.
\
n");


while(1)


{

if (scanf("%d", &x) == EOF)


break;


pTemp = (
PNODE) malloc (sizeof(NODE));

if (pTemp == NULL)

{


printf("Out of memory, could not store number!
\
n");

}

else

{


pTemp
-
>x = x;


pTemp
-
>next = pList;


pList = pTemp;

}

}


for (pTemp = pList; pTemp != NULL; pTemp = pTemp
-
>next)

{


printf("%d
\
n", pTemp
-
>x);

}


return 0;

}


Note that we need to declare two "PNODE" variables. One is "pList", which should
always point to the beginning of the linked list. The other is "pTemp", which we use to
allocate new nodes that we want to set up and also to walk along the
final list. At the
beginning of the program, "pList" is set to NULL, so we are dealing with an empty list.
If the user hits Ctrl
-
D right away, it will stay empty, we will exit the "while" loop, and
the "for" loop will be skipped because the condition it
depends on is false right away.


Once in the "while" loop, we allow the user to enter integers. Each time an integer is
entered, we need to set up a new node structure in which to store it. To do this, we use
"malloc". The "malloc" function, as always,
takes as a parameter the number of bytes we
need allocated. In this case, we need enough bytes to store one "NODE" structure, which
is "sizeof(NODE)". We also could have said "sizeof(struct node)", the two are equivalent
because of the type definition.
If the call to "malloc" returns NULL, we inform the user
that no memory was available. We might want to exit the loop at this time with a "break"
statement, but as implemented here, the program will loop back and ask for another
integer. Perhaps memory c
urrently used by some other application will be freed, and
we'll be able to allocate space for a node in the future. Assuming we successfully
allocate memory for a node, we fill in its "x" field with the integer entered by the user.
We then have this nod
e point to the beginning of the list so far, and update the start of the
list to point to the newest node. (If this is the first node entered, the previous value of
"pList" will be NULL, so the current node's "next" link will take the value of NULL, and
"
pList" will be updated to point to the current node.)


After Ctrl
-
D is entered (or an EOF is encountered if reading from a file), we exit the
"while" loop. The "for" loop then starts "pTemp" pointing to the first node of the linked
list. For each iterati
on of the "for" loop, we display the integer stored in the current node
to standard output, and after each iteration we update "pTemp" to point to the next node
by setting it equal to the current node's "next" field. After printing out the last integer in

the list, "pTemp" will take the value of NULL (the value of the "next" field in the last
node of the linked list, which was the first one created), and the "for" loop will end.


There's one thing that is a little bit messy about this program, namely that
we are never
freeing the memory which we allocate. It's OK because the memory will be freed
automatically when the program ends, which happens right after the "for" loop to print
out the values, but to be clean and maintain good habits, it might have been

better to
include code which explicitly frees the memory. This could have been done with a
"while" loop right before the final "return" statement as follows:



while (pList != NULL)


{



pTemp = pList
-
>next;



free(pList);



pList = pTemp;


}


So, for e
ach iteration of the loop, we use "pTemp" to store the address of the next node in
the list, then we free the node that "pList" points to and update "pList" to point to the next
node.


It is actually a bit more complicated to write a similar program that a
llows the user to
enter integers one at a time until EOF is triggered and then prints them out in the same
order that they were entered. In this case, each new node that gets created has to be
inserted at the end of the list so far. One way to do this is

to traverse the entire list every
time a new node is created, and when the end of the list is found, insert the new node
after it. A more efficient method is to keep track of the end of the list as you go along.
You also need to keep track of the beginn
ing of the list so that you can start a traversal of
the list there at the end when you want to print out the integers. Here is the code:


#include <stdio.h>


typedef struct node

{


int x;


struct node *next;

} NODE;


typedef NODE *PNODE;


int main(void)

{


int x;


PNODE pList = NULL, pEnd = NULL, pTemp;



printf("Enter numbers one at a time, Ctrl
-
D to stop.
\
n");


while(1)


{

if (scanf("%d", &x) == EOF)


break;


pTemp = (PNODE) malloc (sizeof(NODE));

if (pTemp == NULL)

{


printf("Out of memory, could not s
tore number!
\
n");

}

else

{


pTemp
-
>x = x;


pTemp
-
>next = NULL;


if (pEnd == NULL)


{



pList = pTemp;



pEnd = pTemp;


}


else


{



pEnd
-
>next = pTemp;


pEnd = pTemp;

}

}

}


for (pTemp = pList; pTemp != NULL; pTemp = pTemp
-
>next)

{


printf("%d
\
n", pTemp
-
>x
);

}


return 0;

}


Now, after each new node is created, the integer field of the node is filled in with the
value that the user entered, and since this will become, at least temporarily, the last node
of the linked list, its "next" field is set equal to NU
LL. Then the code checks to see if
"pEnd" is currently equal to NULL. If so, this is the first node we are adding to the
linked list (and, for now, it will also be the last node of the linked list), so both "pList"
and "pEnd" point to the new node. Othe
rwise, the list has already been started, and
"pList" points to the beginning of it so that pointer does not have to be updated. Instead,
the previous node (the node pointed to by "pEnd") gets its "next" field updated to point to
"pTemp", and then "pEnd"
gets updated to point directly to "pTemp" (i.e. the last node).


Now we will look at a program which once again allows the user to enter one integer at a
time until EOF is indicated, but this time, at the end, we want to print out the final list in
sorted
order. Rather than create the entire linked list and then sort it, we will keep it
sorted as we go along. Each time a new node is added, we will find its correct position in
the current list and insert it there. Thus we are using a sorting method simila
r to insertion
sort. If the new node gets inserted at the very beginning of the list (because it has the
smallest value so far), the pointer to the head of the list needs to also be updated.


The program is getting larger now, so rather than put everythin
g in "main", we will write
separate routines to create a node and to insert a node into the list so far. When calling
the routine to insert a node, we need to pass it a pointer to the beginning of the list (so it
can traverse the list and find the correct

position for the new node) and the new node to
be inserted. We also must keep in mind that the pointer to the beginning of the list may
need to be changed, but remember that when passing a pointer to a function, the function
can permanently change the va
lue that the pointer points to, but it can not permanently
change where the pointer points. There are at least three ways to deal with this problem.
One would be to pass the function the memory address of the pointer (hence, a pointer to
a pointer). A s
econd would be to make sure that the linked list starts with a dummy node
which always remains as the first node of the list and doesn't store any significant data.
We will take a third approach, which is to have the called function return a pointer to th
e
beginning of the list after the insert takes place, and the calling function sets its pointer to
the beginning of the list to this return value. If the new node gets inserted at the
beginning of the list, the called function returns a pointer to this ne
w node, and the calling
function updates its pointer to the beginning of the list; Otherwise, the called function
returns the same pointer it was passed, and the calling function sets its pointer to the
beginning of the list to itself, which has no negativ
e effect.


#include <stdio.h>


typedef struct node

{


int x;


struct node *next;

} NODE;


typedef NODE *PNODE;


PNODE create_node(int);

PNODE insert_node(PNODE, PNODE);


int main(void)

{


int x;


PNODE pList = NULL, pTemp;



printf("Enter numbers one at a
time, Ctrl
-
D to stop.
\
n");


while(1)


{

if (scanf("%d", &x) == EOF)


break;




pTemp = create_node(x);



if (pTemp != NULL)



{




pList = insert_node(pList, pTemp);



}


}


for (pTemp = pList; pTemp != NULL; pTemp = pTemp
-
>next)

{


printf("%d
\
n", pTemp
-
>x
);

}



return 0;

}


/* Creates a node for given data and return pointer to it. */

PNODE create_node(int x)

{


PNODE pTemp;


pTemp = (PNODE) malloc (sizeof(NODE));

if (pTemp == NULL)

{



printf("Out of memory, could not store number!
\
n");

}

else

{



pTemp
-
>
x = x;


pTemp
-
>next = NULL;

}



return pTemp;

}


/* Inserts node pNew into pList, keeping list in sorted order. */

PNODE insert_node(PNODE pList, PNODE pNew)

{


PNODE pPrev, pCur;



/* Inserting pNew into an empty list, it just becomes list. */


if (pList
== NULL)



return pNew;



/* If pNew
-
>x is less than pList
-
>x, pNew becomes start of the list. */


if (pNew
-
>x < pList
-
>x)


{



pNew
-
>next = pList;



pList = pNew;



return pList;


}



/* Otherwise, find its position and put it there. */


pPrev = pList;

pC
ur = pList
-
>next;

while ((pCur != NULL) && (pCur
-
>x < pNew
-
>x))

{

pPrev = pCur;

pCur = pCur
-
>next;

}


pNew
-
>next = pCur;


pPrev
-
>next = pNew;


return pList;

}


So "main" is now pretty simple. It continuously allows the user to enter integers until
EOF is
signaled. For each integer entered, a call to "create_node" is performed to create
the node storing this integer, and a call to "insert_node" is called to add this node to its
correct position in the linked list.


The "create_node" routine is a simple rou
tine which tries to allocate memory for a new
node. If it succeeds, it sets the fields of the node and returns a pointer to it. Otherwise, it
informs the user that memory was not available and returns NULL.


The "insert_node" routine is a little more com
plicated. It is passed a pointer to the
beginning of a linked list and a pointer to a node to insert in that list. It assumes the list is
already in sorted order, and it wants to keep the list in sorted order.


To simplify things, two special cases are h
andled at the beginning. If "pList" is NULL,
then there was no previous list, so "pNew" becomes the list, and this pointer is returned.
If "pList" exists and the data in "pNew" is less than the data in "pList" (the first node of
the linked list), "pNew"
is updated so that its "next" field points to "pList", and then
"pList" is updated to point to "pNew" (the new beginning of the list), after which "pList"
is returned. (We could have just returned "pNew" after updating its "next" field.)


Otherwise, we ha
ve to search the list for the position in which to insert "pNew" into the
list. We make use of two other pointers to nodes, namely "pPrev" and "pCur". We start
off with "pPrev" pointing to the first node (the node pointed to by "pList") and "pCur"
pointi
ng to the second node. We keep updating these two pointers (by setting "pPrev" to
"pCur" and then "pCur" to "pCur
-
>next") until "pCur" becomes NULL or the data in
"pCur" is greater than or equal to the data in "pNew". At this point, we know we want to
in
sert the new node "pNew" in between "pPrev" and "pCur", so we do this with the last
two assignments and return "pList" (still pointing to the beginning of the list).


The order of the two conditions of the "while" statement is actually very important.
Whe
n evaluating a boolean expression consisting of a series of clauses separated by
"&&" statements, they are evaluated from left to right, and as soon as one is false, the
others are not evaluated at all. (There is no need to evaluate the rest, since we alr
eady
know that they are not all true.) So if "pCur" is NULL, then the first clause is false, and
we don't evaluate the expression "(pCur
-
>x < pNew
-
>x)". This is a good thing, because
if we tried to evaluate this expression while pCur was NULL, it would c
ause a crash! If
you try to use indirection on any NULL pointer, including using the selection operator on
a NULL pointer to a structure, this is an example of referencing through a NULL pointer
which causes a crash.


We are now going to look at a routine

which accepts a pointer to a linked list and
searches for a particular item of data. If it finds it, it returns a pointer to the first node
that contains this data; otherwise, it returns NULL. The routine assumes that PNODE is
a pointer to a structure a
s defined in the previous programs.


/* Search for a node with given data in a linked list.


* Return a pointer to the first such node, or NULL if no such node exists. */

PNODE search_list(PNODE list, int x)

{


PNODE walker;



for (walker = list; walker !=

NULL; walker = walker
-
>next)



if (walker
-
>x == x)




break;



return walker;

}


Note that at the end of the "for" loop, walker must either point to a node whose numeric
field contains the value "x", or it must be NULL (those are the only two ways the loo
p
would end). Furthermore, if there is a node with the value "x" anywhere in the list, it will
be found before we reach the end of the list.


This might seem somewhat useless, but similar search routines can be very useful when
dealing with linked lists w
hose nodes are more complex structures containing multiple
fields. For example, let's say that each node is a structure containing fields that store a
person's name, telephone number, and address. You might want to search the linked list
for a structure
containing a given name, and when the node is returned, if it isn't NULL
(meaning such a node was found), print out the telephone number and address. Although
all the routines we have looked at so far assume nodes with the specific structure defined
above
, there is nothing in the algorithms we are using that require this, and it should be
very simple to tweak these routines to work with nodes with different structure.


Let's say you are keeping a linked list of items and their corresponding prices. A node

might then be defined as follows:


typedef struct node

{


char item[100];


int price;


struct node *next;

} NODE;


typedef NODE *PNODE;


The "item" field of each node would be a string which gives the name or description of an

item, and "price" might be t
he price of the item in dollars. Let's say you decide that you
can't consider purchasing any item over 1000 dollars and want to delete the nodes
corresponding to all such items from your list. Here is a routine to do that:


/* Deletes all nodes from pLis
t whose price is greater than cutoff.


* Returns a pointer to the beginning of the new list. */

PNODE delete_nodes(PNODE pList, int cutoff)

{


PNODE pPrev, pCur;



pPrev = NULL;


pCur = pList;


while(pCur != NULL)


{



if (pCur
-
>price > cutoff)



{




/* D
eleting first node is special case. */




if (pPrev == NULL)




{





pList = pList
-
>next;





free(pCur);





pCur = pList;




}




else




{





pPrev
-
>next = pCur
-
>next;





free(pCur);





pCur = pPrev
-
>next;




}



}



else



{




pPrev = pCur;




pCu
r = pCur
-
>next;



}


}



return pList;

}


So "pCur" walks through the linked list, and "pPrev" should always refer to the node
previous to "pCur", unless "pCur" refers to the first node in which case "pPrev" is NULL.
For each node, we compare its price to

the cutoff. If the price is too high, we want to
delete the node from the linked list. If "pPrev" is NULL, we are deleting the first node.
We therefore have "pList" point to the second node of the list, free the memory
associated with the first node, a
nd update "pCur" to point to "pList" (the new start of the
list). The next iteration of the loop will check this next node, which is now the first node
of the current version of the list. If "pPrev" is not NULL, we delete the node associated
with "pCur"
by ensuring that the previous node points to the next node, then freeing the
memory of the current node and updating "pCur" to point to the next node. If the price of
the current node is not too high, we simple move the two pointers forwards by moving
"pP
rev" to "pCur" and pushing "pCur" forwards one node. Eventually, "pCur" will walk
off the end of the list and become NULL, and the loop will end. At this point, "pList"
will point to the beginning of the (possibly) shortened list, and we return it.


To c
all the routine, we need to remember to assign the return value to the pointer that
points to the beginning of the list. In other words, a call might look like this:


pList = delete_nodes(pList, 1000);


If we don't assign the return value back to the orig
inal list and we wind up deleting the
first node of the list, "pList" will wind up pointing to memory that has been freed. We
will likely crash soon, and if nothing else, will not notice that this first node has been
deleted.


So far, all examples using
l
inked lists

kept the contents of nodes unchanged once they
were added to the list. This is not always the case. Let us consider an alternative way to
handle the sorting problem. Last time, each time an integer was entered, a node for that
integer was cr
eated, and that node was added to a linked list of integers, maintained in
sorted order. If a specific integer was entered multiple times, a separate node was created
for each instance of the integer, and these nodes would appear next to each other in the

sorted list. Now, instead, there will be only one node for each distinct integer, and in
addition to the integer, a separate field will contain a count of how many times that
integer was added. The structure of each node might therefore be defined as fo
llows:


typedef struct node

{


int x;


int count;


struct node *next;

} NODE;


typedef NODE *PNODE;


Here is the entire program which uses this node structure and allows the user to enter
integers until EOF is signaled, at which time the program will list
all of the integers in
sorted order:


#include <stdio.h>


typedef struct node

{


int x;


int count;


struct node *next;

} NODE;


typedef NODE *PNODE;


PNODE create_node(int);

PNODE insert_integer(PNODE, int);


int main(void)

{


int x;


PNODE pList = NULL,
pTemp;



printf("Enter numbers one at a time, Ctrl
-
D to stop.
\
n");


while(1)


{

if (scanf("%d", &x) == EOF)


break;




pList = insert_integer(pList, x);


}


for (pTemp = pList; pTemp != NULL; pTemp = pTemp
-
>next)

{


for (x = 0; x < pTemp
-
>count; x++)


prin
tf("%d
\
n", pTemp
-
>x);

}



return 0;

}


/* Creates a node for given data and return pointer to it. */

PNODE create_node(int x)

{


PNODE pTemp;


pTemp = (PNODE) malloc (sizeof(NODE));

if (pTemp == NULL)

{



printf("Out of memory, could not store number!
\
n");

}

else

{



pTemp
-
>x = x;


pTemp
-
>count = 1;


pTemp
-
>next = NULL;

}



return pTemp;

}


/* Increase count of integer x or insert new node in sorted list. */

PNODE insert_integer(PNODE pList, int x)

{


PNODE pPrev, pCur, pNew;



/* Check if we are starting n
ew list. */


if (pList == NULL)


{



pNew = create_node(x);



return pNew;

}



/* Otherwise, find its position and put it there. */


pPrev = NULL;

pCur = pList;

while ((pCur != NULL) && (pCur
-
>x < x))

{

pPrev = pCur;

pCur = pCur
-
>next;

}


if ((pCur != NULL
) && (pCur
-
>x == x))

{


/* Integer has occurred before, increase count. */


pCur
-
>count = pCur
-
>count + 1;

}

else if (pPrev == NULL)

{


/* New integer is first in list up to this point. */


pNew = create_node(x);


pNew
-
>next = pList;


pList = pNew;

}


else

{


/* New integer goes between pPrev and pCur. */


pNew = create_node(x);

pNew
-
>next = pCur;



pPrev
-
>next = pNew;


}



return pList;

}


If you compare this program to the one from last time, you will find it very similar. We
no longer call "create_node"

from "main", because not all integers entered require a new
node to be created. We've changed the insert function from "insert_node", which takes a
node to be inserted, to "insert_integer", which takes an integer to be inserted. If the
integer was alrea
dy in the list, we just increase the count of its node; otherwise,
"insert_integer" will call "create_node" to create the new node. The "create_node"
routine is almost identical to the one from last time, but it also initializes the "count" field
of the n
ode to 1 (since this is the first time the integer was seen, or we wouldn't be
creating a new node for it).


The "insert_integer" function first checks to see if we are starting with an empty list. If
so, a new node (with a count of 1) is created for the
passed integer "x", and this node
becomes the new list. Otherwise, a search of the list is executed which finds the first
node in the list whose data is an integer greater than or equal to "x". If the data in the
node is "x", the count of this node is in
creased. Otherwise, if "pPrev" is NULL, a new
node (with a count of 1) is created for "x", and this node becomes the start of the list so
far. Otherwise, a new node (with a count of 1) is created for "x", and this node is inserted
between the previous no
de and the current node.


Back in "main", when displaying the list in sorted order, for each node, we loop from 0
through "count
-
1" to display the integer represented by the given node "count" times,
since that is how many times it was entered by the user.


Deleting an integer from a list with this type of structure also becomes a bit more
complex. In order to delete an instance of an integer, we must first search the list for the
node whose data is this integer. Assuming it exists, if its count is greate
r than one, we
simply decrease the count by one, while if its count is exactly one, we actually delete the
node by updating the link of the previous node and freeing the memory associated with
the current node. We won't examine the actual code.


Now, we wi
ll examining two ways to reverse a linked list. The data stored in each node
doesn't matter, and we are assuming that the link from each node to the next is called
"next". The first function we will look at is an iterative (non
-
recursive) solution. Here

is
a function that takes a linked list as a parameter, reverses it, and returns a pointer to the
beginning of the reversed list:


PNODE reverse_list(PNODE pList)

{


PNODE pPrev, pCur, pNext;



if (pList == NULL)



return NULL;



pPrev = NULL;


pCur = pLis
t;


do


{



pNext = pCur
-
>next;



pCur
-
>next = pPrev;



pPrev = pCur;



pCur = pNext;

} while (pCur != NULL);


return pPrev;

}


The special case where the passed in list is empty (NULL) is handled first; the reverse of
a NULL list is just a NULL list. Oth
erwise, we need three pointers, which keep track of
the current node (the one which needs its "next" field to be updated), the previous node
(the one to which the current node should point), and the next one (to become the current
after the current node ge
ts its "next" field updated). We start with the current node being
the first one and the previous node being NULL (since the first node becomes the last and
points nowhere). For each iteration of the "do…while" loop, we update the "next" field
of the cur
rent node and then update the pointers to point to the new nodes. When we
reach the end of the list, we stop; at this point, "pPrev" (which stores the previous value
of "pCur") points to what used to be the final node and is now the first, so we return it
.


Here now is a recursive solution to the same problem:


PNODE reverse_list(PNODE pList)

{


PNODE pTemp;



if (pList == NULL)



return NULL;



if (pList
-
>next == NULL)



return pList;



pTemp = reverse_list(pList
-
>next);


pList
-
>next
-
>next = pList;


pList
-
>next = NULL;


return pTemp;

}


We are basically saying the following: In order to reverse the entire list, reverse all of it
other than the first node and then plug the first node on to the end. The function has two
base cases; if the list is empty, i
t stays empty, and if the list is a single node, it remains
the same. (These two base cases could be combined into one, returning "pList" if either
condition is true.) Otherwise, we do the general case. We recursively call "reverse_list"
passing it the
list starting with the second node (considering the current node to be the
first). Since we have not changed the "next" field of the current node, and it used to point
to the first node of the remaining list, it will now point to the last node of the rema
ining
list (which has already been reversed once the recursive call to "reverse_list" returns).
We update that node (the one pointed to by the current node) to point to the current node,
and we update the current node to point nowhere (its "next" field be
comes NULL).


Normally, a linked list has a beginning and an end. You must never lose track of the
beginning of the linked list or there is no way to find it, since links always point
forwards. To avoid this problem, there are two common variations of li
nked lists, namely
circular linked lists

and
double linked lists
.


A circular linked list is similar to a regular linked list, but the last node points back to the
first. Or, alternatively, you can think of it as there is no first node, just a circle of n
odes.
The nodes of a circular linked list have the same structure as nodes of a regular linked
list; they contain data and one pointer which is a link to another node. However, in a
circular linked list, no link is a NULL pointer.


One problem with a cir
cular linked list is that in order to insert a node at the beginning of
the list, you actually have to traverse the list to find the end of the list so that the last node
can point to the new beginning node. This can get annoying, and circular linked list
s do
not seem to be used much in practice.


A double linked list is a list in which each node has two links. One link points to the next
node and one points to the previous node. For example, the structure of a node in a
double linked list whose data is
just a single integer variable might be:


typedef struct node

{


int x;


struct node *back;


struct node *next;

} NODE;


typedef NODE *PNODE;


Now, inserting and deleting nodes from the list are more complicated than with regular
lists because there are mo
re pointers to update, but certain things are made simpler in that
you never have to keep track of previous nodes since you can always take a step back!
The only exception to this is if you step off the end of the list through a NULL link.
Then you can n
ot step back to the previous (final) node, as you can't dereference a NULL
pointer.


Sometimes, you can combine the two variations together, in which case you are dealing
with a circular, double linked list. The structure of each node is the same as for a

double
linked list, but there are no NULL links. The final node's "next" link points to the first
node of the list, and the first node's "back" link points to the final node.


We are now going to move on to a new topic somewhat related to linked lists; t
he topic is
trees
. Like linked lists, this topic involves a combination of structures, pointers, and
dynamic memory allocation. However, where as linked lists are comprised of a linear
sequence of nodes, every tree has a single
root

node, and each node c
an have zero, one,
or multiple children. If we are looking at two nodes "x" and "y" in a tree, and "y" is a
child

of "x", then "x" is said to be the
parent

of "y". Furthermore, if "y" is below "x",
and there is a path in the tree from "x" to "y", then "y
" is said to be a
descendant

of "x",
and "x" is said to be an
ancestor

of "y". A node with no children is said to be a
leaf

of
the tree, and all other nodes are call
internal nodes
. The root of a tree is said to be at
depth

0, and every other node is sai
d to have a depth which is the number of links from
the root to the node. The depth of the tree as a whole is the maximum depth of all nodes
within the tree.


We are going to restrict our attention to
binary trees
, in which each node can have at
most two
children. It is common to refer to the two children of a node as the left child
and the right child. Most binary trees are ordered in some way. For example, if the data
stored in each node is a single integer variable, a likely rule will be that all des
cendants of
a node "x" whose data has a value less than the value of the data in "x" should be stored
in the left subtree of "x", and all descendants whose data has a value greater than or equal
to the value of the data in "x" should be stored in the right

subtree of "x".


The structure of a node for a binary tree whose data is a single integer variable might be
defined as follows:


typedef struct node

{


int x;


struct node *left;


struct node *right;

} NODE;


typedef NODE *PNODE;


Here is a routine to cre
ate a node of this type to store a given integer:


/* Creates a node for given data and return pointer to it. */

PNODE create_node(int x)

{


PNODE pTemp;


pTemp = (PNODE) malloc (sizeof(NODE));

if (pTemp == NULL)

{



printf("Out of memory, could not store
number!
\
n");

}

else

{



pTemp
-
>x = x;


pTemp
-
>left = NULL;


pTemp
-
>right = NULL;

}



return pTemp;

}


If you compare this to the "create_node" routine for creating nodes to be placed in a
linked list, you will find that it is almost identical, except that
there are now two links
which must be set to NULL.


When inserting nodes into a binary tree using the ordering rule discussed above, all new
nodes are inserted as leafs in the tree. To determine the location to insert a new node,
you start searching the t
ree at the root. If the data in the new node is greater than or equal
to the data in the current node, you step to the right child of the current node and continue
the search. If the data in the new node is less than the data in the current node, you ste
p
to the left child of the current node and continue the search.


Here is a routine which will insert a node into a binary tree and return a pointer to the
new tree, assuming the ordering rule discussed above:


/* Inserts node pNew into pRoot. */

PNODE ins
ert_node(PNODE pRoot, PNODE pNew)

{


PNODE pParent, pCur;




/* If this is the first element, it becomes the root of the tree. */


if (pRoot == NULL)



return pNew;



/* Find position of new element, start searching at root of tree. */


pParent = NULL;


pC
ur = pRoot;


do


{



if (pNew
-
>x < pCur
-
>x)



{




/* New node belongs in left subtree. */




pParent = pCur;




pCur = pCur
-
>left;



}



else



{




/* New node belongs in right subtree. */




pParent = pCur;




pCur = pCur
-
>right;



}


} while (pCur != N
ULL);



/* Insert new node as appropriate leaf. */


if (pNew
-
>x < pParent
-
>x)



pParent
-
>left = pNew;


else



pParent
-
>right = pNew;



return pRoot;

}


This routine inserts a node into a tree starting at "pRoot". If the root is NULL, then the
new node bec
omes the root of a new tree. Otherwise, we search left and right, following
the rule which determines ordering, until we reach a NULL pointer. At that point, we
insert the new node as a leaf where the NULL pointer was.


Once a tree like that has set up,
a simple procedure can be followed to print out all
numbers in sorted order. We know that given any node, all nodes in the left subtree must
have values less than the value of the current node, and all nodes in the right subtree must
have values greater t
han or equal to the value of the current node. Therefore, as long as
we make sure to print out the values of all nodes in the left subtree before the value of the
current node and the value of the current node before the values of all nodes in the right
s
ubtree, and this is true for all nodes, we will be printing out the values in sorted order. A
traversal of the tree which prints out values in this order (left child, current node, right
child) recursively is called an
inorder traversal

of the tree. Here

is the routine:


void inorder_traversal(PNODE pCur)

{


if (pCur == NULL)



return;



inorder_traversal(pCur
-
>left);


printf("%d
\
n", pCur
-
>x);


inorder_traversal(pCur
-
>right);


return;

}


Of course, if the binary tree was set up in an arbitrary manner, wit
hout the ordering rule
discussed above being followed, an inorder traversal of the tree will not print the values
in sorted order!


Finally, here is a function "main" which assumes the existence of the above structure and
routines and uses a binary tree to

allow the user to type in integers until they signal EOF
and then displays the numbers in sorted order:


int main(void)

{


int x;


PNODE pNode, pRoot = NULL;



printf("Enter numbers one at a time, Ctrl
-
D to stop.
\
n");


while(scanf("%d", &x) != EOF)


{



p
Node = create_node(x);



if (pNode != NULL)




pRoot = insert_node(pRoot, pNode);


}



inorder_traversal(pRoot);



return 0;

}


If numbers happen to be entered in sorted order already, the tree will just grow to the
right every time, and after "n" integers

are entered, the tree will have depth "n
-
1". If the
numbers happen to be entered in reverse sorted order, the tree will just grow to the left
every time. As it turns out, however, if integers are entered in a basically random order,
the average node wil
l wind up being at a depth of approximately "log n", and if all
possible orders of input are equally likely, the expected run time of a sort using the above
method is O(n log n), which is as good as merge sort! In the worst case (for instance,
when the nu
mbers are already sorted), this run time of this sorting method is O(n^2). It
also turns out there are complex variations of binary trees which allow you to ensure
efficient sorting even in the worst case.


Of course, as with linked lists, we could rewrit
e the program such that each node also
contains a "count" field, and if the same integer is entered more than once, then for all
times other than the first, no new node is created and instead the node for the integer has
its count increased. We won't actu
ally examine that code.


Although it may seem more efficient to always use the method with counts, instead of
duplicating nodes with the same data, remember that sometimes nodes will be sorted
based on one field, but may contain other data that is not iden
tical between nodes with
identical keys. For example, let's say we were sorting nodes representing phone book
entries based on last names. Two nodes may share the same last name, but different first
names and phone numbers. Or, maybe you will even have
two entries for the same
person with two different phone numbers. If the sort routine is only looking at the fields
upon which the sorting is based, it can not simply increase the count of a node that has an
identical field to a new entry, and must create

a new node to store the new entry.


Deleting a node from a binary tree is more complex, especially when the node has both
children. The basic technique is to replace the deleted node with one of its subtrees and
then to insert the other subtree into the
first. We won't go over this in detail.