Familiarization With Doubly Linked List and Performing Different Operations On It.

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Chittagong University of Engineering &

Technology

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


NAME of the experiment

Familiarization with Doubly Linked list and performing different


operations on it.

EXPERIMENT NO. : 02
COURSE NO. : CSE-242
COURSE TITLE : Data structure (sessional)
NAME OF THE STUDENT : Towha Elahi
STUDENT ID. : 1904047
REMARKS
GROUP : A2
LEVEL :2
TERM :I
DATE OF EXPERIMENT : 18-07-2022
DATE OF SUBMISSION : 25-07-2022
1

Objectives:
1. To learn about linked list and its types.
2. To learn about doubly linked list and how to create a doubly link list.
3. To learn how to insert and delete node (beginning, middle, end) in doubly
linked list
4. To learn how to traverse a doubly linked list from the beginning and fromthe
end

Description:
Linked list: A linked list is a linear grouping of nodes, or data pieces, where the
linear order is indicated by a pointer. In other words, each node has two parts: the
first portion, known as the element's information field, and the second part, known
as the link field or next pointer field, which holds the address of the node that
follows it in the list. The components of a linked list are not kept in sequential
memory regions but rather in random locations. Each node in the chain is made up
of its own data plus the address of the one after it. In order to build trees and
graphs, Linked Lists are used.
Types of Linked list:
1. Singly Linked List
2. Header Linked List
3. Doubly Linked List
Singly Linked List:
A singly linked list is a form of linked list that may only be traversed in one direction,
from head to final node, and is also unidirectional. A node is the name given to each
entry in a linked list. Data and a pointer to the following node are both contained in
a single node, which aids in keeping the list's structure. The singly linked list is a
data structure made up of two parts: the data component and the address part,
which holds the address of the node that comes after it or succeeds it.

A node's address component is also referred to as its pointer. Assume that there are
three nodes, and their addresses are A, B, and C respectively.

Header linked list:


A header linked list is a linked list that always has a unique node at the top of the
2

list, known as the header node. The two most popular header lists are as follows:
1. A grounded header list is a header list where the last node contains the null
pointer.

2. A circular header list is a header list where the last node points back to the
header node.

Doubly Linked List:


A doubly linked list is a linked data structure that consists of a set of sequentially
linked records called nodes. Each node contains three fields: two link fields
(references to the previous and to the next node in the sequence of nodes) and one
data field:
1. An information field INFO which contains the data of N
2. A pointer field NEXT which contains the location of the next node in the list.
3. A pointer field PREV which contains the location of the preceding node
in the list
The list also requires two list pointer variables: FIRST, which points to the firstnode
in the list, and LAST, which points to the last node in the list.
3

Traversing a Doubly Linked List:


void PrintList() {

Node* temp = head;

if(temp != NULL)
{
cout<<"The list contains: ";
while(temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
else
{
cout<<"The list is empty.\n";
}
}

Insertion in a Doubly Linked List at the beginning:

void push_front(int newElement) {

Node* newNode = new Node();


newNode->data = newElement;
newNode->next = NULL;
newNode->prev = NULL;

if(head == NULL) {
head = newNode;
}
else {
head->prev = newNode;
newNode->next = head;
head = newNode;
}
}
4

Insertion in doubly linked list at the end:

void pop_back() {
if(head != NULL) {
if(head->next == NULL) {
head = NULL;
}
else {
Node* temp = head;
while(temp->next->next != NULL)
temp = temp->next;

Node* lastNode = temp->next;


temp->next = NULL;
free(lastNode);
}
}
}

Insertion in doubly linked list after Specified node:

void push_at(int newElement, int position) {


Node* newNode = new Node();
newNode->data = newElement;
newNode->next = NULL;
newNode->prev = NULL;

if(position < 1) {
cout<<"\nposition should be >= 1.";
} else if (position == 1) {

newNode->next = head;
head->prev = newNode;
head = newNode;
} else {
Node* temp = head;
for(int i = 1; i < position-1; i++) {
if(temp != NULL) {
temp = temp->next;
}
}
if(temp != NULL) {
newNode->next = temp->next;
newNode->prev = temp;
temp->next = newNode;
if(newNode->next != NULL)
newNode->next->prev = newNode;
} else {
cout<<"\nThe previous node is null.";
}
}
}
Deletion in a Doubly Linked List at beginning:
5

void pop_front() {
if(head != NULL) {
Node* temp = head;
}
head = head->next;
free(temp);

if(head != NULL)
head->prev = NULL;
}
}

Deletion in doubly linked list at the end:

void pop_back() {
if(head != NULL) {
if(head->next == NULL) {
head = NULL;
} else {
Node* temp = head;
while(temp->next->next != NULL)
temp = temp->next;
Node* lastNode = temp->next;
temp->next = NULL;
free(lastNode);
}
}
}

Deletion in doubly linked list after the specified node:

void pop_at(int position) {

if(position < 1) {
cout<<"\nposition should be >= 1.";
} else if (position == 1 && head != NULL) {
Node* nodeToDelete = head;
head = head->next;
free(nodeToDelete);
if(head != NULL)
head->prev = NULL;
} else {
Node* temp = head;
for(int i = 1; i < position-1; i++) {
if(temp != NULL) {
temp = temp->next;
}
}
if(temp != NULL && temp->next != NULL) {
Node* nodeToDelete = temp->next;
temp->next = temp->next->next;
if(temp->next->next != NULL)
temp->next->next->prev = temp->next;
free(nodeToDelete);
6

} else {
cout<<"\nThe node is already null.";
}
}
}

Code Analysis:
In order to create our doubly linked list, we first establish a structure called node
that has three members. Next and Prev. are two pointer variables that are present.
Head pointer received memory allocation. Then we developed the print function to
iterate over the list, the inset method to append an element at the end, and the
delete function to remove a node from a certain location.

Discussion:

We met multiple challenges in conducting the experiments. It took a lot of work to


implement. We experienced some difficulties and a runtime error.

We were able to successfully debug the code after identifying its issues. Because
pointer variables can be extremely problematic at times, we used caution when
using them. The ability to traverse, reverse traverse, insert into, and delete from a
two-way linked list was finally achieved.

You might also like