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

Single Linked List

The document provides a C program that implements a singly linked list with functions for creation, insertion, deletion, and traversal. It includes a menu-driven interface for users to perform various operations such as inserting or deleting nodes at specific positions, and displaying the list. The program handles memory allocation and provides feedback for invalid operations.

Uploaded by

nanisudheer274
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)
24 views5 pages

Single Linked List

The document provides a C program that implements a singly linked list with functions for creation, insertion, deletion, and traversal. It includes a menu-driven interface for users to perform various operations such as inserting or deleting nodes at specific positions, and displaying the list. The program handles memory allocation and provides feedback for invalid operations.

Uploaded by

nanisudheer274
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

1.

Write a program that uses functions to perform the following


operations on singly linked list
i)Creation
ii) Insertion
iii) Deletion
iv)Traversal

Aim: To write a c program to implement Singly Linked List.

Program:
#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node *next;
} *start = NULL, *q, *t;

// Function prototypes
void insert_beg();
void insert_end();
void insert_pos();
void display();
void delete_beg();
void delete_end();
void delete_pos();

int main() {
int ch;
while (1) {
printf("\n\n---- Singly Linked List(SLL) Menu ----");
printf("\n1. Insert at beginning");
printf("\n2. Insert at end");
printf("\n3. Insert at specified position");
printf("\n4. Delete from beginning");
printf("\n5. Delete from end");
printf("\n6. Delete from specified position");
printf("\n7. Display");
printf("\n8. Exit");
printf("\nEnter your choice (1-8): ");
scanf("%d", &ch);

switch (ch) {
case 1: insert_beg(); break;
case 2: insert_end(); break;
case 3: insert_pos(); break;
case 4: delete_beg(); break;
case 5: delete_end(); break;
case 6: delete_pos(); break;
case 7: display(); break;
case 8:
printf("Bye Bro !! HAVE A GREAT DAY !! :)\n");
exit(0);
default: printf("Wrong Choice!!\n");
}
}
return 0;
}

void insert_beg() {
int num;
t = (struct node*)malloc(sizeof(struct node));
if (!t) {
printf("Memory allocation failed!\n");
return;
}
printf("Enter data: ");
scanf("%d", &num);
t->data = num;
t->next = start;
start = t;
}

void insert_end() {
int num;
t = (struct node*)malloc(sizeof(struct node));
if (!t) {
printf("Memory allocation failed!\n");
return;
}
printf("Enter data: ");
scanf("%d", &num);
t->data = num;
t->next = NULL;

if (start == NULL) {
start = t;
} else {
q = start;
while (q->next != NULL) q = q->next;
q->next = t;
}
}

void insert_pos() {
int pos, i, num;
if (start == NULL) {
printf("List is empty! Inserting at beginning.\n");
insert_beg();
return;
}
printf("Enter data: ");
scanf("%d", &num);
printf("Enter position to insert: ");
scanf("%d", &pos);

if (pos == 1) {
insert_beg();
return;
}

t = (struct node*)malloc(sizeof(struct node));


if (!t) {
printf("Memory allocation failed!\n");
return;
}
t->data = num;

q = start;
for (i = 1; i < pos - 1; i++) {
if (q->next == NULL) {
printf("There are less elements!!\n");
free(t);
return;
}
q = q->next;
}
t->next = q->next;
q->next = t;
}

void display() {
if (start == NULL) {
printf("List is empty!!\n");
} else {
q = start;
printf("The linked list is: ");
while (q != NULL) {
printf("%d", q->data);
if (q->next != NULL) printf(" -> ");
q = q->next;
}
printf("\n");
}
}

void delete_beg() {
if (start == NULL) {
printf("The list is empty!!\n");
} else {
q = start;
start = start->next;
printf("Deleted element is %d\n", q->data);
free(q);
}
}

void delete_end() {
if (start == NULL) {
printf("The list is empty!!\n");
} else if (start->next == NULL) { // Only one node
printf("Deleted element is %d\n", start->data);
free(start);
start = NULL;
} else {
q = start;
while (q->next->next != NULL) q = q->next;
t = q->next;
q->next = NULL;
printf("Deleted element is %d\n", t->data);
free(t);
}
}

void delete_pos() {
int pos, i;
if (start == NULL) {
printf("List is empty!!\n");
return;
}
printf("Enter position to delete: ");
scanf("%d", &pos);

if (pos == 1) {
delete_beg();
return;
}

q = start;
for (i = 1; i < pos - 1; i++) {
if (q->next == NULL) {
printf("There are less elements!!\n");
return;
}
q = q->next;
}
t = q->next;
if (t == NULL) {
printf("Invalid position!!\n");
return;
}
q->next = t->next;
printf("Deleted element is %d\n", t->data);
free(t);
}

OUTPUT:

You might also like