A Doubly Linked Circular List
A Doubly Linked Circular List
Marks: 100
Objectives:
Gain familiarity with another important data-structure construct: doubly linked lists.
Note: Before starting task you should create Circular Doubly Linked List such that the
Underlying construction is hidden from the users of these functions. (Please see the last
page from the lecture hand-outs that have been given in last lecture for pictorial
understanding of Circular Double Linked List)
Task:
With the circular list we will not have a beginning or an end, but we must maintain a pointer
to at least one node in the list. We will call this node the “spot.” Starting from the spot, we
can do whatever needs to be done. The details of the functions we are interested in
implementing will be described in a moment, but, as examples of what we would like to be
able to do: starting from the spot, we want to be able to print the information associated
with the entire list (we want to display this in either the “forward” or “backward” direction);
we want to be able to advance the spot to the next node or the previous node; we want to
be able to insert values into the list either before or after the spot; we want to be able to
remove the spot node from the list; and, we want to be able to obtain a (deep) copy of the
data associated with the spot.
The nodes in our list will consist of character pointer and pointers to the next and previous
nodes. The character pointer corresponds to a string and is the “data” associated with each
node.
For this lab you must create three files. They are:
circular.h
circular.c
This is the implementation file for the circular list. Access to the list is gained via the pointer
to the spot node. This pointer is stored as a static global variable in this file. Specifically, in
//
In this way, all of the functions in circular.c have access to this pointer (while functions
outside of the file have no such access).
circular-test.c
1. insertBefore()
Insert the string argument in a new node that is placed before the spot. If the
spot was NULL , the new node becomes the spot. (Otherwise, do not change the spot.)
2. insertAfter()
Insert the string argument in a new node that is placed after the spot. If the spot
Was NULL , the new node becomes the spot. (Otherwise, do not change the spot.)
3. removeSpot()
Remove the spot node and the memory associated with this node. If there is more
than one node in the list, the spot should be set to the node that was formerly the “next”
node. This function must “tie together” the nodes that remain in the list after the removal
4. printListF()
Starting from the spot node, print the string associated with each node. The output
should be in the “forward” order—moving from one node to the next one in the list.
5. printListR()
Starting from the spot node, print the string associated with each node. The output
should be in the “reverse” order—moving from one node to the previous one in the list.
6. printSpot()
7. next()
8. previous()
9. copy()
Provide a (deep) copy of the string associated with the spot node. The address of the
copy is the return-value of this function. The function returns NULL if the list is empty.
10. printSpot()
This is a simple function where the body of the function can be implemented in three
lines (one of these lines is a return statement and another is an if -statement to ensure
11. next()
This is also a very simple function. Provided the spot is not NULL, this function merely
12. insertAfter()
A string argument (char pointer) is provided to this function. When this function starts, it
should allocated memory for a new node, make a copy of the given string, and link that
copy to this new node. If there are no nodes in the list, i.e., if spotPtr is NULL, then set
spotPtr to point to this new node. Also, set both the next and prev pointers of this node
to point back to itself. Thus, if there is only one node in the list and we move to the “next”
If there is already one, or more, nodes in the list, the new node should be placed
between the spot node and the spot node’s “next” node. Thus the new node should be
placed between spotPtr and spotPtr->next. It is likely that you will want to declare a
temporary Node to help you with this task. To tie the new node into the list will require
that you set four node pointers: the two in the new node and the previous and next
After getting these three functions implemented, you should try to run them with the test
function
The only remaining function with a slight bit of complexity is removeSpot(). The “trick” here
is
that you need to detect if there is only one node in the list. To do this, you can, for example,
simply compare spotPtr to spotPtr->prev . If these pointers are the same, there is only one
node in the list and you then take one set of actions. If the pointers are not the same, there
are
at least two nodes in the list and you take a different set of actions (and these actions are
the
Here is session that demonstrates the use functions (user input is shown in
bold):
Valid commands:
q = quit
cmd: f
first string
second string
third string
cmd: p
status: 1
cmd: s
third string
cmd: R
third string
second string
first string
cmd: c
cmd: r
status: 1
cmd: f
first string
second string
cmd: s
first string
cmd: r
status: 1
cmd: s
second string
cmd: f
second string
cmd: q
Once you are convinced your functions are working properly, please email the code to
the TAs. If the TAs agrees that your code is correct (as per the execution of the above
commands), you are done for this Lab7.
Good Luck.