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