Lab 05
Lab 05
Lab 05
Note:
Maintain discipline during the lab.
Listen and follow the instructions as they are given.
Just raise 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:
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not
stored at a contiguous location; the elements are linked using pointers.
The singly Linked List class will only have a public Node type pointer variable “head” to point
to the first node of the list. Together with the help of default and parameterized constructor the
head’s value will be manipulated in the singly Linked List class.
//Node Object
Class Node
{
//public members: key, data, next
Node ()
{
//initialize both key and data with zero while next pointer with NULL.
}
Node (int k, int d)
{
//assign the data members initialized in default constructor to these arguments.
}
};
//SinglyLinkedList Object
Class SinglyLinkedList
{
// create head node as a public member
//Default Constructor
SinglyLinkedList()
{
//initialize the head pointer will NULL;
}
//Parameterized Constructor with node type ‘n’ pointer
SinglyLinkedList (Node* n)
{
//Assign the head pointer to value of pointer n;
}
};
//Creating a Node type helper Function named node Exists (argument key)
Node nodeExists (int k)
{
//temporary pointer var to hold Null value;
//node type pointer to hold head pointers value;
// loop condition (Traverse through the node until the ptr variable points to null)
{
If the ptr variable’s key = passed key argument
{
//assign the pointer ptr to temp variable;
}
Else
{
//assign the pointer ptr to point to the next pointer’s value.
}
return the temp variable;
}
//Append Function
Void appendNode (Node* n, int data)
{
//create new node and assign data to it.
//check if the head pointer points to Null or not with a condition
if (head == NULL)
{
//If it does, assign the head pointer the passed pointer ‘n’. This will put the address of
node n in the head pointer.
}
// Else traverse through the list to find the next pointer holding Null as address (last node)
Else
{
if (nodeExists(n->key) != NULL ) {
// Print an intimation that a node holding passed key already exists.
}
else
{
If (head != NULL)
{
// assign the head pointer to a new pointer of type Node (say, ptr).
//loop through the list using ptr until the next of any node contains null.
//when ptr->next = NULL. Then assign the n node to ptr->next to append that
node after the last found node.
}
}
}}
Task # 3: Add a node at the front of a Singly Linked List (Prepend a new node)
//Prepending a Node.
void prependNode(Node* n)
{
// checking the value of node's key if the node with that key already exists.
if (nodeExists(n->key) != NULL ) {
// Print an intimation that a node holding passed key already exists.
}
else
{
// new node's next is pointing to the head i.e. address of first node.
// since we have changed the head pointer's value from first node to the new node, now the
new . // head will be pointing to address of new node.
}
}
Consider a singly linked list 4 -> 1 -> 5 - > 7 -> 2 and want to add the value 3 after node 1. The
List should be transformed as.
4 -> 1 -> 3-> 5-> 7 -> 2
To insert a new node after some node, create a void function named insertNodeAfter ()
carrying two arguments, one for the key of the node after which the insertion is to be done, the
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.
void deleteNode(key)
{
// create a Node type pointer to hold head (say, temp)
// create a Node type pointer to hold previous node (say, prev)
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.
void updateNode(key, new_data)
{
// call the nodeExists function and hold the return value in a Node type pointer (say, ptr)
If(ptr!=NULL){
// set ptr’s data as new data
}
Else{
// print a message sating node does not exist.
}
}
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:
Input: 1->0->2->0->1->NULL
Output: Linked List is a Palindrome
Input: B->O->R->R->O->W->O->R->R->O->B->NULL
Output: Linked List is a Palindrome