0% found this document useful (0 votes)
14 views9 pages

DSA Lab Journal

DSA lab journal

Uploaded by

stife benz
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)
14 views9 pages

DSA Lab Journal

DSA lab journal

Uploaded by

stife benz
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/ 9

Data Structure & Algorithm Lab

Singly Linked List

Lab Journal – 02
(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:

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() {

Node *node1, * node2;

node1 = new Node;


node1->data = 5;
node1->next = NULL;

node2 = new Node;


node2->data = 10;
node2->next = NULL;

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

Node* head = NULL;


Node* tail = NULL;
Lab Journal 2

void insert_node(int Data) {


Node* node;
node = new Node;
node->data = Data;
node->next = NULL;

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

Node* head = NULL;


Node* tail = NULL;

void insert_node(int Data) {


Node* node;
node = new Node;
node->data = Data;
node->next = NULL;

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();

cout << "\nDeleting Nodes\n" << endl;

delete_node(); //deleting first node


display_node();
cout << endl;

delete_node(); //deleting second node


display_node();
cout << endl;

delete_node(); //deleting third node


display_node();
cout << endl;

delete_node(); //deleting fourth node


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

Node* head = NULL;


Node* tail = NULL;

void InsertNode(int Data) {


Node* node;
node = new Node;
node->data = Data;
node->next = NULL;

if (head == NULL)
head = node;

else
tail->next = node;

tail = node;
}

void InsertNode_atHead(int Data) {


Node* node;
node = new Node;
node->data = Data;
node->next = head;
head = node;

void delete_node() {
Node* temp;
temp = head;
head = head->next;
free(temp);
}

void delete_user_specific_node(int Data) {


Node* current_node = head;
Node* last_node = head;
while (1)
{
if (current_node->data == Data)
4
Lab Journal 2
{
if (current_node == head)
head = 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;
}

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 = new Node;


node1->data = 1;
node1->next = NULL;

node2 = new Node;


node2->data = 2;
node2->next = NULL;

node3 = new Node;


node3->data = 3;
node3->next = NULL;

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:

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy