Date: 08/04/2025
Date: 08/04/2025
struct Node {
int data;
Node* next;
};
if (head == NULL) {
head = new_node;
return;
}
temp->next = new_node;
}
if (position == 0) {
head = temp->next;
delete temp;
return;
}
void printList() {
Node* temp = head;
cout << "[ ";
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << "]\n";
}
int main() {
insertEnd(10);
insertEnd(20);
insertEnd(30);
insertEnd(40);
insertEnd(50);
cout << "Original List:\n";
printList();
int position;
cout << "\nEnter position to delete (starting from 0): ";
cin >> position;
deleteAtPosition(position);
cout << "Updated List:\n";
printList();
return 0;
}
Date: 08/04/2025
37. C++ program to insert an element at the beginning of a double Linked list.
#include <iostream>
#include <string>
#include <cstdlib>
#include <stdbool.h>
using namespace std;
struct Node {
int data;
int key;
Node* next;
Node* prev;
};
bool isEmpty() {
return head == NULL;
}
void printList() {
Node* ptr = head;
while (ptr != NULL) {
cout << "(" << ptr->key << ", " << ptr->data << ") ";
ptr = ptr->next;
}
cout << endl;
}
if (isEmpty()) {
last = link;
} else {
head->prev = link;
}
head = link;
}
Date: 08/04/2025
int main() {
insertFirst(1, 10);
insertFirst(2, 20);
insertFirst(3, 30);
insertFirst(4, 40);
return 0;
}
Date: 08/04/2025
38. C++ program to delete the first element of a double Linked list.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* prev;
Node* next;
};
if (head != NULL)
head->prev = newNode;
head = newNode;
}
void deleteFirst() {
if (head == NULL) {
cout << "List is already empty.\n";
return;
}
if (head != NULL)
head->prev = NULL;
delete temp;
cout << "First node deleted successfully.\n";
}
void printList() {
Node* temp = head;
cout << "Doubly Linked List: ";
while (temp != NULL) {
Date: 08/04/2025
int main() {
insertAtBeginning(30);
insertAtBeginning(20);
insertAtBeginning(10);cout << "Original List:\n";
printList();deleteFirst();
cout << "List after deleting first node:\n";
printList();
return 0;
}
Date: 08/04/2025
39. C++ program to insert at the last element of a double linked list.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* prev;
Node* next;
};
int main() {
insertAtEnd(10);
insertAtEnd(20);
insertAtEnd(30);
insertAtEnd(40);
cout << "List after inserting at the end:\n";
printList();
return 0;
}
Date: 08/04/2025
40. C++ program to delete the last element of a double Linked list.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* prev;
Node* next;
};
Node* head = NULL;
void insertAtEnd(int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = NULL;
if (head == NULL) {
newNode->prev = NULL;
head = newNode;
return;
}
temp->next = newNode;
newNode->prev = temp;
}
void deleteLast() {
if (head == NULL) {
cout << "List is already empty.\n";
return;
}
if (head->next == NULL) {
delete head;
head = NULL;
cout << "Last node deleted successfully.\n";
return;
}
Node* temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->prev->next = NULL;
delete temp;
cout << "Last node deleted successfully.\n";
Date: 08/04/2025
void printList() {
Node* temp = head;
cout << "Doubly Linked List: ";
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int main() {
insertAtEnd(10);
insertAtEnd(20);
insertAtEnd(30);
cout << "Original List:\n";
printList();
deleteLast();
cout << "List after deleting last node:\n";
printList();
return 0;
}