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

Linked List Detailed

The document contains a C program that implements a singly linked list with various functionalities including insertion at the end, start, and after a specific position, as well as deletion from the end, start, and after a specific position. It also includes a display function to traverse and print the list. The main function demonstrates these operations by inserting and deleting nodes, followed by displaying the list at each step.

Uploaded by

maggiprash59
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)
9 views5 pages

Linked List Detailed

The document contains a C program that implements a singly linked list with various functionalities including insertion at the end, start, and after a specific position, as well as deletion from the end, start, and after a specific position. It also includes a display function to traverse and print the list. The main function demonstrates these operations by inserting and deleting nodes, followed by displaying the list at each step.

Uploaded by

maggiprash59
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/ 5

#include <stdio.

h>

#include <stdlib.h>

// Define node structure

struct Node {

int data;

struct Node* next;

};

// Head pointer for the list

struct Node* head = NULL;

// b1. Insert at the end

void insert_end(int value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = NULL;

if (head == NULL) {

head = newNode;

} else {

struct Node* temp = head;

while (temp->next != NULL)

temp = temp->next;

temp->next = newNode;

// b2. Insert at the start

void insert_start(int value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = head;

head = newNode;

// b3. Insert after specific position (0-based index)


void insert_after(int position, int value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

struct Node* temp = head;

int i;

for (i = 0; i < position; i++) {

if (temp == NULL) {

printf("Position out of range\n");

free(newNode);

return;

temp = temp->next;

if (temp == NULL) {

printf("Position out of range\n");

free(newNode);

return;

newNode->next = temp->next;

temp->next = newNode;

// c1. Delete from end

void delete_end() {

if (head == NULL) {

printf("List is empty.\n");

return;

if (head->next == NULL) {

free(head);

head = NULL;

return;
}

struct Node* temp = head;

while (temp->next->next != NULL)

temp = temp->next;

free(temp->next);

temp->next = NULL;

// c2. Delete from start

void delete_start() {

if (head == NULL) {

printf("List is empty.\n");

return;

struct Node* temp = head;

head = head->next;

free(temp);

// c3. Delete after specific position (0-based index)

void delete_after(int position) {

struct Node* temp = head;

int i;

for (i = 0; i < position; i++) {

if (temp == NULL) {

printf("Position out of range.\n");

return;

temp = temp->next;

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

printf("Nothing to delete at given position.\n");

return;
}

struct Node* del = temp->next;

temp->next = del->next;

free(del);

// d. Traverse and display all the elements in the list

void display() {

struct Node* temp = head;

if (temp == NULL) {

printf("List is empty.\n");

return;

while (temp != NULL) {

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

temp = temp->next;

printf("NULL\n");

// Main function to demonstrate

int main() {

insert_end(10);

insert_end(20);

insert_start(5);

insert_after(1, 15); // insert 15 after position 1

display(); // 5 -> 10 -> 15 -> 20 -> NULL

delete_end();

display(); // 5 -> 10 -> 15 -> NULL

delete_start();

display(); // 10 -> 15 -> NULL

delete_after(0);

display(); // 10 -> NULL


return 0;

You might also like