DSA Lab Journal
DSA Lab Journal
Lab Journal – 04
(OC-Lab 02)
Objectives:
DOUBLY LINKLIST:
A doubly linked list is a list that contains links to next and previous nodes. Unlike singly linked lists
where traversal is an only one way, doubly linked list allows traversals in both ways. In doubly linked
list each node contains address of next node except end node. Each node contains address of previous
node except first node.
TASK # 01:
a. Write a code for to insert a node in a doubly link list shown below.
Head
15 20
temp
Solution:
Code: void insert(int key, int value) {
Node* ptr = new Node;
ptr->data = value;
ptr->next = NULL;
ptr->prev = NULL;
Node* temp = head;
if (temp->data != key) {
cout << "\nNode not found!" << endl;
exit(0);
}
if (temp == tail) {
temp->next = ptr;
ptr->prev = temp;
tail = ptr;
exit(0);
}
temp->next->prev = ptr;
ptr->next = temp->next;
temp->next = ptr;
ptr->prev = temp;
};
b. Write a code for to delete a node in a doubly link list shown below:
Ptr
15 17 20
Solution:
if (ptr == head) {
head = head->next;
}
else {
ptr->prev->next = ptr->next;
}
if (ptr == tail) {
tail = tail->prev;
}
else {
ptr->next->prev = ptr->prev;
}
delete ptr;
};
TASK # 02:
Implement a class doubly link list to create a list of integers. You need to provide the implementation of member
functions listed below. Your driver function should give you a choice to call following user-defined functions.
Class doublylist
private:
int data;
Node* head;
public:
doublylist ();
delete_first();
delete_last();
delete_userspecific(int key);
display_list_forward ();
display_list_backward ();
YOUR OUTPUT SHOULD BE SIMILAR TO AS SHOWN BELOW:
class Node {
public:
int data;
Node* next;
Node* prev;
};
class Dlist {
public:
Node* head, *tail;
Dlist() {
head = nullptr;
tail = nullptr;
}
if (head == NULL) {
head = ptr;
tail = ptr;
}
else{
ptr->next = head;
head->prev = ptr;
head = ptr;
}
};
void insert_last(int value) {
Node* ptr = new Node;
ptr->data = value;
ptr->next = NULL;
ptr->prev = NULL;
if (head == NULL) {
head = ptr;
tail = ptr;
}
else {
Node* t = tail;
t->next = ptr;
ptr->prev = t;
tail = ptr;
}
};
void insert_specific(int key, int value) {
if (head == NULL) {
cout << "\nList is empty!" << endl;
return;
}
if (t->data != key) {
cout << "\nNode not found!" << endl;
exit(0);
}
if (t == tail) {
t->next = ptr;
ptr->prev = t;
tail = ptr;
exit(0);
}
t->next->prev = ptr;
ptr->next = t->next;
t->next = ptr;
ptr->prev = t;
};
void delete_first() {
if (head == NULL) {
cout << "List is empty! " << endl;
return;
}
Node* t = head;
if (head->next == tail) {
tail = NULL;
}
else{
head->next->prev = NULL;
}
head = head->next;
delete t;
};
void delete_last() {
if (head == NULL) {
cout << "List is empty! " << endl;
return;
}
Node* t = tail;
if (head->next == tail) {
head = NULL;
}
else {
tail->prev->next = NULL;
}
tail = tail->prev;
delete t;
};
if (t == head) {
head = head->next;
}
else {
t->prev->next = t->next;
}
if (t == tail) {
tail = tail->prev;
}
else {
t->next->prev = t->prev;
}
delete t;
};
void display_list_forward() {
Node * t = head;
cout << endl;
while (t != NULL) {
cout << t->data << endl;
t = t->next;
}
};
void display_list_backward() {
Node* t = tail;
cout << endl;
while (t != NULL) {
cout << t->data << endl;
t = t->prev;
}
};
};
int main() {
Dlist obj;
while(1){
cout << "Select from the following:" << endl;
cout << "1. Insert new node at the beginning. \n2. Insert new node in the last. \n3. Insert node at specific location. \n4. Delete first node. \
n5. Delete last node. \n6. Delete node at specifi location. \n7. Display list in forward direction. \n8. Display list in backward direction. \n9.Exit "
<< endl;
int a;
cin >> a;
if (a == 1) {
int x;
cout << "Enter value for the node:" << endl;
cin >> x;
obj.insert_first(x);
cout << "\nNode added in the front! " << endl;
}
else if (a == 2) {
int x;
cout << "Enter value for the node:" << endl;
cin >> x;
obj.insert_last(x);
cout << "\nNode added at the back! " << endl;
}
else if (a == 3) {
int x,y;
cout << "Enter value for the node:" << endl;
cin >> x;
cout << "Enter the node after you want to insert:" << endl;
cin >> y;
obj.insert_specific(y,x);
cout << "\nNode added at the desired location! " << endl;
}
else if (a == 4) {
obj.delete_first();
cout << "\nFirst node deleted! " << endl;
}
else if (a == 5) {
obj.delete_last();
cout << "\nLast node deleted! " << endl;
}
else if (a == 6) {
int x;
cout << "Enter value for the node:" << endl;
cin >> x;
obj.delete_userspecific(x);
cout << "\nNode deleted at the desired location! " << endl;
}
else if (a == 7) {
cout << "\nList in forward direction: ";
obj.display_list_forward();
}
else if (a == 8) {
cout << "\nList in backward direction: ";
obj.display_list_backward();
}
else if (a == 9) {
break;
}
else {
cout << "Enter valid value!" << endl;
}
cout << "\n\n";
}
}
Output:
EXERCISE # 01:
Code:
int Max_find() {
Node* temp = head;
int val = 0;
val = temp->data;
while (temp != NULL) {
if (temp->data > val) {
val = temp->data;
}
temp = temp->next;
}
return val;
}
int Node_count() {
Node* temp = head;
int count = 0;
while (temp != NULL) {
temp = temp->next;
count++;
}
return count;
}
Output:
EXERCISE # 02:
Use a doubly linked list to implement the tasks below. Each node of a linked list will have a data item
which will represent the record of employee.
Write definition for the member functions for all the user-defined classes implementation. Link list
after 5 insertions:
class Employee
{
public:
string ID;
int salary;
int bonus;
int yearOfJoining;
Employee()
{
ID = "";
salary = 0;
yearOfJoining = 0;
bonus = 0;
}
};
class node
{
public:
Employee emp;
node* next;
node* prev;
};
class Dlink
{
private:
node* head;
public:
Dlink()
{ }
bool isEmpty()
{ }
void insert_beg(string ID,int salary,int bonus,int yearOfJoining)
{ }
void delete_end()
{ }
void traverse()
{ }
};
int main()
{
Dlink obj;
return 0;
}
Output screenshot:
Code:
#include <iostream>
#include <conio.h>
class Employee
{
public:
string ID;
int salary;
int bonus;
int yearOfJoining;
Employee()
{
ID = "";
salary = 0;
yearOfJoining = 0;
bonus = 0;
}
};
class Node
{
public:
Employee emp;
Node* next;
Node* prev;
};
class Dlink
{
private:
Node* head;
Node* tail;
public:
Dlink()
{
head = nullptr;
tail = nullptr;
}
bool isEmpty()
{
if (head == NULL) {
return true;
}
else
return false;
}
if (head == NULL) {
head = ptr;
tail = ptr;
}
else {
ptr->next = head;
head->prev = ptr;
head = ptr;
}
}
if (head == NULL) {
head = ptr;
tail = ptr;
}
else {
Node* t = tail;
t->next = ptr;
ptr->prev = t;
tail = ptr;
}
void delete_beg()
{
if (head == NULL)
return;
Node* temp = head;
head = head->next;
if (head != NULL)
head->prev = NULL;
delete temp;
}
void delete_end()
{
if (head == NULL)
return;
Node* temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
if (temp->prev != NULL)
temp->prev->next = NULL;
delete temp;
}
void traverse()
{
Node* temp = head;
Output:
Implement the given exercises and get them checked by your instructor.
S No. Exercise Checked By:
1. Exercise 1
2. Exercise 2