DSA Lab Journal 2
DSA Lab Journal 2
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.
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;
if (head == NULL) {
cout << "List is empty! " << endl;
exit(0);
}
Node* temp;
temp = head;
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;
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;
};
Node* head;
Node* tail;
if (head == NULL) {
cout << "List is empty! " << endl;
exit(0);
}
Node* temp;
temp = head;
4
Lab Journal 3
if (temp == NULL) {
cout << "Node not found! " << endl;
exit(0);
}
ptr->next = temp->next;
temp->next = ptr;
}
else
cout << "No element in the node! " << endl;
}
while (t != NULL){
if (t->data == val) {
cout << "Node found! " << endl;
return true;
exit(0);
}
t = t->next;
}
int main() {
Node* ptr1, * ptr2, * ptr3, * ptr4;
ptr1 = new Node;
ptr1->data = 23;
ptr1->next = NULL;
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 ();
6
Lab Journal 3
emptyList ();
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;
}
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;
}
if (head->next == NULL) {
cout << "List not found!" << endl;
exit(0);
}
if(t == NULL){
cout << " Node not found! " << endl;
exit(0);
}
ptr->next = t->next;
t->next = ptr;
}
else{
Node* t = head->next;
while (t->next != NULL) {
t = t->next;
}
t->next = ptr;
}
}
if (head->next == NULL) {
cout << "No list found!" << endl;
exit(0);
}
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;
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 ();
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;
}
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;
}
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;
}
}
}
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;
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:
17