Lab 5 Manual
Lab 5 Manual
Course: Data Structures (CL2001) Semester: Spring 2023 Instructor: Bushra Sattar T.A:
Muhammad Anas
Note:
● Maintain discipline during the lab.
● Listen and follow the instructions as they are given.
● Just raise your hand if you have any problem.
● Completing all tasks of each lab is compulsory.
● Get your lab checked at the end of the session.
Linked List:
We saw that arrays had certain disadvantages as data storage structures. In an unordered array, searching
is slow, whereas, in an ordered array, insertion is slow. In both kinds of arrays, deletion is slow. Also, the
size of an array can’t be changed after it’s created.
Linked lists are probably the second most commonly used general purpose storage structures after arrays.
The linked list is a versatile mechanism suitable for use in many kinds of general-purpose databases. It can
also replace an array as the basis for other storage structures such as stacks and queues. In fact, you can
use a linked list in many cases in which you use an array unless you need frequent random access to
individual items using an index. A simple linked list is shown below:
The linked list shown above is called as Singly or Single-Ended Linked List because we have the
reference of only one end called the first (which refers/points to the front end of the list) and can move
only forward because each node has the reference of next node only.
Consider the above example; node 1 is the head of the list and node 4 is the tail of the list. Each node is
connected in such a way that node 1 is pointing to node 2 which in turn pointing to node 3. Node 3 is
again pointing to node 4. Node 4 is pointing to null as it is the last node of the list.
Algorithm
o Create a class Node which has two attributes: data and next. Next is a pointer to the next node.
o Create another class which has two attributes: head and tail.
o It first checks, whether the head is equal to null which means the list is empty.
o If the list is empty, both head and tail will point to the newly added node.
o If the list is not empty, the new node will be added to end of the list such that tail's next will point to the
newly added node. This new node will become the new tail of the list.
o Define a node current which initially points to the head of the list.
o Display each node by making current to point to node next to it in each iteration.
Task # 3: Add a node at the front of a Singly Linked List (Prepend a new node)
Task # 5
- Consider a singly linked list 4 -> 1 -> 5 - > 7 -> 2 and want to delete the value 5. The List should be
transformed as.
4 -> 1 -> 7 -> 2
To delete a node from the linked list, we need to do the following steps.
1) Find the previous node of the node to be deleted.
2) Change the next of the previous node to hold the node next to the node to be deleted.
3) Free memory for the node to be deleted.
Task # 6
Consider a singly linked list 4 -> 1 -> 5 - > 7 -> 2 and want to update the node 7 to 6. The List should be
transformed as.
4 -> 1 -> 5-> 6 -> 2
Updating Linked List or modifying Linked List means replacing the data of a particular node with the
new data. Implement a function to modify a node’s data when the key is given. First, check if the node
exists using the helper function node Exists.
Task # 7
Given a Linked List of integers, write a function to modify the linked list such that all even numbers
appear before all the odd numbers in the modified linked list. Also, keep the order of even and odd
numbers same.
Examples:
Input: 17->15->8->12->10->5->4->1->7->6->NULL
Output: 8->12->10->4->6->17->15->5->1->7->NULL
Input: 8->12->10->5->4->1->6->NULL
Output: 8->12->10->4->6->5->1->NULL
Given a Linked List of integers or string, write a function to check if the entirety of the linked list is a
palindrome or not
Examples:
The reference to the last link permits you to insert a new link directly at the end of the list as well as at the
beginning. Of course, you can insert a new link at the end of an ordinary single- ended list by iterating
through the entire list until you reach the end, but this approach is inefficient. Access to the end of the list
as well as the beginning makes the double-ended listsuitable for certain situations that a single-ended list
can’t handle efficiently. One such situation is implementing a queue.
20 10 40 35 30 10 20 30 35 40
Task-10:
Create a doubly link list and perform the mentioned tasks.
i. Insert a new node at the end of the list.
ii. Insert a new node at the beginning of list.
iii. Insert a new node at given position.
iv. Delete any node.
v. Print the complete doubly link list.
In the doubly linked list, the next pointer of the last node points to the first node and the previous pointer of
the first node points to the last node making the circular in both directions.
As per the above illustration, the following are the important points to be considered.
∙ The last link's next points to the first link of the list in both cases of singly as well as the
doubly linked list.
∙ The first link's previous points to the last of the list in case of a doubly linked list.
Basic Operations
Following are the important operations supported by a circular list.
∙ insert − Inserts an element at the start of the list.
∙ delete − Deletes an element from the start of the list.
∙ display − Displays the list
Task-11:
Create a circular link list and perform the mentioned tasks.
i. Insert a new node at the end of the list.
ii. Insert a new node at the beginning of list.
iii. Insert a new node at given position.
iv. Delete any node.
v. Print the complete circular link list.
Task-12:
Create a circular Double link list and perform the mentioned tasks.
i. Insert a new node at the end of the list.
ii. Insert a new node at the beginning of list.
iii. Insert a new node at given position.
iv. Delete any node.
v. Print the complete circular double link list.
Task-13:
Give an efficient algorithm for concatenating two doubly linked lists L and M, with head and tail preserved
nodes, into a single list that contains all the nodes of L followed by all the nodes of M