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

Date: 08/04/2025

The document contains multiple C++ programs that demonstrate operations on linked lists, including deletion at a specific position, insertion at the beginning and end of doubly linked lists, and deletion of the first and last elements. Each program includes a main function that tests the implemented functionalities and prints the linked list before and after modifications. The programs utilize structures for nodes and provide functions to manipulate the linked lists effectively.

Uploaded by

akashphukan69
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)
1 views9 pages

Date: 08/04/2025

The document contains multiple C++ programs that demonstrate operations on linked lists, including deletion at a specific position, insertion at the beginning and end of doubly linked lists, and deletion of the first and last elements. Each program includes a main function that tests the implemented functionalities and prints the linked list before and after modifications. The programs utilize structures for nodes and provide functions to manipulate the linked lists effectively.

Uploaded by

akashphukan69
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

Date: 08/04/2025

36. C++ program to perform deletion at a given position of a linked list.


#include <iostream>
#include <cstdlib>
using namespace std;

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

Node* head = NULL;

void insertEnd(int data) {


Node* new_node = new Node();
new_node->data = data;
new_node->next = NULL;

if (head == NULL) {
head = new_node;
return;
}

Node* temp = head;


while (temp->next != NULL)
temp = temp->next;

temp->next = new_node;
}

void deleteAtPosition(int position) {


if (head == NULL) {
cout << "List is empty.\n";
return;
}

Node* temp = head;

if (position == 0) {
head = temp->next;
delete temp;
return;
}

for (int i = 0; temp != NULL && i < position - 1; i++) {


temp = temp->next;
}
Date: 08/04/2025

if (temp == NULL || temp->next == NULL) {


cout << "Invalid position.\n";
return;
}

Node* nodeToDelete = temp->next;


temp->next = nodeToDelete->next;
delete nodeToDelete;
}

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;
};

Node* head = NULL;


Node* last = NULL;

bool isEmpty() {
return head == NULL;
}

void printList() {
Node* ptr = head;
while (ptr != NULL) {
cout << "(" << ptr->key << ", " << ptr->data << ") ";
ptr = ptr->next;
}
cout << endl;
}

void insertFirst(int key, int data) {


Node* link = new Node();
link->key = key;
link->data = data;
link->prev = NULL;
link->next = head;

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);

cout << "Doubly linked list: ";


printList();

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;
};

Node* head = NULL;

void insertAtBeginning(int data) {


Node* newNode = new Node();
newNode->data = data;
newNode->prev = NULL;
newNode->next = head;

if (head != NULL)
head->prev = newNode;

head = newNode;
}

void deleteFirst() {
if (head == NULL) {
cout << "List is already empty.\n";
return;
}

Node* temp = head;


head = head->next;

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

cout << temp->data << " ";


temp = temp->next;
}
cout << endl;
}

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;
};

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;
}
Node* temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
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);
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;
}

Node* temp = head;


while (temp->next != NULL)
temp = temp->next;

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;
}

You might also like