#include <iostream>
using namespace std;
// Define the node structure for the linked list
struct Node {
int data;
Node* next;
};
// Function to display the linked list
void displayList(Node* head) {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
cout << endl;
// Function to insert a new node at the end of the linked list
Node* insertAtEnd(Node* head, int value) {
Node* newNode = new Node;
newNode->data = value;
newNode->next = nullptr;
if (head == nullptr) {
return newNode;
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
temp->next = newNode;
return head;
// Function to delete the first occurrence of a value in the linked list
Node* deleteNode(Node* head, int value) {
if (head == nullptr) {
return nullptr;
if (head->data == value) {
Node* temp = head->next;
delete head;
return temp;
Node* temp = head;
while (temp->next != nullptr && temp->next->data != value) {
temp = temp->next;
}
if (temp->next != nullptr) {
Node* toDelete = temp->next;
temp->next = temp->next->next;
delete toDelete;
return head;
int main() {
Node* head = nullptr;
// Adding elements to the linked list
int n, value;
cout << "Enter the number of elements to add to the linked list: ";
cin >> n;
cout << "Enter " << n << " elements: " << endl;
for (int i = 0; i < n; i++) {
cin >> value;
head = insertAtEnd(head, value);
// Displaying the initial linked list
cout << "Initial linked list: ";
displayList(head);
// Deleting an element from the beginning of the linked list
head = deleteNode(head, head->data);
cout << "Linked list after deleting the first element: ";
displayList(head);
// Deleting an element from the middle of the linked list
Node* temp = head;
for (int i = 0; i < n / 2 - 1; i++) {
temp = temp->next;
head = deleteNode(head, temp->data);
cout << "Linked list after deleting an element from the middle: ";
displayList(head);
// Deleting an element from the end of the linked list
temp = head;
for (int i = 0; i < n - 2; i++) {
temp = temp->next;
head = deleteNode(head, temp->next->data);
cout << "Linked list after deleting the last element: ";
displayList(head);
return 0;
}
Q1
#include <iostream>
using namespace std;
// Define the node structure for the linked list
struct Node {
int data;
Node* next;
};
// Function to display the linked list
void displayList(Node* head) {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
cout << endl;
// Function to insert a new node at the end of the linked list
Node* insertAtEnd(Node* head, int value) {
Node* newNode = new Node;
newNode->data = value;
newNode->next = nullptr;
if (head == nullptr) {
return newNode;
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
temp->next = newNode;
return head;
// Function to delete the first node in the linked list
Node* deleteFirst(Node* head) {
if (head == nullptr) {
return nullptr;
Node* temp = head->next;
delete head;
return temp;
// Function to delete the last node in the linked list
Node* deleteLast(Node* head) {
if (head == nullptr) {
return nullptr;
}
if (head->next == nullptr) {
delete head;
return nullptr;
Node* temp = head;
while (temp->next->next != nullptr) {
temp = temp->next;
delete temp->next;
temp->next = nullptr;
return head;
// Function to delete the middle node in the linked list
Node* deleteMiddle(Node* head) {
if (head == nullptr || head->next == nullptr) {
return nullptr;
Node* slow = head;
Node* fast = head->next;
Node* prev = nullptr;
while (fast != nullptr && fast->next != nullptr) {
prev = slow;
slow = slow->next;
fast = fast->next->next;
if (prev != nullptr) {
prev->next = slow->next;
delete slow;
return head;
int main() {
Node* head = nullptr;
// Adding elements to the linked list
int n, value;
cout << "Enter the number of elements to add to the linked list: ";
cin >> n;
cout << "Enter " << n << " elements: " << endl;
for (int i = 0; i < n; i++) {
cin >> value;
head = insertAtEnd(head, value);
// Displaying the initial linked list
cout << "Initial linked list: ";
displayList(head);
// Deleting the first element in the linked list
head = deleteFirst(head);
cout << "Linked list after deleting the first element: ";
displayList(head);
// Deleting the middle element in the linked list
head = deleteMiddle(head);
cout << "Linked list after deleting the middle element: ";
displayList(head);
// Deleting the last element in the linked list
head = deleteLast(head);
cout << "Linked list after deleting the last element: ";
displayList(head);
return 0;