KEMBAR78
Create Display Insert and Delete | PDF | C++ | Computer Data
0% found this document useful (0 votes)
5 views6 pages

Create Display Insert and Delete

This document contains a C program that implements a singly linked list with functionalities to create, display, insert, and delete nodes at specific positions. It includes functions for memory allocation, error handling, and freeing allocated memory. The main function demonstrates the usage of these linked list operations.

Uploaded by

Sathish
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)
5 views6 pages

Create Display Insert and Delete

This document contains a C program that implements a singly linked list with functionalities to create, display, insert, and delete nodes at specific positions. It includes functions for memory allocation, error handling, and freeing allocated memory. The main function demonstrates the usage of these linked list operations.

Uploaded by

Sathish
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/ 6

//create display insert and delete@21 March 2025

#include <stdio.h>

#include <stdlib.h>

struct node {

int data;

struct node *next;

};

// Function to create a linked list

struct node *create(struct node *head) {

struct node *new_node, *temp = NULL;

int num;

printf("Enter the data (-1 to stop): ");

scanf("%d", &num);

while (num != -1) {

new_node = (struct node *)malloc(sizeof(struct node)); //dma

if (new_node == NULL) {

printf("Memory allocation failed!\n");

return head;

new_node->data = num;

new_node->next = NULL;

if (head == NULL) {

head = new_node;

temp = new_node;

}
else {

temp->next = new_node;

temp = new_node;

printf("Enter the data (-1 to stop): ");

scanf("%d", &num);

} //end of while loop

return head;

// Function to display the linked list

void display(struct node *head) {

struct node *temp = head;

if (temp == NULL) {

printf("The list is empty.\n");

return;

printf("Linked list elements: ");

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

printf("\n");

}
// Function to insert at a specific position

struct node *insertAtPosition(struct node *head, int data, int position) {

struct node *new_node = (struct node *)malloc(sizeof(struct node)); //dma

if (new_node == NULL) {

printf("Memory allocation failed!\n");

return head;

new_node->data = data;

new_node->next = NULL;

// Insert at the beginning

if (position == 1) {

new_node->next = head;

head = new_node;

return head;

struct node *temp = head;

int count = 1;

// Traverse to the position before where new node should be inserted

while (temp != NULL && count < position - 1) {

temp = temp->next;

count++;

}
// If position is invalid (greater than the number of nodes)

if (temp == NULL) {

printf("Position out of range! Inserting at the end.\n");

free(new_node);

return head;

// Insert in the middle or end

new_node->next = temp->next;

temp->next = new_node;

return head;

// Function to delete a node at a specific position

struct node *deleteAtPosition(struct node *head, int position) {

if (head == NULL) {

printf("The list is empty, nothing to delete.\n");

return head;

struct node *temp = head;

// If deleting the first node

if (position == 1) {

head = head->next;

free(temp);

return head;

}
struct node *prev = NULL;

int count = 1;

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

while (temp != NULL && count < position) {

prev = temp;

temp = temp->next;

count++;

// If position is out of range

if (temp == NULL) {

printf("Position out of range, nothing to delete.\n");

return head;

// Adjust links and delete node

prev->next = temp->next;

free(temp);

return head;

// Function to free allocated memory

void freeList(struct node *head) {

struct node *temp;

while (head != NULL) {

temp = head;

head = head->next;

free(temp);

}
int main() {

struct node *head = NULL;

int data, position, choice;

head = create(head);

display(head);

// Insert at a specific position

printf("Enter the data to insert: ");

scanf("%d", &data);

printf("Enter the position to insert at: ");

scanf("%d", &position);

head = insertAtPosition(head, data, position);

display(head);

// Delete from a specific position

printf("Enter the position to delete from: ");

scanf("%d", &position);

head = deleteAtPosition(head, position);

display(head);

// Free allocated memory

freeList(head);

return 0;

You might also like