0% found this document useful (0 votes)
20 views44 pages

DSA-Aditya Singh

Uploaded by

abhijeetpundir27
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)
20 views44 pages

DSA-Aditya Singh

Uploaded by

abhijeetpundir27
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/ 44

//(singly linked list insertion)

#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};

void insertAtBeginning(Node** head, int newData) {


Node* newNode = new Node(newData);
newNode->next = *head;
*head = newNode;
}

void insertAtEnd(Node** head, int newData) {


Node* newNode = new Node(newData);
if (*head == nullptr) {
*head = newNode;
return;
}
Node* last = *head;
while (last->next) {
last = last->next;
}
last->next = newNode;
}

void insertAtPosition(Node** head, int newData, int position) {


Node* newNode = new Node(newData);
if (position == 0) {
newNode->next = *head;
*head = newNode;
return;
}
Node* current = *head;
for (int i = 0; i < position - 1; i++) {
if (current == nullptr || current->next == nullptr) {
std::cerr << "Position out of range" << std::endl;
return;
}
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}

void printList(Node* head) {


Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}

int main() {
Node* head = nullptr;
int choice, value, position;
while (true) {
cout << "\nMenu:\n";
cout << "1. Insert at Beginning\n";
cout << "2. Insert at End\n";
cout << "3. Insert at Position\n";
cout << "4. Print List\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the value to insert at the beginning: ";
cin >> value;
insertAtBeginning(&head, value);
break;
case 2:
cout << "Enter the value to insert at the end: ";
cin >> value;
insertAtEnd(&head, value);
break;
case 3:
cout << "Enter the value to insert: ";
cin >> value;
cout << "Enter the position to insert at: ";
cin >> position;
insertAtPosition(&head, value, position);
break;
case 4:
printList(head);
break;
case 5:
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid choice. Please try again" << endl;
}
}
}

OUTPUT

Menu: Menu:
1. Insert at Beginning 1. Insert at Beginning

2. Insert at End 2. Insert at End


3. Insert at Position 3. Insert at Position
4. Print List 4. Print List
5. Exit 5. Exit

Enter your choice: 2 Enter your choice: 1


Enter the value to insert at the end: 333 Enter the value to insert at the
beginning: 222
Menu:

1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Print List

5. Exit
Enter your choice: 4
222-> 333 -> NULL
(singly linked list deletion)
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;

Node(int val) : data(val), next(nullptr) {}


};

void insertAtEnd(Node** head, int newData) {


Node* newNode = new Node(newData);

if (*head == nullptr) {
*head = newNode;
return;
}

Node* last = *head;


while (last->next) {
last = last->next;
}

last->next = newNode;
}

void deleteFromBeginning(Node** head) {


if (*head == nullptr) {
cout << "List is empty, nothing to delete" << endl;
return;
}

Node* temp = *head;


*head = (*head)->next;
delete temp;
cout << "Node deleted from beginning" << endl;
}

void deleteFromEnd(Node** head) {


if (*head == nullptr) {
cout << "List is empty, nothing to delete" << endl;
return;
}
Node* temp = *head;

if (temp->next == nullptr) {
delete temp;
*head = nullptr;
cout << "Node deleted from end" << endl;
return;
}
Node* prev = nullptr;
while (temp->next) {
prev = temp;
temp = temp->next;
}
prev->next = nullptr;
delete temp;
cout << "Node deleted from end" << endl;
}

void deleteFromPosition(Node** head, int position) {


if (*head == nullptr) {
cout << "List is empty, nothing to delete" << endl;
return;
}
Node* temp = *head;

if (position == 0) {
*head = temp->next;
delete temp;
cout << "Node deleted from position 0" << endl;
return;
}
Node* prev = nullptr;
for (int i = 0; i < position; i++) {
prev = temp;
if (temp->next == nullptr) {
cout << "Position out of range" << endl;
return;
}
temp = temp->next;
}
prev->next = temp->next;
delete temp;
cout << "Node deleted from position " << position << endl;
}

void printList(Node* head) {


Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}

int main() {
Node* head = nullptr;

insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
int choice, position;
while (true) {
cout << "\nMenu:\n";
cout << "1. Delete from Beginning\n";
cout << "2. Delete from End\n";
cout << "3. Delete from Position\n";
cout << "4. Print List\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
deleteFromBeginning(&head);
break;
case 2:
deleteFromEnd(&head);
break;
case 3:
cout << "Enter the position to delete from: ";
cin >> position;
deleteFromPosition(&head, position);
break;
case 4:
printList(head);
break;
case 5:

Menu: Menu:
1. Delete from Beginning 1. Delete from Beginning
2. Delete from End 2. Delete from End
3. Delete from Position 3. Delete from Position

4. Print List 4. Print List


5. Exit 5. Exit
Enter your choice: 3 Enter your choice: 4
Enter the position to delete from: 10 -> 20 -> 30 -> NULL
3
Node deleted from position 3

cout << "Exiting..." << endl;


return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
}

OUTPUT
// (search in singly linked list)
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;

Node(int val) : data(val), next(nullptr) {}


};

void insertAtEnd(Node** head, int newData) {


Node* newNode = new Node(newData);
if (*head == nullptr) {
*head = newNode;
return;
}
Node* last = *head;
while (last->next) {
last = last->next;
}
last->next = newNode;
}

Node* searchNode(Node* head, int value) {


Node* current = head;
while (current != nullptr) {
if (current->data == value) {
return current; // Node found
}
current = current->next;
}
cout << "Node with value " << value << " not found" << endl;
return nullptr; // Node not found
}

void printList(Node* head) {


Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "nullptr" << endl;
}

int main() {
Node* head = nullptr;

insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
int value;
cout << "Singly Linked List: ";
printList(head);
cout << "Enter the value to search for: ";
cin >> value;
Node* foundNode = searchNode(head, value);
if (foundNode) {
cout << "Node found with value: " << foundNode->data << endl;
}
return 0;
}

OUTPUT

Singly Linked List: 10 -> 20 -> 30 ->


40 -> nullptr

Enter the value to search for: 40

Node found with value: 40


// (insert in doubly linked list)
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
Node* prev;

Node(int val) : data(val), next(nullptr), prev(nullptr) {}


};

void insertAtBeginning(Node** head, int newData) {


Node* newNode = new Node(newData);
if (*head != nullptr) {
(*head)->prev = newNode;
}
newNode->next = *head;
*head = newNode;
}

void insertAtEnd(Node** head, int newData) {


Node* newNode = new Node(newData);
if (*head == nullptr) {
*head = newNode;
return;
}
Node* last = *head;
while (last->next != nullptr) {
last = last->next;
}
last->next = newNode;
newNode->prev = last;
}

void insertAtPosition(Node** head, int newData, int position) {


Node* newNode = new Node(newData);
if (position == 0) {
insertAtBeginning(head, newData);
return;
}
Node* current = *head;
for (int i = 0; i < position - 1; i++) {
if (current->next == nullptr) {
cerr << "Position out of range" << endl;
return;
}
current = current->next;
}
newNode->next = current->next;
newNode->prev = current;
if (current->next != nullptr) {
current->next->prev = newNode;
}
current->next = newNode;
}

void printList(Node* head) {


Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " <-> ";
temp = temp->next;
}
cout << "NULL" << endl;
}

int main() {
Node* head = nullptr;

insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
int choice, value, position;
while (true) {
cout << "\nMenu:\n";
cout << "1. Insert at Beginning\n";
cout << "2. Insert at End\n";
cout << "3. Insert at Position\n";
cout << "4. Print List\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the value to insert at the beginning: ";
cin >> value;
insertAtBeginning(&head, value);
break;
case 2:
cout << "Enter the value to insert at the end: ";
cin >> value;
insertAtEnd(&head, value);
break;
case 3:
cout << "Enter the value to insert: ";
cin >> value;
cout << "Enter the position to insert at: ";
cin >> position;
insertAtPosition(&head, value, position);
break;
case 4:
printList(head);
break;
case 5:
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
}

OUTPUT
Menu: Menu:

1. Insert at Beginning 1. Insert at Beginning

2. Insert at End 2. Insert at End

3. Insert at Position 3. Insert at Position

4. Print List 4. Print List

5. Exit 5. Exit

Enter your choice: 1 Enter your choice: 4

Enter the value to insert at the 22 <-> 10 <-> 20 <-> 30 <-> 40 <->
beginning: 22 NULL
// (deletion in doubly linked list)
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
Node* prev;

Node(int val) : data(val), next(nullptr), prev(nullptr) {}


};
void insertAtEnd(Node** head, int newData) {
Node* newNode = new Node(newData);
if (*head == nullptr) {
*head = newNode;
return;
}
Node* last = *head;
while (last->next != nullptr) {
last = last->next;
}
last->next = newNode;
newNode->prev = last;
}

void deleteFromBeginning(Node** head) {


if (*head == nullptr) {
cout << "List is empty, nothing to delete" << endl;
return;
}
Node* temp = *head;
*head = (*head)->next;
if (*head != nullptr) {
(*head)->prev = nullptr;
}
delete temp;
cout << "Node deleted from beginning" << endl;
}

void deleteFromEnd(Node** head) {


if (*head == nullptr) {
cout << "List is empty, nothing to delete" << endl;
return;
}
Node* temp = *head;

if (temp->next == nullptr) {
delete temp;
*head = nullptr;
cout << "Node deleted from end" << endl;
return;
}
while (temp->next != nullptr) {
temp = temp->next;
}
temp->prev->next = nullptr;
delete temp;
cout << "Node deleted from end" << endl;
}

void deleteFromPosition(Node** head, int position) {


if (*head == nullptr) {
cout << "List is empty, nothing to delete" << endl;
return;
}
Node* temp = *head;
if (position == 0) {
deleteFromBeginning(head);
return;
}
for (int i = 0; i < position; i++) {
if (temp->next == nullptr) {
cout << "Position out of range" << endl;
return;
}
temp = temp->next;
}
if (temp->next != nullptr) {
temp->next->prev = temp->prev;
}
if (temp->prev != nullptr) {
temp->prev->next = temp->next;
}
delete temp;
cout << "Node deleted from position " << position << endl;
}

void printList(Node* head) {


Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " <-> ";
temp = temp->next;
}
cout << "NULL" << endl;
}

int main() {
Node* head = nullptr;

insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
int choice, position;
while (true) {
cout << "\nMenu:\n";
cout << "1. Delete from Beginning\n";
cout << "2. Delete from End\n";
cout << "3. Delete from Position\n";
cout << "4. Print List\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
deleteFromBeginning(&head);
break;
case 2:
deleteFromEnd(&head);
break;
case 3:
cout << "Enter the position to delete from: ";
cin >> position;
deleteFromPosition(&head, position);
break;
case 4:
printList(head);
break;
case 5:
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
}
OUTPUT

Menu: Menu:

1. Delete from Beginning 1. Delete from Beginning

2. Delete from End 2. Delete from End

3. Delete from Position 3. Delete from Position

4. Print List 4. Print List

5. Exit 5. Exit

Enter your choice: 3 Enter your choice: 4

Enter the position to delete from: 2 10 <-> 20 <-> 40 <-> NULL

Node deleted from position 2


// (search in doubly linked list)
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
Node* prev;

Node(int val) : data(val), next(nullptr), prev(nullptr) {}


};

void insertAtEnd(Node** head, int newData) {


Node* newNode = new Node(newData);
if (*head == nullptr) {
*head = newNode;
newNode->next = newNode->prev = newNode; // Point to itself
return;
}
Node* last = (*head)->prev;
last->next = newNode;
newNode->prev = last;
newNode->next = *head;
(*head)->prev = newNode;
}

Node* searchNode(Node* head, int value) {


if (head == nullptr) {
cout << "List is empty" << endl;
return nullptr;
}
Node* temp = head;
do {
if (temp->data == value) {
return temp; // Node found
}
temp = temp->next;
} while (temp != head);
cout << "Node with value " << value << " not found" << endl;
return nullptr; // Node not found
}
void printList(Node* head) {
if (head == nullptr) {
cout << "List is empty" << endl;
return;
}
Node* temp = head;
do {
cout << temp->data << " <-> ";
temp = temp->next;
} while (temp != head);
cout << "(back to head)" << endl;
}

int main() {
Node* head = nullptr;

insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
int value;
cout << "Doubly Linked List: ";
printList(head);
cout << "Enter the value to search for: ";
cin >> value;
Node* foundNode = searchNode(head, value);
if (foundNode) {
cout << "Node found with value: " << foundNode->data << endl;
}
return 0;
}

OUTPUT

Doubly Linked List: 10 <-> 20 <-> 30


<-> 40 <-> (back to head)

Enter the value to search for: 678

Node with value 678 not found


// (insert in singly circular linked list)
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;

Node(int val) : data(val), next(nullptr) {}


};

void insertAtBeginning(Node** head, int newData) {


Node* newNode = new Node(newData);
if (*head == nullptr) {
newNode->next = newNode;
*head = newNode;
return;
}
Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
newNode->next = *head;
temp->next = newNode;
*head = newNode;
}

void insertAtEnd(Node** head, int newData) {


Node* newNode = new Node(newData);
if (*head == nullptr) {
newNode->next = newNode;
*head = newNode;
return;
}
Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head;
}
void insertAtPosition(Node** head, int newData, int position) {
if (position == 0) {
insertAtBeginning(head, newData);
return;
}
Node* newNode = new Node(newData);
Node* temp = *head;
for (int i = 0; i < position - 1; i++) {
if (temp->next == *head) {
cout << "Position out of range" << endl;
return;
}
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}

void printList(Node* head) {


if (head == nullptr) {
cout << "List is empty" << endl;
return;
}
Node* temp = head;
do {
cout << temp->data << " -> ";
temp = temp->next;
} while (temp != head);
cout << "(back to head)" << endl;
}

int main() {
Node* head = nullptr;

insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
int choice, value, position;
while (true) {
cout << "\nMenu:\n";
cout << "1. Insert at Beginning\n";
cout << "2. Insert at End\n";
cout << "3. Insert at Position\n";
cout << "4. Print List\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the value to insert at the beginning: ";
cin >> value;
insertAtBeginning(&head, value);
break;
case 2:
cout << "Enter the value to insert at the end: ";
cin >> value;
insertAtEnd(&head, value);
break;
case 3:
cout << "Enter the value to insert: ";
cin >> value;
cout << "Enter the position to insert at: ";
cin >> position;
insertAtPosition(&head, value, position);
break;
case 4:
printList(head);
break;
case 5:
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
}

OUTPUT
Menu: Menu:
1. Insert at Beginning 1. Insert at Beginning
2. Insert at End 2. Insert at End
3. Insert at Position 3. Insert at Position
4. Print List 4. Print List
5. Exit 5. Exit
Enter your choice: 3 Enter your choice: 4
Enter the value to insert: 76 10 -> 20 -> 30 -> 76 -> 40 -> (back to
head)]
Enter the position to insert at: 3
// (deletion in singly circular linked list)
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;

Node(int val) : data(val), next(nullptr) {}


};

void insertAtEnd(Node** head, int newData) {


Node* newNode = new Node(newData);
if (*head == nullptr) {
newNode->next = newNode;
*head = newNode;
return;
}
Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head;
}

void deleteFromBeginning(Node** head) {


if (*head == nullptr) {
cout << "List is empty, nothing to delete" << endl;
return;
}
Node* temp = *head;

if ((*head)->next == *head) {
delete *head;
*head = nullptr;
cout << "Node deleted from beginning" << endl;
return;
}
while (temp->next != *head) {
temp = temp->next;
}
Node* firstNode = *head;
temp->next = (*head)->next;
*head = (*head)->next;
delete firstNode;
cout << "Node deleted from beginning" << endl;
}

void deleteFromEnd(Node** head) {


if (*head == nullptr) {
cout << "List is empty, nothing to delete" << endl;
return;
}
Node* temp = *head;

if ((*head)->next == *head) {
delete *head;
*head = nullptr;
cout << "Node deleted from end" << endl;
return;
}

Node* prev = nullptr;


while (temp->next != *head) {
prev = temp;
temp = temp->next;
}
prev->next = *head;
delete temp;
cout << "Node deleted from end" << endl;
}

void deleteFromPosition(Node** head, int position) {


if (*head == nullptr) {
cout << "List is empty, nothing to delete" << endl;
return;
}
if (position == 0) {
deleteFromBeginning(head);
return;
}
Node* temp = *head;
Node* prev = nullptr;
for (int i = 0; i < position; i++) {
prev = temp;
temp = temp->next;
if (temp == *head) {
cout << "Position out of range" << endl;
return;
}
}
prev->next = temp->next;
delete temp;
cout << "Node deleted from position " << position << endl;
}

// Function to print the list


void printList(Node* head) {
if (head == nullptr) {
cout << "List is empty" << endl;
return;
}
Node* temp = head;
do {
cout << temp->data << " -> ";
temp = temp->next;
} while (temp != head);
cout << "(back to head)" << endl;
}

int main() {
Node* head = nullptr;

insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
int choice, position;
while (true) {
cout << "\nMenu:\n";
cout << "1. Delete from Beginning\n";
cout << "2. Delete from End\n";
cout << "3. Delete from Position\n";
cout << "4. Print List\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
deleteFromBeginning(&head);
break;
case 2:
deleteFromEnd(&head);
break;
case 3:
cout << "Enter the position to delete from: ";
cin >> position;
deleteFromPosition(&head, position);
break;
case 4:
printList(head);
break;
case 5:
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
}

OUTPUT
Menu: Menu:

1. Delete from Beginning 1. Delete from Beginning

2. Delete from End 2. Delete from End

3. Delete from Position 3. Delete from Position

4. Print List 4. Print List

5. Exit 5. Exit

Enter your choice: 3 Enter your choice: 4

Enter the position to delete from: 2 10 -> 20 -> 40 -> (back to head)

Node deleted from position 2


// (search in singly circular linked list)
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;

Node(int val) : data(val), next(nullptr) {}


};

void insertAtEnd(Node** head, int newData) {


Node* newNode = new Node(newData);
if (*head == nullptr) {
*head = newNode;
newNode->next = newNode; // Point to itself
return;
}
Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head;
}

Node* searchNode(Node* head, int value) {


if (head == nullptr) {
cout << "List is empty" << endl;
return nullptr;
}
Node* temp = head;
do {
if (temp->data == value) {
return temp; // Node found
}
temp = temp->next;
} while (temp != head);
cout << "Node with value " << value << " not found" << endl;
return nullptr; // Node not found
}
void printList(Node* head) {
if (head == nullptr) {
cout << "List is empty" << endl;
return;
}
Node* temp = head;
do {
cout << temp->data << " -> ";
temp = temp->next;
} while (temp != head);
cout << "(back to head)" << endl;
}

int main() {
Node* head = nullptr;

insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
int value;
cout << "Circular Singly Linked List: ";
printList(head);
cout << "Enter the value to search for: ";
cin >> value;
Node* foundNode = searchNode(head, value);
if (foundNode) {
cout << "Node found with value: " << foundNode->data << endl;
}
return 0;
}

OUTPUT

Circular Singly Linked List: 10 -> 20 -


> 30 -> 40 -> (back to head)

Enter the value to search for: 10

Node found with value: 10


//(insert in doubly circular linked list)
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
Node* prev;

Node(int val) : data(val), next(nullptr), prev(nullptr) {}


};

void insertAtBeginning(Node** head, int newData) {


Node* newNode = new Node(newData);

if (*head == nullptr) {
newNode->next = newNode;
newNode->prev = newNode;
*head = newNode;
return;
}
Node* last = (*head)->prev;
newNode->next = *head;
newNode->prev = last;
last->next = newNode;
(*head)->prev = newNode;
*head = newNode;
}

void insertAtEnd(Node** head, int newData) {


Node* newNode = new Node(newData);
if (*head == nullptr) {
newNode->next = newNode;
newNode->prev = newNode;
*head = newNode;
return;
}
Node* last = (*head)->prev;
last->next = newNode;
newNode->prev = last;
newNode->next = *head;
(*head)->prev = newNode;
}

void insertAtPosition(Node** head, int newData, int position) {


if (position == 0) {
insertAtBeginning(head, newData);
return;
}
Node* newNode = new Node(newData);
Node* temp = *head;

for (int i = 0; i < position - 1; i++) {


temp = temp->next;
if (temp == *head) {
cout << "Position out of range" << endl;
return;
}
}
newNode->next = temp->next;
newNode->prev = temp;
temp->next->prev = newNode;
temp->next = newNode;
}

void printList(Node* head) {


if (head == nullptr) {
cout << "List is empty" << endl;
return;
}
Node* temp = head;
do {
cout << temp->data << " <-> ";
temp = temp->next;
} while (temp != head);
cout << "(back to head)" << endl;
}

int main() {
Node* head = nullptr;

insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
int choice, value, position;
while (true) {
cout << "\nMenu:\n";
cout << "1. Insert at Beginning\n";
cout << "2. Insert at End\n";
cout << "3. Insert at Position\n";
cout << "4. Print List\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the value to insert at the beginning: ";
cin >> value;
insertAtBeginning(&head, value);
break;
case 2:
cout << "Enter the value to insert at the end: ";
cin >> value;
insertAtEnd(&head, value);
break;
case 3:
cout << "Enter the value to insert: ";
cin >> value;
cout << "Enter the position to insert at: ";
cin >> position;
insertAtPosition(&head, value, position);
break;
case 4:
printList(head);
break;
case 5:
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
} Menu:
Menu:
1. Insert at Beginning
1. Insert at Beginning
OUTPUT 2. Insert at End
2. Insert at End
3. Insert at Position
3. Insert at Position
4. Print List
4. Print List
5. Exit
5. Exit
Enter your choice: 2
Enter your choice: 4
Enter the value to insert at the end:
789 10 <-> 20 <-> 30 <-> 40 <-> 789 <->
(back to head)

//(deletion in doubly circular linked list)


#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
Node* prev;

Node(int val) : data(val), next(nullptr), prev(nullptr) {}


};

void insertAtEnd(Node** head, int newData) {


Node* newNode = new Node(newData);

if (*head == nullptr) {
newNode->next = newNode;
newNode->prev = newNode;
*head = newNode;
return;
}
Node* last = (*head)->prev;
last->next = newNode;
newNode->prev = last;
newNode->next = *head;
(*head)->prev = newNode;
}

void deleteFromBeginning(Node** head) {


if (*head == nullptr) {
cout << "List is empty, nothing to delete" << endl;
return;
}
Node* temp = *head;

if ((*head)->next == *head) {
delete *head;
*head = nullptr;
cout << "Node deleted from beginning" << endl;
return;
}
Node* last = (*head)->prev;
*head = (*head)->next;
last->next = *head;
(*head)->prev = last;
delete temp;
cout << "Node deleted from beginning" << endl;
}

void deleteFromEnd(Node** head) {


if (*head == nullptr) {
cout << "List is empty, nothing to delete" << endl;
return;
}
Node* last = (*head)->prev;

if ((*head)->next == *head) {
delete *head;
*head = nullptr;
cout << "Node deleted from end" << endl;
return;
}
Node* secondLast = last->prev;
secondLast->next = *head;
(*head)->prev = secondLast;
delete last;
cout << "Node deleted from end" << endl;
}

void deleteFromPosition(Node** head, int position) {


if (*head == nullptr) {
cout << "List is empty, nothing to delete" << endl;
return;
}
if (position == 0) {
deleteFromBeginning(head);
return;
}
Node* temp = *head;
for (int i = 0; i < position; i++) {
temp = temp->next;
if (temp == *head) {
cout << "Position out of range" << endl;
return;
}
}
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
delete temp;
cout << "Node deleted from position " << position << endl;
}

void printList(Node* head) {


if (head == nullptr) {
cout << "List is empty" << endl;
return;
}
Node* temp = head;
do {
cout << temp->data << " <-> ";
temp = temp->next;
} while (temp != head);
cout << "(back to head)" << endl;
}

int main() {
Node* head = nullptr;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
int choice, position;
while (true) {
cout << "\nMenu:\n";
cout << "1. Delete from Beginning\n";
cout << "2. Delete from End\n";
cout << "3. Delete from Position\n";
cout << "4. Print List\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
deleteFromBeginning(&head);
break;
case 2:
deleteFromEnd(&head);
break;
case 3:
cout << "Enter the position to delete from: ";
cin >> position;
deleteFromPosition(&head, position);
break;
case 4:
printList(head);
break;
case 5:
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
}

OUTPUT
Menu: Menu:

1. Delete from Beginning 1. Delete from Beginning

2. Delete from End 2. Delete from End

3. Delete from Position 3. Delete from Position

4. Print List 4. Print List

5. Exit 5. Exit

Enter your choice: 1 Enter your choice: 4

Node deleted from beginning 20 <-> 30 <-> 40 <-> (back to head)


// (search in doubly circular linked list)
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
Node* prev;

Node(int val) : data(val), next(nullptr), prev(nullptr) {}


};

void insertAtEnd(Node** head, int newData) {


Node* newNode = new Node(newData);
if (*head == nullptr) {
newNode->next = newNode;
newNode->prev = newNode;
*head = newNode;
return;
}
Node* last = (*head)->prev;
last->next = newNode;
newNode->prev = last;
newNode->next = *head;
(*head)->prev = newNode;
}

Node* searchNode(Node* head, int value) {


if (head == nullptr) {
cout << "List is empty" << endl;
return nullptr;
}
Node* temp = head;
do {
if (temp->data == value) {
return temp; // Node found
}
temp = temp->next;
} while (temp != head);
cout << "Node with value " << value << " not found" << endl;
return nullptr; // Node not found
}
void printList(Node* head) {
if (head == nullptr) {
cout << "List is empty" << endl;
return;
}
Node* temp = head;
do {
cout << temp->data << " <-> ";
temp = temp->next;
} while (temp != head);
cout << "(back to head)" << endl;
}

int main() {
Node* head = nullptr;

insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
int value;
cout << "Circular Doubly Linked List: ";
printList(head);
cout << "Enter the value to search for: ";
cin >> value;
Node* foundNode = searchNode(head, value);
if (foundNode) {
cout << "Node found with value: " << foundNode->data << endl;
}
return 0;
}

OUTPUT

Circular Doubly Linked List: 10 <->


20 <-> 30 <-> 40 <-> (back to head)

Enter the value to search for: 10

Node found with value: 10


// (push and pop on stack)
#include <iostream>
using namespace std;

struct StackNode {
int data;
StackNode* next;
};
StackNode* createNode(int value) {
StackNode* newNode = new StackNode();
newNode->data = value;
newNode->next = nullptr;
return newNode;
}

/
bool isEmpty(StackNode* top) {
return top == nullptr;
}

void push(StackNode*& top, int value) {


StackNode* newNode = createNode(value);
newNode->next = top;
top = newNode;
cout << value << " pushed onto the stack." << endl;
}

int pop(StackNode*& top) {


if (isEmpty(top)) {
cout << "Stack is empty. Cannot pop." << endl;
return -1;
}
int poppedValue = top->data;
StackNode* temp = top;
top = top->next;
delete temp;
cout << poppedValue << " popped from the stack." << endl;
return poppedValue;
}
int peek(StackNode* top) {
if (isEmpty(top)) {
cout << "Stack is empty." << endl;
return -1;
}
return top->data;
}

void display(StackNode* top) {


if (isEmpty(top)) {
cout << "Stack is empty." << endl;
return;
}
StackNode* current = top;
StackNode* tempStack = nullptr;

while (current != nullptr) {


StackNode* newNode = createNode(current->data);
newNode->next = tempStack;
tempStack = newNode;
current = current->next;
}

cout << "Stack contents: ";


current = tempStack;
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
cout << endl;

while (tempStack != nullptr) {


StackNode* temp = tempStack;
tempStack = tempStack->next;
delete temp;
}
}

int main() {
StackNode* stack = nullptr;

push(stack, 10);
push(stack, 20);
push(stack, 30);
push(stack, 40);

display(stack);

int choice, value;


while (true) {
cout << "Choose an operation:" << endl;
cout << "1. Push an element" << endl;
cout << "2. Pop an element" << endl;
cout << "3. Peek at top element" << endl;
cout << "4. Display stack contents" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 5) {
cout << "Exiting..." << endl;
break; // Exit the loop
}
switch (choice) {
case 1: // Push
cout << "Enter value to push: ";
cin >> value;
push(stack, value);
break;
case 2: // Pop
pop(stack);
break;
case 3: // Peek
cout << "Top element is: " << peek(stack) << endl;
break;
case 4: // Display
display(stack);
break;
default:
cout << "Invalid choice! Please try again." << endl;
}
}

while (!isEmpty(stack)) {
pop(stack);
}
return 0;
}

OUTPUT

10 pushed onto the stack. Choose an operation:

20 pushed onto the stack. 1. Push an element

30 pushed onto the stack. 2. Pop an element

40 pushed onto the stack. 3. Peek at top element

Stack contents: 10 20 30 40 4. Display stack contents

Choose an operation: 5. Exit

1. Push an element Enter your choice: 1

2. Pop an element Enter value to push: 789

3. Peek at top element 789 pushed onto the stack.

4. Display stack contents

5. Exit Choose an operation:

Enter your choice: 2 1. Push an element

40 popped from the stack. 2. Pop an element

3. Peek at top element


Choose an operation: 4. Display stack contents
1. Push an element 5. Exit
2. Pop an element Enter your choice: 3
3. Peek at top element Top element is: 789
4. Display stack contents

5. Exit

Enter your choice: 4

Stack contents: 10 20 30 789

You might also like