Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

IT3201 Data Structures

Lab2 Manual –Linked List I

Linked List
There are two ways of storing data in memory, namely storing in fixed data locations and in dynamic
locations. The fixed locations are assigned at compilation time, whereas the dynamic locations are
allocated at runtime. Fixed locations are reserved using static data types/structures such as array.
Whereas, the dynamic locations are reserved using pointers. One of the data structures that occupy
memory dynamically using pointers is called linked list.

A list of the same type can be stored in arrays or in linked lists. Thus, fixed number of lists are stored using
arrays, whereas dynamically changing lists shall be stored using linked lists. Both arrays and linked lists
are two alternatives to store lists.

A linked list is a linear data structure for specially designed data elements, called nodes, linked to one
another by means of pointers. Each node has two parts, namely, the data/information part and the
address/next part. The data part holds the data element of the list, whereas the address part holds the
address of the next node.

Some important tips for the linked list data structure:

• The arrow is drown from the address part to the next node.
• The next pointer of the last node contains a special value, called the NULL pointer, which does not
point to any address of the node. i.e. NULL pointer indicates the end of the linked list.
• START pointer will hold the address of the first node in the list START=NULL if there is no list (i.e.
NULL list or empty list)

Linked List Representation


Suppose we want to store a list of integer numbers using linked list. It can be schematically represented:

Asmelash Girmay
Department of Information Technology
IT3201 Data Structures
Lab2 Manual –Linked List I
And the linear linked list can be represented in memory using C programming as:

struct Node {
int DATA; // the information part of the node
struct Node *Next; // The address part of the node
} typedef struct Node *NODE;

Operations on Linked List


The primitive operations of linked list are:

1. Creation – used to create a linked list. Once a linked list is created, insertion operation works for
new nodes.
2. Insertion – is used to insert new node at any specified location of the linked list. A new node may
be inserted:
o At the beginning of the linked list
o At the end of the linked list
o At any specific position in between the linked list
3. Deletion – is used to remove a node from the linked list, maybe, from
o The beginning of the linked list,
o The end of the linked list, or
o Any specific position of the linked list
4. Traversing – is the process of going through all the nodes from one end to another end of a linked
list. Where,
o In singly linked list, traversal from left to right is possible
o In doubly linked list, traversal from left to right and vice versa is possible
5. Searching – is used to find a data element from the linked list.
6. Concatenation – is the process of appending one linked list at the end of the first linked list.
o If linked list A has n nodes and list has m bodes.
▪ Then, if B is appended at the end of A’s node, its placement is at (n + 1)th node
▪ After concatenation, A will contain (n + m) nodes.

Types of Linked List


There are three types of linked lists, namely singly linked list, doubly linked list, and circular linked list. In
this lab, you will concentrate on the singly linked list.

Singly Linked List


The above description of linked list, by default, it is about singly linked list. I.e., all the nodes in a linked
list are arranged sequentially linked by a pointer. As it is dynamic data structure, it can grow or shrink at
runtime. A singly linked list is identified with only one pointer and one or more data parts.

Example:

Asmelash Girmay
Department of Information Technology
IT3201 Data Structures
Lab2 Manual –Linked List I

Algorithms for Singly Linked List operations


1. Insertion of a node

Asmelash Girmay
Department of Information Technology
IT3201 Data Structures
Lab2 Manual –Linked List I
2. Deletion of a node

3. Search for a node

4. Traversal around the linked list

Asmelash Girmay
Department of Information Technology
IT3201 Data Structures
Lab2 Manual –Linked List I
Lab Exercise
1. Singly Linked List Operations
Use the above algorithm of the following singly linked list operations to implement them using singly
linked list: Insertion, Deletion, Traversing, Concatenation, and Searching.
Then, test your work using scenario 101 with the following requirements:
• Insertion shall be made in numbers natural order,
• Make traversal after each insertion and deletion operation
• Use the linked list sequentially as output of step 1 is an input to the step 2, the output of step 2 is
an input to the step 3, and so on.
• Search for students with ID number of 297 before deletion and after deletion.
Scenario 101
FIT Fidel Institute of Technology is a private owned university located in Mekelle city. As a result of the
open registration session, students are registering at relaxed time interval. Each student is assigned an ID
number with the format of “FID/YYYYMMDD-TXXX/11”, where YYYY stands for four-digit year, MM for
two-digit month, DD for two-digit date, T for temporary, XXX for three-digit unique random number
assigned for each student, and the last “11” tells that the batch is starting their study at 2011 E.C. Example:
FID/19901130-T657/11. Consider the last XXX number format and perform the following operations using
your implementation of singly linked list operations.
A. Hagos is the first comer student for the registration. He is registered on October 30, 2018 at 8:40.
Thus, create a linked list that contains Hagos’s system assigned ID number = 825
B. Birhan was with Hagos, of course, she registered the same day at 8:55. The system assigned her
an ID number = 297
C. Tsige has arrived at 9:00 of the same day and registered at 9:10 with system generated ID = 406
D. Bahta is registered after two days on November 1, 2018 at 10:00 with system generated ID
number = 331
E. Birhan, whose ID number is FID/19900621-T297/11 got a scholarship opportunity to MIT Mekelle
Institute of Technology. As a result, she left FID on November 1, 2018 at 12:00, thus, the institute
(FID) removed her from the list of students of this academic year.
F. Tsedal arrived on November 1, 2018 at 14:30 (after noon) and registered with the system
generated new ID number = 527
G. Kiros, Girmay, and Selam with IDs 544, 983, and 201 respectively transferred from Humera
campus of the same university to Mekelle campus. As a result, their short list is added to the
students list in Mekelle campus.
H. Meanwhile, after classes started, Selam returned to her home city, Humera to peruse her study
at Humera campus of FID.
I. A late registered student, Hadgay with system generated ID number = 390 joined the class after
two weeks.
What are you going to modify in your code to represent a student with name and full format ID number?

Asmelash Girmay
Department of Information Technology
IT3201 Data Structures
Lab2 Manual –Linked List I
2. Polynomial Function
Design linked list representation of polynomial functions with its corresponding addition and subtraction
operations of two polynomials, then implement your design using singly linked list. To test your work,
represent the following polynomial functions using your code:

A. F(x) = 3x4 + x2 + 45x + 67


B. G(x) = x7 + 7x5 + 5x3 + 3x + 3
C. H(x) = F(x) + G(x)
D. I(x) = G(x) – G(x)

Discuss how to implement multiplication and division of two polynomial functions using linked list.

P.S., For queries, contact your lab assistant. At the end of your lab exercise, please show to your lab
assistant and upload your work through the eLearning form provided before the deadline.

Asmelash Girmay
Department of Information Technology

You might also like