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

Bre

The document contains C++ code for a linked list implementation, including functions to display the list, insert nodes at the end, and delete nodes from the beginning, middle, and end. It provides a main function that allows users to input elements, demonstrating the linked list operations. The code includes error handling for empty lists and ensures proper memory management by deleting nodes.

Uploaded by

halefomgere45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views9 pages

Bre

The document contains C++ code for a linked list implementation, including functions to display the list, insert nodes at the end, and delete nodes from the beginning, middle, and end. It provides a main function that allows users to input elements, demonstrating the linked list operations. The code includes error handling for empty lists and ensures proper memory management by deleting nodes.

Uploaded by

halefomgere45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

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

You might also like