0% found this document useful (0 votes)
15 views5 pages

Deletion in An Linked List

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

Deletion in An Linked List

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

#include<iostream.

h>

#include<conio.h>

// Node structure definition

struct Node {

int data;

Node* next;

};

// Function to insert a node at a particular position

void insertAtPosition(Node** head, int value, int position) {

Node* newNode = new Node(); // Create a new node

newNode->data = value; // Assign value to the new node

newNode->next = NULL;

// If inserting at the head (position 1)

if (position == 1) {

newNode->next = *head;

*head = newNode;

return;

Node* temp = *head;

for (int i = 1; i < position - 1; i++) {


if (temp == NULL) {

cout << "Position out of bounds." << endl;

return; // Position is out of range

temp = temp->next;

if (temp != NULL) {

newNode->next = temp->next; // Insert at the desired position

temp->next = newNode;

} else {

cout << "Position out of bounds." << endl;

// Function to delete a node at a particular position

void deleteAtPosition(Node** head, int position) {

if (*head == NULL) {

cout << "List is empty, cannot delete." << endl;

return;

Node* temp = *head;


// If the head node is to be deleted (position 1)

if (position == 1) {

*head = temp->next; // Move head to the next node

delete temp; // Free the memory of the old head

return;

// Traverse to the node before the node to be deleted

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

temp = temp->next;

// If the position is out of bounds

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

cout << "Position out of bounds." << endl;

return;

Node* nodeToDelete = temp->next;

temp->next = nodeToDelete->next; // Link the previous node to the next node

delete nodeToDelete; // Free memory of the deleted node

// Function to display the linked list


void display(Node* head) {

Node* temp = head;

while (temp != NULL) {

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

temp = temp->next;

cout << "NULL" << endl;

void main() {

clrscr(); // Clear the console screen

Node* head = NULL; // Initialize the linked list as empty

int choice, value, position;

while (1) {

cout << "\nMenu:\n1. Insert at position\n2. Delete from position\n3. Display list\n4. Exit\nEnter
your choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter value and position to insert: ";

cin >> value >> position;


insertAtPosition(&head, value, position);

display(head);

break;

case 2:

cout << "Enter position to delete: ";

cin >> position;

deleteAtPosition(&head, position);

display(head);

break;

case 3:

display(head);

break;

case 4:

return; // Exit the program

default:

cout << "Invalid choice! Please try again." << endl;

getch(); // Wait for keypress before closing

You might also like