CSC-112
Algorithms and Data Structures
Lab Report # 03
Name: Muhammad Souban Javaid
Roll No: FA20-BEE-146
Section: 6C
Submitted to: Sir Muhammad Imran
Dated: 19-03-2023
Lab 03: Advanced Topics in Singly Linked List Implementation
Objectives:
• Learn to insert nodes in a linked list.
• Learn to delete nodes from the linked list.
• Learn to store/load database to/from a file.
In-Lab Tasks:
You are given the following four files for this lab;
1. ‘main.c’ // This file contains the main application (menu based)
2. ‘SinglyLinkedList.c’ // This file contains the functions to implement linked list.
3. ‘SinglyLinkedList.h’ // This file contains the prototypes of functions.
4. ‘Node.h’ // This file contains the structure definitions for the linked list.
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’.
Inserting at Beginning:
Function:
int insertNodeAtStart(struct node ** head)
{
struct node * new_node = (struct node *) malloc(sizeof(struct node));
inputNodeData(new_node);
new_node->next=*head;
*head = new_node;
Calling function:
case 8:
{
insertNodeAtStart(&head);
break;
}
Output:
Inserting before Node:
Function:
int insertNodeBefore(struct node * head, int idx)
{
idx = idx-1;
int index = 0;
struct node * temp = head;
if(isListEmpty(head)) /// if currently the list is empty return -1
{
return(-1);
}
///create a new node
struct node * new_node = (struct node *) malloc(sizeof(struct node));
inputNodeData(new_node); /// get data for the newly created node from the user.
while((index != idx) && (temp->next) != NULL)
{
index ++;
temp = temp->next; /// scroll to the end of the list
}
if((temp->next == NULL) && (index != idx)) /// We reached the end of the list without
{ /// reaching the required index
free(new_node);
printf("\nNode insertion not done. New data is discarded!!\n");
return(-1);
}
new_node->next = temp->next; /// Setting up the pointers for insertion
temp->next = new_node;
return(0);
}
Calling Function:
case 9:
{
int idx = 0;
printf("\nEnter the index before which you want to insert a node: ");
scanf("%d",&idx);
flush();
if(idx==0){
printf("can not add data before index 0");
break;
}
if(insertNodeBefore(head, idx) == 0)
printf("\nNode inserted successfully!");
else
printf("\nIndex exceeds the list length!!\n");
break;
}
Output:
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.
Delete From beginning:
Function:
void deleteNodeFromStart(struct node ** head)
{
struct node * temp = *head;
*head = (*head)->next;
free(temp);
}
Calling Function:
case 10:
{
if(head!=NULL){
deleteNodeFromStart(&head);
printf("\nfirst node deleted\n");
break;
}
else{
printf("list already empty.\n");
break;
}
}
Output:
Delete after given Node:
Function:
int deleteNodeAfter(struct node * head, int idx)
{
int index = 0;
struct node * temp = head;
if(isListEmpty(head)) /// if currently the list is empty return -1
{
return(-2);
}
while((index != idx) && (temp->next) != NULL)
{
index ++;
temp = temp->next; /// scroll to the end of the list
}
if((index != idx) || (temp->next) == NULL) /// We reached the end of the list without
{ /// reaching the required index
return(-1);
}
struct node *next = temp->next->next;
free(temp->next); // Free memory
temp->next = next;
return(0);
Calling Function:
case 11:
{
int idx = 0;
printf("\nEnter the index after which you want to delete a node: ");
scanf("%d",&idx);
flush();
if(deleteNodeAfter(head, idx) == 0)
printf("\nNode deleted successfully!");
else if(deleteNodeAfter(head, idx) == -1)
printf("\nIndex exceeds the list length!!\n");
else if(deleteNodeAfter(head, idx) == -2)
printf("list empty. !!");
break;
}
Output:
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.
Function SaveListToFile:
int saveListToFile(struct node * head, FILE * fptr)
{
struct employee new_record;
while(head!=NULL){
new_record=head->data;
fwrite(&new_record, sizeof(struct employee), 1, fptr);
head=head->next;
}
return 0;
}
Output:
Conclusion / Critical Analysis:
In conclusion, the lab addressed a variety of topics, including the basic data structure operations
of adding and removing nodes from a linked list. To make these concepts easier to grasp,
thorough diagrams and code samples were used in the explanations. The lab also addressed data
loading and storing in files, which is an essential component of data management in real-world
applications.
******** End of Document *******