DSA-Aditya Singh
DSA-Aditya Singh
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
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
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;
if (*head == nullptr) {
*head = newNode;
return;
}
last->next = newNode;
}
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;
}
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;
}
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
OUTPUT
// (search in singly linked list)
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
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
struct Node {
int data;
Node* next;
Node* prev;
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:
5. Exit 5. Exit
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;
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;
}
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:
5. Exit 5. Exit
struct Node {
int data;
Node* next;
Node* prev;
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
struct Node {
int data;
Node* next;
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;
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;
}
if ((*head)->next == *head) {
delete *head;
*head = nullptr;
cout << "Node deleted from end" << endl;
return;
}
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:
5. Exit 5. Exit
Enter the position to delete from: 2 10 -> 20 -> 40 -> (back to head)
struct Node {
int data;
Node* next;
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
struct Node {
int data;
Node* next;
Node* prev;
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;
}
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)
struct Node {
int data;
Node* next;
Node* prev;
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;
}
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;
}
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;
}
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:
5. Exit 5. Exit
struct Node {
int data;
Node* next;
Node* prev;
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
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;
}
int main() {
StackNode* stack = nullptr;
push(stack, 10);
push(stack, 20);
push(stack, 30);
push(stack, 40);
display(stack);
while (!isEmpty(stack)) {
pop(stack);
}
return 0;
}
OUTPUT
5. Exit