0% found this document useful (0 votes)
12 views17 pages

DSA Lab Journal 2

DSA lab journal 2

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)
12 views17 pages

DSA Lab Journal 2

DSA lab journal 2

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/ 17

Lab Journal 3

Data Structure & Algorithm Lab

Variants of Linked List

Lab Journal – 03

1
Lab Journal 3

1. Objectives
The objective of this lab is to introduce people about the different variant of link list.

By the end of this lab session, students are expected to:

• Understand Random insertion in singly linked list.


• Understand circular linked list.
• Understand different operations performed on circular linked list.
• Concept and basic operations of dummy head node in the link list.

2 Tasks
2.1 Write a program to insert a node in a link list at some arbitrary position.
Steps:
• Create a Node
• Set the node data values
• Break pointer connection
• Re-connect the pointers

Code:
#include<iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

Node* head;
Node* tail;

void insert_arb(int val_pos, int val) {


Node* ptr;
ptr = new Node;
ptr->data = val;
ptr->next = NULL;

if (head == NULL) {
cout << "List is empty! " << endl;
exit(0);
}

Node* temp;
temp = head;

while (temp != NULL && temp->data != val_pos) {


temp = temp->next;
}

2
Lab Journal 3
if (temp == NULL) {
cout << "Node not found! " << endl;
exit(0);
}

ptr->next = temp->next;
temp->next = ptr;
}

void display() {
cout << "Nodes in the link list are: " << endl;
Node* temp;
temp = head;
if (head != NULL) {
while (1) {
cout << temp->data << endl;
temp = temp->next;
if (temp == NULL)
break;
}
}

else
cout << "No element in the node! " << endl;
}

int main() {
Node* ptr1, * ptr2, * ptr3, * ptr4;
ptr1 = new Node;
ptr1->data = 23;
ptr1->next = NULL;

ptr2 = new Node;


ptr2->data = 15;
ptr2->next = NULL;

ptr3 = new Node;


ptr3->data = 45;
ptr3->next = NULL;

ptr4 = new Node;


ptr4->data = 57;
ptr4->next = NULL;

head = ptr1;
ptr1->next = ptr2;
ptr2->next = ptr3;
ptr3->next = ptr4;
tail = ptr4;

display();
insert_arb(45, 69);
cout << endl;
display();

3
Lab Journal 3

Output:

2.2 Define an additional utility function as a part of above program, to check the
presence of a particular element in a list. Try solving this problem by defining a non-
member function that takes a head pointer and a value to search. The function
should return a Boolean result.

Code:
#include<iostream>
using namespace std;

class Node {
public:
int data;
Node* next;

void insert_arb(int, int);


void display();

};

Node* head;
Node* tail;

void Node:: insert_arb(int val_pos, int val) {


Node* ptr;
ptr = new Node;
ptr->data = val;
ptr->next = NULL;

if (head == NULL) {
cout << "List is empty! " << endl;
exit(0);
}

Node* temp;
temp = head;

while (temp != NULL && temp->data != val_pos) {


temp = temp->next;
}

4
Lab Journal 3
if (temp == NULL) {
cout << "Node not found! " << endl;
exit(0);
}

ptr->next = temp->next;
temp->next = ptr;
}

void Node:: display() {


cout << "Nodes in the link list are: " << endl;
Node* temp;
temp = head;
if (head != NULL) {
while (1) {
cout << temp->data << endl;
temp = temp->next;
if (temp == NULL)
break;
}
}

else
cout << "No element in the node! " << endl;
}

bool check_node(Node *head, int val) {


if (head == NULL) {
cout << "No list found! " << endl;
exit(0);
}
Node* t = head;

while (t != NULL){
if (t->data == val) {
cout << "Node found! " << endl;
return true;
exit(0);
}
t = t->next;
}

cout << "Node not found! " << endl;


return false;

int main() {
Node* ptr1, * ptr2, * ptr3, * ptr4;
ptr1 = new Node;
ptr1->data = 23;
ptr1->next = NULL;

ptr2 = new Node;


ptr2->data = 15;
ptr2->next = NULL;

ptr3 = new Node;


ptr3->data = 45;
ptr3->next = NULL;

ptr4 = new Node;


ptr4->data = 57;

5
Lab Journal 3
ptr4->next = NULL;

head = ptr1;
ptr1->next = ptr2;
ptr2->next = ptr3;
ptr3->next = ptr4;
tail = ptr4;

Node node;
node.display();
node.insert_arb(45, 69);
cout << endl;
node.display();

check_node(head, 45);

Output:

Exercise 1
Implement the (class) Linked List with Dummy head node to create a list of integers. You need to provide
the implementation of the member functions as described in the following.

head ? 1 2 2 3

class HList
{
Private:
Node ∗ head;
public :
HList ();

// Checks if the list is empty or not bool

6
Lab Journal 3

emptyList ();

// Inserts a new node with value” newV” &

// after node with value” oldV” in the list

void insert after (int oldV, int newV);

// Inserts a new node at the start of the list

void insert begin (int value);

// Inserts a new node at the end of the list

void insert end (int value);

// Deletes a node of value” val” from the list

void delete Node (int val);

// Deletes a node from the beginning of the list

void delete begin ();

//Deletes a node from the end of the list

void delete end ();


// Displays the values stored in the list
void traverse ();
};

7
Lab Journal 3

Code:
#include<iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
};

class HList {
private:
Node* head;
public:
HList() {
head = NULL;
}

void set_head(Node* node) {


head = node;
};

bool emptyList() {
Node* ptr;
ptr = head->next;

if (ptr->next == NULL) {
cout << "The list is empty! " << endl;
return true;
}

else
cout << "The list is not empty! " << endl;
return false;
}

void insert_after(int oldV, int newV) {


Node* ptr;
ptr = new Node;
ptr->data = newV;
Node* t = head->next;

if (head->next == NULL) {
cout << "List not found!" << endl;
exit(0);
}

while (t != NULL && t->data != oldV) {


t = t->next;
}

if(t == NULL){
cout << " Node not found! " << endl;
exit(0);
}

ptr->next = t->next;
t->next = ptr;
}

void insert_begin(int value) {


Node* ptr;
8
Lab Journal 3
ptr = new Node;
ptr->data = value;
ptr->next = NULL;
if (head->next == NULL) {
head->next = ptr;
}
else{
Node* t = head->next;
ptr->next = t->next;
t->next = ptr;
}
}

void insert_end(int value) {


Node* ptr;
ptr = new Node;
ptr->data = value;
ptr->next = NULL;
if (head->next == NULL) {
head = ptr;
}

else{

Node* t = head->next;
while (t->next != NULL) {
t = t->next;
}
t->next = ptr;
}
}

void delete_Node(int value) {


Node* prev = head;
Node* current = head->next;

if (head->next == NULL) {
cout << "No list found!" << endl;
exit(0);
}

while (current->next != NULL && current->data != value) {


prev = current;
current = current->next;
}

if (current == NULL) {
cout << "Node not found! " << endl;
exit(0);
}
prev->next = current->next;
delete(current);
}

void delete_begin() {
Node* prev = head;
Node* t = head->next;
prev->next = t->next;
delete (t);
}

void delete_end() {
Node* prev = head;
Node* t = head->next;
while (t->next != NULL) {
prev = t;

9
Lab Journal 3
t = t->next;
}
delete(t);
prev->next = NULL;
}

void traverse() {
cout << "Nodes in the link list are: " << endl;
Node* temp;
temp = head->next;
if (temp != NULL) {
while (1) {
cout << temp->data << endl;
temp = temp->next;
if (temp == NULL)
break;
}
}

else
cout << "No element in the node! " << endl;
}
};

int main() {
Node* d_ptr, * ptr1, * ptr2, * ptr3, * ptr4;
d_ptr = new Node;
d_ptr->data = NULL;
d_ptr->next = NULL;

ptr1 = new Node;


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

ptr2 = new Node;


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

ptr3 = new Node;


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

ptr4 = new Node;


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

HList head;
head.set_head(d_ptr);
d_ptr->next = ptr1;
ptr1->next = ptr2;
ptr2->next = ptr3;
ptr3->next = ptr4;

head.emptyList();
head.traverse();
head.insert_after(2, 5);
head.traverse();
head.insert_begin(6);
head.traverse();
head.insert_end(7);
head.traverse();
head.delete_Node(5);
head.traverse();
head.delete_begin();
head.traverse();

10
Lab Journal 3
head.delete_end();
head.traverse();
}

Output:

11
Lab Journal 3

Exercise 2
Implement the (class) Circular Linked List to create a list of integers. You need to provide the
implementation of the member functions as described in the following.

class CList
{ private
:
Node ∗head ;
public :
CList ();

// Checks if the list is empty or not


bool emptyList ();
// Inserts a new node with value ”value”
// at position” pos” in the list //
(restrict user from entering
pos=1) void insert-at (int pos, int
value);
// Inserts a new node at the start of the
list
void insert-begin (int value);
// Inserts a new node at the end of the list
void insert end (int value);
// Deletes a node from position ” pos”
void delete-Node (int pos);
// Deletes a node from the beginning of the
list void delete-begin ();
// Deletes a node from the end of the list
void delete-end ();
// Displays the values stored in the list
void traverse ();
};

12
Lab Journal 3

Code:
#include<iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
};

class CList {
private:
Node* head;
public:
CList() {
head = NULL;
}

void set_head(Node* node) {


head = node;
};

bool emptyList() {
Node* ptr;
ptr = head->next;

if (ptr->next == NULL) {
cout << "The list is empty! " << endl;
return true;
}

else
cout << "The list is not empty! " << endl;
return false;
}

void insert_after(int oldV, int newV) {


Node* t = head;
if (head == NULL) {
cout << "No List found!" << endl;
exit(0);
}
else if(head != NULL){
if (head->data == oldV) {
Node* ptr = new Node;
ptr->data = newV;
ptr->next = t->next;
t->next = ptr;
//exit(0);
}
else if (head->data != oldV){
do {
t = t->next;
} while (t->data != oldV && t != head);

if (t->data == oldV) {
Node* ptr = new Node;
ptr->data = newV;
ptr->next = t->next;
t->next = ptr;
}
else {

13
Lab Journal 3
cout << "Node not found! " << endl;
}

}
}

void insert_begin(int value) {


Node* ptr;
ptr = new Node;
ptr->data = value;
ptr->next = NULL;
if (head == NULL) {
head = ptr;
ptr->next = head;
}
else{
Node* t = head;
while (t->next != head) {
t = t->next;
}
t->next = ptr;
ptr->next = head;
head = ptr;
}
}

void insert_end(int value) {


Node* ptr;
ptr = new Node;
ptr->data = value;
ptr->next = NULL;
if (head == NULL) {
head = ptr;
ptr->next = head;
}
else {
Node* t = head;
while (t->next != head) {
t = t->next;
}
t->next = ptr;
ptr->next = head;
}
}

void delete_Node(int value) {


Node* temp = head;
Node* t = NULL;

if (head == NULL) {
cout << "No list found!" << endl;
exit(0);
}
else {
if (head->data == value) {
do {
t = temp;
temp = temp->next;
} while (temp != head);
head = head->next;
delete temp;
t->next = head;
}
else {

14
Lab Journal 3
do {
t = temp;
temp = temp->next;
} while (temp->data != value && temp != head);
if (temp != head) {
t->next = temp->next;
delete temp;
}
else {
cout << "Node not found!";
exit(0);
}

}
}
}

void delete_begin() {
Node* t = head;
if (head == NULL) {
cout << "List is empty! " << endl;
exit(0);
}

if (head->next == head) {
head = NULL;
delete t;
exit(0);
}

else {
Node* t1 = head;
while (t->next != head) {
t = t->next;
}
head = head->next;
t->next = head;
delete t1;
}
}

void delete_end() {
Node* t = head;
if (head == NULL) {
cout << "List is empty! " << endl;
exit(0);
}

if (head->next == head) {
head = NULL;
delete t;
exit(0);
}

else {
Node* t1 = NULL;
while (t->next != head) {
t1 = t;
t = t->next;
}
t1->next = t->next;
delete t;
}
}

void traverse() {

15
Lab Journal 3
cout << "Nodes in the link list are: " << endl;
Node* temp;
temp = head;
if (head != NULL) {
while (1) {
cout << temp->data << endl;
temp = temp->next;
if (temp == head)
break;
}
}

else
cout << "No element in the node! " << endl;
}
};

int main() {
Node* ptr0, * ptr1, * ptr2, * ptr3, * ptr4;
ptr0 = new Node;
ptr0->data = 2;
ptr0->next = NULL;

ptr1 = new Node;


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

ptr2 = new Node;


ptr2->data = 7;
ptr2->next = NULL;

ptr3 = new Node;


ptr3->data = 8;
ptr3->next = NULL;

ptr4 = new Node;


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

CList head;
head.set_head(ptr0);
ptr0->next = ptr1;
ptr1->next = ptr2;
ptr2->next = ptr3;
ptr3->next = ptr4;
ptr4->next = ptr0;
head.emptyList();
head.traverse();
head.insert_after(2, 4);
head.traverse();
head.insert_begin(6);
head.traverse();
head.insert_end(9);
head.traverse();
head.delete_Node(5);
head.traverse();
head.delete_begin();
head.traverse();
head.delete_end();
head.traverse();
}

16
Lab Journal 3

Output:

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

17

You might also like