0% found this document useful (0 votes)
51 views7 pages

A Doubly Linked Circular List

This document provides instructions for implementing a doubly linked circular list data structure. Students are asked to create three files: circular.h (header file), circular.c (implementation file), and circular-test.c (test file). The circular.c file should contain 11 functions for manipulating and traversing the list, including insertBefore(), insertAfter(), removeSpot(), printListF(), printListR(), printSpot(), next(), previous(), copy(), and helper functions. Students are given examples of how to use the functions and are asked to test their implementation before submitting the files.

Uploaded by

khawar abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views7 pages

A Doubly Linked Circular List

This document provides instructions for implementing a doubly linked circular list data structure. Students are asked to create three files: circular.h (header file), circular.c (implementation file), and circular-test.c (test file). The circular.c file should contain 11 functions for manipulating and traversing the list, including insertBefore(), insertAfter(), removeSpot(), printListF(), printListR(), printSpot(), next(), previous(), copy(), and helper functions. Students are given examples of how to use the functions and are asked to test their implementation before submitting the files.

Uploaded by

khawar abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab#7: Friday 24th April 2020

Data Structure and Algorithms

Starting Time: 3:00 PM --- Ending Time 11:00PM

Course Instructor: Dr. Zobia Suhail

Marks: 100

A Doubly Linked Circular List

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

This is the header file that accompanies

circular.c

which provides the interface to the list.

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

circular.c file you can use the following statement:

//

Globally maintained spot pointer.

Static Node spotPtr=NULL;

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

Allow testing of the functions

The functions provided in circular.c are as follows

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

of the spot node.

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()

Print the string associated with the spot node.

7. next()

Move the spot to the next one in the list.

8. previous()

Move the spot to the previous one in the list.

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

the spot is not NULL).

11. next()

This is also a very simple function. Provided the spot is not NULL, this function merely

sets the spot pointer to be the node given by spotPtr->next.

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”

node, we end up pointing to the same node.

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

pointers in the “neighbors” of the new node.

After getting these three functions implemented, you should try to run them with the test
function

(i.e. main () in circular-test.c).

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

same for all lists with two or more nodes).

Here is session that demonstrates the use functions (user input is shown in

bold):

Valid commands:

b = insertBefore () [insert string before spot]

a = insertAfter () [insert string after spot]

r = removeSpot () [remove node corresponding to spot]

f = printListF () [print all nodes in forward order]

R = printListR () [print all nodes in reverse order]

s = printSpot () [print spot]

n = next () [advance spot to next node]

p = previous () [move spot to previous node]

c = copy () [obtain copy of spot]

h = help [this menu]

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

Copy of spot: third string

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.

You might also like