0% found this document useful (0 votes)
48 views7 pages

01 134231 011 117453104636 21022024 105824pm

The document discusses various tasks involving linked lists including creating nodes and linking them, traversing a linked list to print node elements, inserting nodes at the beginning and end of a list, and defining functions to delete nodes from the head of the list and to delete a specific node. Solutions are provided using C++ code snippets to implement linked lists and functions for insertion and deletion of nodes.

Uploaded by

Armish Aamir
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)
48 views7 pages

01 134231 011 117453104636 21022024 105824pm

The document discusses various tasks involving linked lists including creating nodes and linking them, traversing a linked list to print node elements, inserting nodes at the beginning and end of a list, and defining functions to delete nodes from the head of the list and to delete a specific node. Solutions are provided using C++ code snippets to implement linked lists and functions for insertion and deletion of nodes.

Uploaded by

Armish Aamir
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/ 7

Data Structure & Algorithm Lab

Singly Linked List


NAME=ARMISH AHMED
ENROLLMENT NO=01-134231-011

Lab Journal – 02
(OC-Lab 01)

Task 1: Given the following linked list, state output of the following statements.
Output: 1 3 3
Solution:
Task 2: Redraw the above list after given instructions are executed.

Solution:

2
first 1
3

ptr
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;

Node() {
data = 1;
next = NULL;
}
};

int main() {
Node* p, * q;

p = new Node;
p->data = 10;

q = new Node;
q->data = 11;
q->next = NULL;

p->next = q;
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 lastnode.
Solution:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node() {}
Node* head_ptr = nullptr;
Node* tail_ptr = nullptr;
void InsertNode(int value) {
Node* new_node = new Node;
new_node->data = value;
new_node->next = nullptr;

if (head_ptr == nullptr) {
head_ptr = new_node;
tail_ptr = new_node;
}
else {
tail_ptr->next = new_node;
tail_ptr = new_node;
}
}
void traverse() {
Node* temp = head_ptr;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
}
};

int main() {
Node* p = new Node();
p->InsertNode(10);
p->InsertNode(1);
p->InsertNode(0);
p->InsertNode(15);
p->InsertNode(19);

p->traverse();

}]

Exercise 02:
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:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node() {}
Node* head_ptr = nullptr;
Node* tail_ptr = nullptr;
void InsertNode(int value) {
Node* new_node = new Node;
new_node->data = value;
new_node->next = nullptr;

if (head_ptr == nullptr) {
head_ptr = new_node;
tail_ptr = new_node;
}
else {
tail_ptr->next = new_node;
tail_ptr = new_node;
}
}
void traverse() {
Node* temp = head_ptr;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
}

void DeleteNode()
{
Node* temp_node;
temp_node = head_ptr;
head_ptr = head_ptr->next;
free(temp_node);

};

int main() {
Node* p = new Node();
p->InsertNode(10);
p->InsertNode(1);
p->InsertNode(0);
while (p->head_ptr != nullptr) {
p->DeleteNode();
}
p->traverse();

}
Exercise 03:
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:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next = nullptr;
Node* head_ptr = nullptr;
Node* tail_ptr = nullptr;
void InsertNode(int value) {
Node* new_node = new Node;
new_node->data = value;
new_node->next = nullptr;

if (head_ptr == nullptr) {
head_ptr = new_node;
tail_ptr = new_node;
}
else {
tail_ptr->next = new_node;
tail_ptr = new_node;
}
}
void traverse() {
Node* temp = head_ptr;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
}

void DeleteuNode(int Data)


{
Node* current_node = head_ptr;
Node* last_node = head_ptr;
while (1)
{
if (current_node->data == Data)
{
if (current_node == head_ptr)
head_ptr = current_node->next;
else
last_node->next = current_node->next;
free(current_node);
break;
}
last_node = current_node;
current_node = current_node->next;
if (current_node == NULL)
break;
}
}
};

int main() {
Node* p = new Node();
cout << "Before deletion:" << endl;
p->InsertNode(10);
p->InsertNode(1);
p->InsertNode(5);

p->traverse();
cout << endl;
p->DeleteuNode(1);
cout << "After deletion: " << endl;
p->traverse();
return 0;
}

You might also like