Docsity

DSA Lab Report 4 Comsats, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
Lab 04 Implementation of Circular Doubly
Linked Lists in C language
Objectives:
•Learn to create the circular doubly linked lists.
•Implement node traversal in circular doubly linked lists.
•Implement other linked list operations such as node insertion and deletion.
•Learn to use linked lists in practical applications by solving classical Josephus
Problem
In Lab Tasks
For this lab you are given the following files:
1. main.c
2. Node2.h
3. DoublyLinkedList.h
4. DoublyLinkedList.c
5. EmployeeData.dat
Task01: Debugging code for errors.
•Make a new project and add these files to the project.
•Compile and run the program
•Choose to load the list from the file
The errors were ā€œMismatch file namesā€ in Node.h files.
Task 02: Implementing Node Removal and Node Insertion Tasks
Use the created circular linked list in Task 01 and add functions for
1. void deleteNodeFromStart(struct node_d ** head)
void insertNodeAtStart(struct node_d **head)
{
struct node_d *newNode = (struct node_d *)malloc(sizeof(struct
node_d));
printf("Enter data for the new node: ");
scanf("%d", &(newNode->data));
newNode->next = *head;
*head = newNode;
}
/** Function to delete the first node in the list.
Note: This function takes an argument of type pointer to pointer.
*/
void deleteNodeFromStart(struct node_d **head)
{
if (*head == NULL) {
printf("List is empty. Nothing to delete.\n");
return;
}
struct node_d *temp = *head;
pf3
pf4
pf5

Partial preview of the text

Download DSA Lab Report 4 Comsats and more Exams Data Structures and Algorithms in PDF only on Docsity!

Lab 04 Implementation of Circular Doubly

Linked Lists in C language

Objectives:

• Learn to create the circular doubly linked lists.

• Implement node traversal in circular doubly linked lists.

• Implement other linked list operations such as node insertion and deletion.

• Learn to use linked lists in practical applications by solving classical Josephus

Problem

In Lab Tasks

For this lab you are given the following files:

1. main.c

2. Node2.h

3. DoublyLinkedList.h

4. DoublyLinkedList.c

5. EmployeeData.dat

Task01: Debugging code for errors.

• Make a new project and add these files to the project.

• Compile and run the program

• Choose to load the list from the file

The errors were ā€œMismatch file namesā€ in Node.h files.

Task 02: Implementing Node Removal and Node Insertion Tasks

Use the created circular linked list in Task 01 and add functions for

  1. void deleteNodeFromStart(struct node_d ** head) void insertNodeAtStart ( struct node_d ****** head ) { struct node_d ***** newNode = ( struct node_d *) malloc (sizeof( struct node_d )); printf ( "Enter data for the new node: " ); scanf ( "%d" , &( newNode -> data )); newNode -> next **= *** head ; *** head = newNode ; } / Function to delete the first node in the list. Note: This function takes an argument of type pointer to pointer. / void deleteNodeFromStart ( struct node_d ****** head ) { if ( head == NULL) { printf ( "List is empty. Nothing to delete.\n" ); return; } struct node_d ***** temp **= *** head ;

***** head = (* head )-> next ; free ( temp ); }

  1. int deleteNodeAfter(struct node_d * head, int idx) /** This function deletes the node after the node at idx. */ int deleteNodeAfter ( struct node_d ***** head , int idx ) { if ( head == NULL || head -> next == NULL) { printf ( "List is empty or has only one node. Nothing to delete.\n" ); return 0 ; } int count = 0 ; struct node_d ***** current = head ; struct node_d ***** temp ; while ( current != NULL && count < idx ) { current = current -> next ; count ++; } if ( current == NULL || current -> next == NULL) { printf ( "Node at index %d doesn't exist or doesn't have a next node. Nothing to delete.\n" , idx ); return 0 ; } temp = current -> next ; current -> next = current -> next -> next ; free ( temp ); return 1 ; }
  2. void insertNodeAtStart(struct node_d ** head) void printMemMap ( struct node_d ***** head ) { int index = 0 ; int i ; printf ( "No.\t\t Head\t\t Next\t\t Previous\t Data" ); for ( i = 0 ; i <= 100 ; i ++); while ( index < getListLength ( head )) { printf ( "\n%d\t\t" , index ); printf ( "%u\t\t" , head ); printf ( "%u\t\t" , &( head -> next )); printf ( "%u\t\t" , &( head -> prev )); printf ( "%s\t\t" , (( head -> data ). name )); head = head -> next ; index ++; }

prevPerson = newPerson ; } newPerson -> next = head ; head -> prev = newPerson ; return head ; } // Print the names of the people in the list void printList ( struct Person ***** head ) { struct Person ***** current = head ; do { cout << current -> name << " " ; current = current -> next ; } while ( current != head ); cout << endl ; } // Eliminate every mth person in the list until only one person remains void josephus ( int n , int m ) { struct Person ***** head = createList ( n ); struct Person ***** current = head ; int count = 1 ; while ( head -> next != head ) { if ( count == m ) { current -> prev -> next = current -> next ; current -> next -> prev = current -> prev ; struct Person ***** temp = current ; current = current -> next ; delete temp ; count = 1 ; printList ( head ); } else { current = current -> next ; count ++; } } cout << "The last person standing is " << head -> name << endl ; } int main () { int n , m ; cout << "Enter the number of people standing in the circle: " ; cin >> n ; cout << "Enter the number of people to be eliminated in each iteration: " ; cin >> m ; josephus ( n , m ); return 0 ; }

Output:

Critical Analysis and Conclusion

In conclusion, the implementation of Circular Doubly Linked Lists in C language is a

powerful and versatile data structure that allows efficient traversal in both forward and

backward directions. It provides better performance in comparison to singly linked lists,

especially in use cases where frequent traversal in reverse order is required. The circular

aspect of the data structure allows for efficient insertion and deletion at both ends of the

list. The implementation involves the use of a doubly linked list with two pointers: one for

the previous node and one for the next node. The circular aspect is achieved by

connecting the first node's previous pointer to the last node and the last node's next pointer

to the first node. It is important to note that proper memory management is crucial when

working with linked lists to avoid memory leaks. The use of dynamic memory allocation

with functions such as malloc() and free() should be employed to ensure efficient use of

memory. Overall, the implementation of Circular Doubly Linked Lists in C language

provides a powerful tool for managing and manipulating data in a variety of use cases.