Lab 03 Advanced Topics in Linked List Implementation
Lab 03 Advanced Topics in Linked List Implementation
Objectives:
Pre-Lab
Reading Task 1:
Read linked lists node insertion and deletion topics (page 167 to 180) from the book “Data
Structures using C” by Reema Thareja, 2nd Edition. Some excerpts from the book are listed here for
convenience.
New nodes can be inserted in a linked list in a variety of ways. Some of the cases are listed below:
If a new node is to be inserted at the beginning of a linked list, following steps should be performed:
• Allocate space for new node (unless system runs out of memory) and get its data.
• Let the ‘next’ part of new node contain the address of ‘start’
• Make ‘start’ point to the new node (which is now the first node in the list).
If a new node is to be inserted at the end of a linked list, following steps should be performed:
• Allocate space for new node (unless system runs out of memory) and get its data.
• Take a pointer which is pointing to the start of the list.
• Move this pointer to the last node of the list.
• Store the address of the new node in the ‘next’ part of this pointer.
• Now this new node is the last node in the list.
• Assign the ‘next’ pointer of the las node as NULL.
Read through rest of the section for the remaining two cases of node insertion in the linked list.
Deleting Nodes from a Linked List:
Nodes from an existing list can be deleted in a number of ways. Following are the most common
cases when deleting nodes from a linked list.
Case 1: The first node is to be deleted.
Case 2: The last node is to be deleted.
Case 3: The node after a given node is to be deleted.
Case 1: The first node is to be deleted.
When a the first node is to be deleted from the list following steps should be performed.
• Make ‘start’ point to the next node in the list. (but first copy its address in pointer).
• De-allocate the memory for the previous first node (using the above mentioned pointer).
For writing in file, it is easy to write string or int to file using fprintf and putc, but you might have
faced difficulty when writing contents of struct. fwrite and fread make task easier when you want to
write and read blocks of data. Following is a sample C program used to write structures to file using
‘fwrite’ function.
// a struct to read and write
struct person
{
int id;
char fname[20];
char lname[20];
};
int main ()
{
FILE *fptr;
int count = 0;
// open file for writing
fptr = fopen ("person.dat", "wb");
if (fptr == NULL)
{
printf("\nError opening file!!\n");
exit (1);
}
struct person input1 = {1, "Kamran", "Asghar"};
struct person input2 = {2, "Jameel", "Ahmed"};
// write struct to file
count += fwrite(&input1, sizeof(struct person), 1, fptr);
count += fwrite(&input2, sizeof(struct person), 1, fptr);
if(count != 0)
printf("contents to file written successfully !\n");
else
printf("Error writing to file !\n");
// close file
fclose (fptr);
return 0;
}
https://www.geeksforgeeks.org/readwrite-structure-file-c/
In-Lab Tasks:
You are given the following four files for this lab;
As you would know that each node in a singly linked list consists of a data part and a pointer to the
next node in the list. Our implementation defines the node as follows (in file ‘Node.h’).
struct employee
{
char name[50];
int age;
float bs;
};
struct node
{
struct employee data;
struct node * next;
};
You would notice that we have made the data part as a separate structure. This will allow us to write
generic functions for linked list modifications even if there are changes in the ‘data’ part. Moreover
it will allow us to store/load our database to/from a file much more conveniently.
In-Lab Task 1:
‘Inserting nodes at the end’ and ‘inserting node after a given node’ are already implemented in
‘SinglyLinkedList.c’. Your task is to implement ‘insert at the beginning’ and ‘insert before’
functions in the file ‘SinglyLinkedList.c’.
In-Lab Task 2:
Deleting a node from the end is already implemented in ‘SinglyLinkedList.c’ your task is to
implement ‘delete from beginning’ and ‘delete after’ a given node.
Post-Lab Task:
Reading database from a file on the hard disk is already implemented. Your first task is to study and
understand this implementation. Then you will have to implement the write to file function
‘saveListToFile()’. Submit a report on your implementation.