UMBC CMSC202, Computer Science II, Spring 1998,
Sections 0101, 0102, 0103, 0104 and Honors
Thursday March 19, 1998
Assigned Reading:
- A Book on C:
- Programming Abstractions in C:
Handouts (available on-line):
Topics Covered:
- Yet another implementation of the list ADT. This time,
we use a doubly-linked list. Each node on the list has a
pointer to the previous item on the list as well as a pointer to the
next item. Thus, it is possible to traverse the list in both
directions. In particular, we can then use the pointer to a node to
represent the position of a node we want to delete. (In the
singly-linked list implementation, we used the pointer to the node
before the node we want to delete.) In this example, we also use
make the list circular. That is, the last node of the list points
to the header node instead of NULL. (See header file, implementation, main program and sample run.)
- How would you sort a linked list? It turns out that the
easiest way is to sort an array of pointers to nodes on the
linked list. In this example, we reuse the Mergesort function from
before. Note that all we needed to
change in the source code for Mergesort was two type definitions and
the one line in the merge routine which does a comparison.
(See
header file,
implementation,
main program and
sample run.)
- Next we considered a new ADT that maintains an ordered
linked list. The header file shows
that the new interface simply includes list1.h then
provides a new insertion function and two new search functions. The
search functions take advantage of the fact that the list is
ordered. So, if we see an item that is alphabetically larger than
the key, then we already know that the list does not contain the
item. Note that it was necessary for us to use new names for the
new search routines so they do not conflict with the search routines
in list1.h. Also, the implementation of the ordered list ADT must
have its own NewNode() and FreeNode() functions
because these functions were private in the implementation of
list1.c --- even though the implementation of
NewNode() and FreeNode() are identical in the
two files. It is a serious deficiency of C that we cannot have
functions be "private" in certain situations but "available"
in other situations. This deficiency and the naming deficiency
mentioned previously will be resolved when we use C++.
(See also:
main program and
sample run.)
Last Modified:
22 Jul 2024 11:27:45 EDT
by
Richard Chang
Back up
to Spring 1998 CMSC 202 Section Homepage