DSA Lab Journal
DSA Lab Journal
Lab Journal – 02
(OC-Lab 01)
Objective
By the end of this lab session, students are expected to:
Task 1: Given the following linked list, state output of the following statements.
Solution:
Task 2: Redraw the above list after given instructions are executed.
Solution:
Lab Journal 2
Task 3: Write a code snippet to create two nodes with data elements and link them with each
other. (Do not use head pointer here)
Solution:
Code:
#include<iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
int main() {
node1->next = node2;
return (0);
}
Exercise 01: Write a program to create a link list such that 5 nodes are created via an
insert_node () function with integer data. In order to traverse the array, you would need two
pointers i.e. head pointer and tail pointer. Once you are done with node creation, use
display_node () function to print all node elements one after another. In order to display the
elements, you would need an additional pointer let say (temp) to start traversing from the
start until the last node.
Solution:
Code:
#include<iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
if (head == NULL) {
head = node;
}
else
tail->next = node;
tail = node;
void display_node() {
Node* temp;
temp = new Node;
temp = head;
while (1) {
if (temp != NULL) {
cout << temp->data << endl;
temp = temp->next;
}
else
break;
}
int main() {
insert_node(23);
insert_node(24);
insert_node(25);
insert_node(26);
display_node();
return(0);
}
Output:
Exercise 02:
1
Lab Journal 2
Write code for the “delete_node ()” function to delete the head node of a link list. The
deletion process should start from head node until you reach the last node. You would need to
call the function multiple times for the step-by-step delection of nodes. Again, services of any
temporary pointer would be required.
Solution:
Code:
#include<iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
if (head == NULL) {
head = node;
}
else
tail->next = node;
tail = node;
}
void display_node() {
Node* temp;
temp = new Node;
temp = head;
while (1) {
if (temp != NULL) {
cout << temp->data << endl;
temp = temp->next;
}
else
break;
}
}
void delete_node() {
Node* temp;
temp = head;
head = head->next;
free(temp);
}
2
Lab Journal 2
int main() {
insert_node(23);
insert_node(24);
insert_node(25);
insert_node(26);
display_node();
return(0);
}
Output:
Exercise 03:
3
Lab Journal 2
Write a program to implement link list, with respect to insertion of nodes in the start and in
the end. Furthermore, you would need to define two separate functions for deletion of node,
which would delete a node starting from head and other function, which would be
“delete_user_specific_node()” would do any node of your choice.
Solution:
Code:
#include<iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
if (head == NULL)
head = node;
else
tail->next = node;
tail = node;
}
void delete_node() {
Node* temp;
temp = head;
head = head->next;
free(temp);
}
void display_node() {
Node* temp;
temp = new Node;
temp = head;
while (1) {
if (temp != NULL) {
cout << temp->data << endl;
temp = temp->next;
}
else
break;
}
}
int main() {
Node* node1, * node2, * node3;
node1->next = node2;
node2->next = node3;
head = node1;
tail = node3;
cout << "Before inserting new nodes the list is:" << endl;
display_node();
InsertNode_atHead(23);
InsertNode(45);
cout << "\nAfter inserting new nodes the list is:" << endl;
display_node();
delete_node();
cout << "\nAfter deleting head node the list is:" << endl;
5
Lab Journal 2
display_node();
delete_user_specific_node(3);
cout << "\nAfter deleting user specific node the list is:" << endl;
display_node();
return (0);
}
Output: