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

DSA Day 1

Uploaded by

engg.mohsin1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

DSA Day 1

Uploaded by

engg.mohsin1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Programming

Session
Phase 1
DSA
Guidelines for Effective Learning
1. Carefully read the provided documents and references to build a strong
foundation on the topic.
2. Engage actively with the content, clarify any doubts, and summarize key points in
your own words.
3. Problems are given in group, you can collaborate with group members to solve the
provided problems, discussing and debating different approaches.
4. Regularly update your solutions on GitHub, ensuring clear organization and proper
documentation.
5. If you encounter any problems, you can freely ask in the Competitive
Programming group.

Day 1

Array
An array is a collection of items of the same data type stored at contiguous memory
locations.
A single block of memory with partition.

Linked List
A Linked List is a linear data structure which looks like a chain of nodes, where each
node contains a data field and a reference(link) to the next node in the list.
Multiple blocks of memory linked to each other.
Arrays VS Linked list

Types of Linked List


Singly Linked List
Each node has data and a reference to the next node.
A singly linked list is like a train journey with different segments, where
each train (node) has the task of taking you from one station to the next
until you reach your final destination. The transitions between trains are like
pointers, guiding you through each segment of your journey.

Doubly Linked List


Each node has data and references to both the next and previous nodes.

You can think of a doubly linked list as a book where each page not only
contains information about a task but also provides easy navigation to the
tasks before and after it. The first page, similar to the head of a doubly
linked list, does not have a previous reference, while the last page, identical
to the tail of a doubly linked list, does not have a next reference.
Circular Linked List
The last node of the list points back to the first node, forming a circle or
loop.

Imagine a train line that forms a loop, with passengers boarding and exiting
at various stations along the way. This loop represents a circular linked list,
where each station is a node and the train continuously travels around the
loop, picking up and dropping off passengers.

Applications

In web browsers
While moving through web pages, you have both options to move to the next page or
the previous page. This is only possible because of a doubly linked list.

In music players
Similarly, we can switch to the next and the previous song in a music player using links
between songs. We can also play songs either from the starting or the end of the
playlist.

In image viewer
The next and the previous photos are linked, and we can easily access them by using the
previous and next buttons.

In operating systems
The thread scheduler uses a doubly-linked list to maintain the list of all processes
running at any time. It enables us to move a certain process from one queue to another
with ease.

In Multiplayer games
Online multiplayer games such as the likes of PUBG and Call of Duty use a circular
linked list to swap between players in a loop.

Implementation
Insertion
#include<iostream>
using namespace std;

class node {
public:
int data;
node* next;
node(int v) {
data = v;
next = nullptr;
}
};

class linkedlist {
public:
node* head = nullptr;

//-----------------INSERT AT START-------------------------------------------
void Insert_At_Start(int val) {
if (head == nullptr) {
head = new node(val);
}
else {
node* temp = new node(val);
temp->next = head;
head = temp;
}
}

//------------------INSERT AT END--------------------------------------------
void Insert_At_End(int val) {
if (head == nullptr) {
head = new node(val);
}
else {
node* travel = head;
while (travel->next != nullptr) {
travel = travel->next;
}
node* newnode = new node(val);
travel->next = newnode;

}
}
//--------------------INSERT AT INDEX----------------------------------------
void Insert_at_Index(int index, int val) {
if (index < 0) {
cout << "INVALID INDEX\n";
}
else if (index == 0) {
Insert_At_Start(val);
}
else {
bool flag = false;
node* previous = head;
node* current = head;
for (int i = 0; i < index; i++)
{
if (current == nullptr) {
flag = true;
break;
}
previous = current;
current = current->next;
}
if (flag) {
cout << "Index out of range\n ";
}
else {
node* newnode = new node(val);
previous->next = newnode;
newnode->next = current;
}
}

}
};

Deletion

void Delete_At_Index(int index) {


if (index < 0) {
cout << "INVALID INDEX" << endl;
}
else if (index == 0) {
node* todelete = head;
head = head->next;
delete todelete;
}
else {
node* current = head;
node* previous = head;
for (int i = 0; i < index; i++)
{
previous = current;
current = current->next;
if (current == nullptr) {
cout << "INDEX OUT OF RANGE\n";
return;
}
}
previous->next = current->next;
delete current;

}
}

Once, the link pointing to the target node is removed, we'll also remove the link from Node B to
Node C.

Now, Node B should point to ‘Null’. Deallocate memory to eliminate Node B.

Search and Print


bool search(node* temp, int tosearch) {
if (temp == nullptr) {
return false;
}
if (temp->data == tosearch) {
return true;
}
return search(temp->next, tosearch);
}

void reverse_print(node* temp) {


if (temp == nullptr) {
return;
}
reverse_print(temp->next);
cout << temp->data << " -> ";
}

void forward_print() {
if (head == nullptr) {
cout << "list is empty\n";
}
else {
node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
cout << endl;
}
}

Make the LinkedList object in the main and access its function.
linkedlist list1;
cout << "\tInsert at Start \n";
list1.Insert_At_Start(15);
list1.forward_print();

References
Adding two polynomials using Linked List

https://www.masaischool.com/blog/linked-list/

https://www.freecodecamp.org/news/what-is-a-linked-list-types-and-examples/#understandi
ng-linked-list.

https://www.geeksforgeeks.org/linked-list-data-structure/

You might also like