0% found this document useful (0 votes)
27 views11 pages

LAB 05 - Link List 19102023 024021pm 09102024 011547pm

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views11 pages

LAB 05 - Link List 19102023 024021pm 09102024 011547pm

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Data Structure & Algorithm

Lab
Singly Linked List

Lab Journal – 05
(OC-Lab 01)
Objective
By the end of this lab session, students are expected to:

 This lab session is aimed at introducing students to singly linked


list.
 Students will be required to implement the Singly Linked List ADT.
 In addition, student will also develop a number of utility functions
to manipulate a given singly linked lists.

Task 1: Given the following linked list, state output of the following statements.

Solution:
1
3
3

Task 2: Redraw the above list after given instructions are executed.

Solution:

2
Lab Journal – Lab 3

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:
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
int main() {
Node a, b;
a.data = 11;
b.data = 12;
a.next = &b;
b.next = 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 lastnode.
Solution:
#include<iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
static Node* head;
static Node* tail;

void insert_node(int data) {


Node* ptr = new Node;
ptr->data = data;
ptr->next = nullptr;

if (head == nullptr) {
head = ptr;
tail = ptr;
}
else {

tail->next = ptr;
tail = ptr;
}
}
static void display_node() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
cout << endl;
}
}
};

Node* Node::head = nullptr;


Node* Node::tail = nullptr;

int main() {
Node node;
node.insert_node(11);
node.insert_node(12);
node.insert_node(13);
node.insert_node(14);
node.insert_node(15);
Node::display_node();

return 0;
}

Exercise 02:
Write code for the “delete_node ()” function to delete the head_node of a any 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:
#include<iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
static Node* head;
static Node* tail;

void insert_node(int data) {


Node* ptr = new Node;
ptr->data = data;
ptr->next = nullptr;

if (head == nullptr) {
head = ptr;
tail = ptr;
}
else {

tail->next = ptr;
tail = ptr;
}
}
static void display_node() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
cout << endl;
}
}
static void delete_node() {
Node* temp = head;
head = head->next;
delete temp;

}
};

Node* Node::head = nullptr;


Node* Node::tail = nullptr;

int main() {
Node node;
node.insert_node(11);
node.insert_node(12);
node.insert_node(13);
node.insert_node(14);
node.insert_node(15);
Node::display_node();
for (int i = 0; i < 5; i++) {
Node::delete_node();
cout << "List after deletion: ";
cout << endl;
Node::display_node();
}
return 0;
}
5

Exercise 03:
Write a program to implement link list completely, 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:

#include<iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
static Node* head;
static Node* tail;

void insert_node_at_end(int data) {


Node* ptr = new Node;
ptr->data = data;
ptr->next = nullptr;

if (head == nullptr) {
head = ptr;
tail = ptr;
}
else {
tail->next = ptr;
tail = ptr;
}
}
void insert_node_at_start(int data) {
Node* ptr = new Node;
ptr->data = data;
ptr->next = head;

head = ptr;
if (tail == nullptr) {
tail = ptr;
}
}

static void display_node() {


Node* temp = head;
if (temp == nullptr) {
cout << "List is empty!" << endl;
return;
}
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}

static void delete_head_node() {


if (head == nullptr) {
cout << "List is empty!" << endl;
return;
}
Node* temp = head;
head = head->next;
delete temp;

if (head == nullptr) {
tail = nullptr;
}
}

static void delete_user_specific_node(int value) {


if (head == nullptr) {
cout << "List is empty!" << endl;
return;
}

if (head->data == value) {
delete_head_node();
return;
}

Node* prev = nullptr;


Node* temp = head;

while (temp != nullptr && temp->data != value) {


prev = temp;
temp = temp->next;
}

if (temp == nullptr) {
cout << "Node with value " << value << " not found." << endl;
return;
}

prev->next = temp->next;
if (temp == tail) {
tail = prev;
}
delete temp;
}
};

Node* Node::head = nullptr;


Node* Node::tail = nullptr;

int main() {
Node list;

list.insert_node_at_end(11);
list.insert_node_at_end(12);
list.insert_node_at_end(13);
list.insert_node_at_end(14);
list.insert_node_at_end(15);

list.insert_node_at_start(10);

cout << "List after inserting nodes: " << endl;


Node::display_node();

Node::delete_head_node();
cout << "List after deleting the head node: " << endl;
Node::display_node();

Node::delete_user_specific_node(13);
cout << "List after deleting node with value 13: " << endl;
Node::display_node();

Node::delete_user_specific_node(100);
cout << "List after trying to delete non-existing node: " << endl;
Node::display_node();

return 0;
}

Exercise 04: You can do that as your homework (Storing Records in a List)

Write a program to store student’s information. Such that a list of students has to be created in
such a way, that each node of the list contains student ID along with the name and enrollment
number. Along with other basic list functions, also write a function that allows the user to
search a student in a list by using his ID and then displays the name of the student, if found.

#include<iostream>
#include<string>
using namespace std;
class Student {
public:
int studentID;
string name;
string enrollmentNumber;
Student* next;

static Student* head;

void insert_student(int id, string name, string enrollment) {


Student* newStudent = new Student;
newStudent->studentID = id;
newStudent->name = name;
newStudent->enrollmentNumber = enrollment;
newStudent->next = nullptr;

if (head == nullptr) {
head = newStudent;
}
else {
Student* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newStudent;
}
}

static void display_students() {


if (head == nullptr) {
cout << "No students in the list." << endl;
return;
}
Student* temp = head;
while (temp != nullptr) {
cout << "ID: " << temp->studentID << ", Name: " << temp->name << ", Enrollment: "
<< temp->enrollmentNumber << endl;
temp = temp->next;
}
}

static void search_student_by_id(int id) {


if (head == nullptr) {
cout << "The list is empty." << endl;
return;
}

Student* temp = head;


while (temp != nullptr) {
if (temp->studentID == id) {
cout << "Student found: " << temp->name << endl;
return;
}
temp = temp->next;
}

cout << "Student with ID " << id << " not found." << endl;
}
};

Student* Student::head = nullptr;

int main() {
Student list;

list.insert_student(21, "Ranjha", "ENR021");


list.insert_student(120, "Hanzi", "ENR120");
list.insert_student(263, "awais", "ENR263");

cout << "List of students: " << endl;


Student::display_students();

int searchID;
cout << "Enter student ID to search: ";
cin >> searchID;
Student::search_student_by_id(searchID);

return 0;

*******************************END OF LAB JOURNAL***************************

You might also like