0% found this document useful (0 votes)
76 views

Data Structure and Algorithum LAB 02

This document describes a lab assignment on implementing a singly linked list in C. The objectives are to create a linked list, insert nodes at the beginning and end, and perform operations like searching and finding the length. The tasks involve making a project with main.c, employee.c and employee.h files, adding a function to print the length, writing a search function to find records by age, and adding a delete function to remove the last node. Sample C code is provided to demonstrate creating a linked list, adding nodes, printing and searching the list. The conclusion states the lab helped learn singly linked lists and reinforced C concepts for data structures.

Uploaded by

earn money
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Data Structure and Algorithum LAB 02

This document describes a lab assignment on implementing a singly linked list in C. The objectives are to create a linked list, insert nodes at the beginning and end, and perform operations like searching and finding the length. The tasks involve making a project with main.c, employee.c and employee.h files, adding a function to print the length, writing a search function to find records by age, and adding a delete function to remove the last node. Sample C code is provided to demonstrate creating a linked list, adding nodes, printing and searching the list. The conclusion states the lab helped learn singly linked lists and reinforced C concepts for data structures.

Uploaded by

earn money
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Data structure and Algorithms

Lab 2

Name
Ch Zohaib

Registration Number FA20-BCE-017

Class BCE-5A

Instructor’s Name Dr. Omer Ahmed


Lab 02 Singly Linked List Implementation

Objectives:

• Learn to create a singly linked list.


• Learn to insert nodes at the end/start of a singly linked list.
• Learn to implement other simple linked list operations such as searching the list for key,
find the length of the linked list etc.

Pre-Lab

Reading Task 1:

Working with Linked Lists in C:

A linked list, in simple terms, is a linear collection of data elements. These data elements are
called nodes. Linked list is a data structure which in turn can be used to implement other data
Learning Objective A linked list is a collection of data elements called nodes in which the linear
representation is given by links from one node to the next node. In this chapter, we are going to
discuss different types of linked lists and the operations that can be performed on these lists. A
linked list can be perceived as a train or a sequence of nodes in which each node contains one
or more data fields and a pointer to the next node.

Figure 1: Visual depiction of a singly linked list

In Figure 1, we can see a linked list in which every node contains two parts, an integer and a
pointer to the next node. The left part of the node which contains data may include a simple
data type, an array, or a structure. The right part of the node contains a pointer to the next node
(or address of the next node in sequence). The last node will have no next node connected to it,
so it will store a special value called NULL . In Fig. 6.1, the NULL pointer is represented by X .
While programming, we usually define NULL as –1. Hence, a NULL pointer denotes the end of
the list. Since in a linked list, every node contains a pointer to another node which is of the same
type, it is also called a selfreferential data type.
Linked lists contain a pointer variable START that stores the address of the first node in the list.
We can traverse the entire list using START which contains the address of the first node; the next
part of the first node in turn stores the address of its succeeding node. Using this technique, the
individual nodes of the list will form a chain of nodes. If START = NULL , then the linked list is
empty and contains no nodes.
For further discussion on Linked Lists and their implementation read Chapter 6 of the book
“Data Structures using C”, Oxford University Press, 2nd Edition by Reema Thareja.
In-Lab Tasks:

You are given the following three files for this lab;

1. ‘main.c’ // This file contains the main application (menu based)

2. ‘employee.c’ // This file contains the functions to implement linked list

3. ‘employee.h’ // This file contains the structure definition and prototypes of functions.

In-Lab Task 1:

Make a new project and add the above mentioned files to this project. Compile and run the
program.
Use the function ‘int getListLength(struct employee * emp)’ defined in employee.c to
print the length of the linked list. You will have to add an option in the menu so that the user
may see the length of the list any time he wishes.
In-Lab Task 2:

Write a function to implement ‘search by key’ feature for the linked list. This function should be
able to search the database by ‘age’ and list the appropriate records. It is okay at this stage if
only the first record with the selected age is displayed.
Post-Lab Task:

Write appropriate function to implement the delete function. This function should allow the user
to delete the last node in the list.

Make sure that you deallocate the memory for the deleted node (using C function free()).
In C, we can implement a linked list using the following code
/************************************************************************** *
* This program demonstrates making linked lists using C structures.
*
* The following features of the list will be demonstrated. * 1. Add a new node to the list (at the
end)
* 2. Print the list.
* 3. Search the list for a record.
*
* Author: Dr. Omar Ahmad
* Dated: 22-09-2020
*
**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
void flush();
/// Declare a new structure type which will serve as our node in the linked list struct
node
{
int data;
struct node * next;
};

int main(void)
{ struct node * head = NULL; /// The list is currently
empty.
int choice = 0;
printf("This program demonstrates the making and usage of linked lists in C.\n\n");
while(1) {
printf("\nEnter one of the choices below:\n\n");
printf("1. Add a new node to the list.\n"); printf("2.
Print the list.\n");
printf("3. Search for a data element in the list.\n");
printf("4. Exit the program.\n\n");

scanf("%d", &choice);

switch(choice)
{ case 1: { if(head
== NULL) /// case when the list is empty.
{
head = (struct node *) malloc(sizeof(struct node)); /// allocate space for the new node
head->data = rand()%21;
head->next = NULL; /// as this is currently the last node in the list
printf("New node with data: %d and address %u added to the list\n", head->data, head);
}
else {
struct node * ptr = head;
while(ptr->next != NULL) /// this loop will terminate when ptr is pointing to the last
node in the list
ptr = ptr->next;
ptr->next = (struct node *) malloc(sizeof(struct node)); /// allocate space for the new
node
ptr = ptr->next;

ptr->data = rand()%21;
ptr->next = NULL; /// as this is currently the last node in the list
printf("New node with data: %d and address %u added to the list\n", ptr->data, ptr);
}

}
break;
CONCLUSION:
In This lab we learn about Singly linked list and learn the difference
between linked list and arrays. The lab clear Our concept of c in
data structures.

You might also like