Data Structure and Algorithm
Data Structure and Algorithm
Lab # 3
struct Nodetype
{
int data; // Add data member to store the data in the node
struct Nodetype* next; // points towards next
struct Nodetype* prev; // points to previous
};
void insert_end()
{
Nodetype* p;
p = new Nodetype;
cout << "Enter the data in node: ";
cin >> p->data;
if (first == NULL)
first = last = p;
else {
last->next = p;
p->prev = last;
last = p;
}
}
int main() {
insert_end();
return 0;
}
Output:
Activity # 2
#include <iostream>
struct Node {
int data;
Node* next;
Node* prev;
Node(int val) : data(val), next(nullptr), prev(nullptr) {}
};
class DoublyLinkedList {
public:
DoublyLinkedList() : head(nullptr), tail(nullptr) {}
while (current) {
if (count == position) {
return current->data;
}
current = current->next;
count++;
}
int main() {
DoublyLinkedList dll;
return 0;
}
Output:
Activity # 3 Activity # 4
#include <iostream>
using namespace std;
struct Nodetype
{
int data;
struct Nodetype* next;
struct Nodetype* prev;
};
if (first == NULL) {
// The list is empty
first = last = p;
p->next = NULL;
p->prev = NULL;
}
else {
p->next = first;
p->prev = NULL;
first->prev = p;
first = p;
}
}
if (first == NULL) {
// The list is empty
first = last = p;
p->next = NULL;
p->prev = NULL;
}
else {
p->next = NULL;
p->prev = last;
last->next = p;
last = p;
}
}
void delete_first()
{
Nodetype* nodePtr = first;
if (first == nullptr)
{
return; // list is empty
}
if (first == last)
{
// list has only one node
first = last = nullptr;
}
else
{
// list has more than one node
first = first->next;
first->prev = nullptr;
}
delete nodePtr;
}
void delete_last()
{
Nodetype* nodePtr = last;
if (first == nullptr)
{
return; // list is empty
}
if (first == last)
{
// list has only one node
first = last = nullptr;
}
else
{
// list has more than one node
last = last->prev;
last->next = nullptr;
}
delete nodePtr;
}
if (temp == NULL) {
// Node with the specified key not found
return;
}
if (temp == first) {
// Node to be deleted is the first node
delete_first();
}
else if (temp == last) {
// Node to be deleted is the last node
delete_last();
}
else {
// Node to be deleted is in the middle
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
delete temp;
}
}
int main() {
// Initialize an empty doubly linked list
first = last = NULL;
delete_first();
cout << "List after deletion of first element: ";
current = first;
while (current != NULL) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
delete_last();
cout << "List after deletion of last element: ";
current = first;
while (current != NULL) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
deleteSpecific(4);
cout << "List after deleting a specific number: ";
current = first;
while (current != NULL) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
// Don't forget to free memory by deleting any remaining nodes in the list.
while (first != NULL) {
Nodetype* temp = first;
first = first->next;
delete temp;
}
return 0;
}
OutPut:
Activity # 5
#include <iostream>
using namespace std;
struct Nodetype
{
int data;
struct Nodetype* next;
struct Nodetype* prev;
};
if (first == NULL) {
// The list is empty
first = last = p;
p->next = NULL;
p->prev = NULL;
}
else {
p->next = first;
p->prev = NULL;
first->prev = p;
first = p;
}
}
void completeDelete()
{
Nodetype* temp = first;
while (temp != NULL)
{
Nodetype * toDelete = temp;
temp = temp -> next;
delete toDelete;
}
}
Output:
Graded Task#1
#include <bits/stdc++.h>
struct Node {
int data;
Node(int data)
this->data = data;
next = NULL;
};
struct LinkedList {
Node* head;
void reverse()
// Store next
next = current->next;
current->next = prev;
// Move pointers one position ahead.
prev = current;
current = next;
head = prev;
void print()
temp = temp->next;
temp->next = head;
head = temp;
};
int main()
LinkedList ll;
ll.push(1);
ll.push(2);
ll.push(3);
ll.push(4);
ll.print();
ll.reverse();
ll.print();
return 0;
Output:
Graded Task # 2
#include <iostream>
struct Node {
int data;
Node* next;
};
// Function to search for two values in the linked list
Node* node1 = nullptr; // Pointer to the node containing the first value
Node* node2 = nullptr; // Pointer to the node containing the second value
if (head->data == value1) {
node1 = head;
if (head->data == value2) {
node2 = head;
head = head->next;
int main() {
newNode->next = head;
head = newNode
cout << "Enter two values to search in the linked list: ";
cout << "Both values found in nodes: " << result.first->data << " and " << result.second->data <<
endl;
} else {
cout << "At least one of the values was not found in the linked list." << endl;
head = head->next;
delete temp;
return 0;
Output:
Graded Activity # 3
#include <iostream>
struct SinglyNode {
int data;
SinglyNode* next;
};
struct DoublyNode {
int data;
DoublyNode* prev;
DoublyNode* next;
};
if (!singlyHead)
return nullptr;
while (currentSinglyNode) {
currentDoublyNode->next = newDoublyNode;
newDoublyNode->prev = currentDoublyNode;
currentDoublyNode = newDoublyNode;
currentSinglyNode = currentSinglyNode->next;
return doublyHead;
while (current) {
current = current->next;
int main() {
printDoublyLinkedList(doublyHead);
// Clean up memory (not shown in this simplified example)
return 0;
Output: