CptS 122
Data Structures
Programming
Assignment 3: Doubly Linked Lists
Assigned:
Due:
I. Learner
Objectives:
At the conclusion of
this programming assignment, participants should be able to:
II.
Prerequisites:
Before starting
this programming assignment, participants should be able to:
In the linked lists that we have used so far each node provides information about where is the next node in the list. It has no knowledge about where the previous node lies in memory. If we are at say 15th node in the list, then to reach the 14th node we have to traverse the list right from the first node. To avoid this we can store in each node not only the address of next node but also the address of the previous node in linked list. This arrangement is often known as 'Doubly Linked List' and is shown in the following figure.
As you can see from the illustration a doubly linked list has a pointer to the next node and the previous node in the list. The first nodes previous node pointer is always NULL and the last nodes next pointer is always NULL. When you insert and delete nodes from a doubly linked list, you must always carefully link the previous and next pointers.
Write a C program to implement this Doubly Linked List (DLL). First declare a structure representing a node of the doubly linked list.
struct dnode{
struct dnode *prev;
int data;
struct dnode *next;
}
You can have the following function prototypes to implement this.
/*adds a new node at
the end of the doubly linked list*/
d_append(struct
dnode **s, int num)
/*adds a new node at
the beginning of the doubly linked list*/
d_addatbeg(struct
dnode **s, int num)
/*adds a new node after
the specified number of nodes*/
d_addafter(struct
dnode *q, int loc, int num) //loc is not equal to 0
/*displays the contents
of the linked list*/
d_display (struct
dnode *q)
/*counts the number of
nodes present in the linked list*/
d_count (struct
dnode *q)
/*deletes the
specified node from the doubly linked list*/
d_delete(struct
dnode **s, int num)
2. In the same project, create one source file main.c. In main.c you should have the following codes to test your implementation.
Initialize an empty doubly linked list pointed by p;
Call d_append() function to add elements at the end of the doubly linked list (DLL) ;
d_display (p);
printf ("\n No. of Elements in the DLL: %d\n", d_count(p));
Call d_addatbeg()
function to add elements at the beginning of the DLL;
d_display (p);
printf ("\n No. of Elements in the DLL: %d\n", d_count(p));
Call d_addafter()
function to add elements after the specified number of
nodes in the DLL;
d_display (p);
printf ("\n No. of Elements in the DLL: %d\n", d_count(p));
Call d_delete()
function to delete elements from the DLL;
d_display (p);
printf ("\n No. of Elements in the DLL after deletion: %d\n", d_count(p));
Have your code print out a menu that looks like this:
***PA3 Menu - (your name) ***
*** The list currently has (n) items ***
1 = Append a new integer value to the end of the list
2 = Append a new integer value to the beginning of the list
3 = Insert a new integer value after a specific number of nodes
4 = Delete a node with a specific integer value
5 = Display the contents of the list
Enter selection:
Show this menu when your program starts and get the option from the user. After an option is selected, get any additional input values from the user that are relevant for the task they have selected. Then perform the task on the list and return to the menu. Note that the menu display requires you to show how many items are currently in the list (second line of menu output). Thus, your menu display code is expected to call the d_count() function.
V. Submitting
Assignments:
VI. Grading Guidelines:
This assignment is
worth 100 points. Your assignment will be evaluated based on a
successful
compilation and adherence to the program requirements. We will grade
according to
the following criteria: