0% found this document useful (0 votes)
23 views19 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)
23 views19 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/ 19

Data Structure & Algorithm Lab

Doubly Linked List

Lab Journal – 04
(OC-Lab 02)
Objectives:

 To understand the difference between singly and doubly link list.


 To understand the concept of predecessor and successor nodes in a link list.
 To learn how nodes are connected/linked together with the help of previous and next pointers.
 To insert nodes in the beginning, middle and end.
 To delete nodes from the beginning, middle and end.

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;

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


temp = temp->next;
}

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:

void delete(int key) {


Node* ptr = head;
while (ptr->data != key) {
ptr = ptr->next;
if (ptr == NULL) {
cout << "Node not found!" << endl;
return;
}
}

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

void insert_first(int value);


void insert_last(int value);
void insert_specific(int key, int value);

delete_first();

delete_last();

delete_userspecific(int key);

display_list_forward ();

display_list_backward ();
YOUR OUTPUT SHOULD BE SIMILAR TO AS SHOWN BELOW:

LOGIC FLOW: (INSERTION AT THE BEGINNING)


LOGIC FLOW: (INSERTING A USER SPECIFIC NODE (NODE AFTER A NODE)

LOGIC FLOW: (DELETING ITEM WITH A GIVEN KEY)


Code:
#include<iostream>
using namespace std;

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

class Dlist {
public:
Node* head, *tail;

Dlist() {
head = nullptr;
tail = nullptr;
}

void insert_first(int value) {


Node* ptr = new Node;
ptr->data = value;
ptr->next = NULL;
ptr->prev = NULL;

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

Node* ptr = new Node;


ptr->data = value;
ptr->next = NULL;
ptr->prev = NULL;
Node* t = head;

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


t = t->next;
}

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

void delete_userspecific(int key) {


Node* t = head;
while (t->data != key) {
t = t->next;
if (t == NULL) {
cout << "Node not found!" << endl;
return;
}
}

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:

Create a link list


with
additional functions such that the first one finds out the maximum value in a list and the second one counts and
returns the total number of nodes in a link list.
Your functions to be defined are:
1. Max_find();
2. Node_count();

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:

Complete and implement the program.


Employee class implementation of Doubly Linked List
#include <iostream>
#include <conio.h>

using namespace std;

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 insert_end(string ID, int salary, int bonus, int yearOfJoining)


{ }
void delete_beg()
{ }

void delete_end()
{ }

void delete_specific(string value)


{ }

void traverse()
{ }

};
int main()
{
Dlink obj;

return 0;
}
Output screenshot:

Code:
#include <iostream>
#include <conio.h>

using namespace std;

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

void insert_beg(string ID, int salary, int bonus, int yearOfJoining)


{
Node* ptr = new Node;
ptr->emp.ID = ID;
ptr->emp.salary = salary;
ptr->emp.bonus = bonus;
ptr->emp.yearOfJoining = yearOfJoining;
ptr->next = NULL;
ptr->prev = NULL;

if (head == NULL) {
head = ptr;
tail = ptr;
}
else {
ptr->next = head;
head->prev = ptr;
head = ptr;
}
}

void insert_end(string ID, int salary, int bonus, int yearOfJoining)


{
Node* ptr = new Node;
ptr->emp.ID = ID;
ptr->emp.salary = salary;
ptr->emp.bonus = bonus;
ptr->emp.yearOfJoining = yearOfJoining;
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 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 delete_specific(string value)


{
Node* temp = head;
while (temp != NULL && temp->emp.ID != value)
{
temp = temp->next;
}
if (temp == NULL || temp->next == NULL)
return;
Node* t = temp->next;
temp->next = t->next;
if (t->next != NULL)
t->next->prev = temp;
delete t;
}

void traverse()
{
Node* temp = head;

while (temp != NULL)


{
cout << "ID: " << temp->emp.ID << "\nSalary: " << temp->emp.salary
<< "\nBonus: " << temp->emp.bonus << "\nYear of Joining: " << temp->emp.yearOfJoining << endl;
temp = temp->next;
}
}
};
int main()
{
Dlink obj;
obj.insert_beg("Abdullah", 30000, 4000, 2020);
obj.insert_end("Azib", 25000, 2000, 2019);
obj.insert_end("Haider", 15000, 5000, 2017);
obj.insert_end("Agha", 37000, 7000, 2010);
obj.insert_end("Ahmed", 15000, 2000, 2023);
obj.traverse();
obj.delete_end();
cout << "\n\nAfter deletion the list is:" <<"\n" << endl;
obj.traverse();

Output:
Implement the given exercises and get them checked by your instructor.
S No. Exercise Checked By:
1. Exercise 1

2. Exercise 2

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

You might also like