*Create and Display singly linked list
#include <stdio.h>
#include <stdlib.h>
struct Slist {
int data;
struct Slist *next;
} *Start = NULL;
typedef struct Slist node;
void create() {
node *tmp, *tmpl;
tmp = (node*) malloc(sizeof(node));
tmp->next = NULL;
printf("\nEnter number in node: ");
scanf("%d", &tmp->data);
if (Start == NULL) {
Start = tmp;
} else {
tmpl = Start;
while (tmpl->next != NULL) {
tmpl = tmpl->next;
tmpl->next = tmp;
void display() {
node *tmp;
tmp = Start;
if (tmp == NULL) {
printf("\nEmpty");
} else {
while (tmp != NULL) {
printf("\n%d", tmp->data);
tmp = tmp->next;
int main() {
int choice;
while (1) {
printf("\n1 -> Create List");
printf("\n2 -> Display List");
printf("\n3 -> Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
if (choice == 1) {
create();
} else if (choice == 2) {
display();
} else if (choice == 3) {
break;
} else {
printf("\nWrong choice!");
}
}
return 0;
Output:
10
15
20
# Insertion of node at first position in single linked list.
# Prototype: void finsert();
# Call: finsert();
void finsert()
node *tmp;
tmp = (node *)malloc(sizeof(node));
tmp->next = NULL;
printf("\nEnter Number:");
scanf("%d", &tmp->data);
tmp->next = head;
head = tmp;
printf("Node Inserted at First Position");
# Insertion of node at the last position in single linked list
void linsert()
node *tmp, *tmp1;
tmp = (node *)malloc(sizeof(node));
printf("\nEnter Data:");
scanf("%d", &tmp->data);
tmp->next = NULL;
tmp1 = head;
while (tmp1->next != NULL)
tmp1 = tmp1->next;
tmp1->next = tmp;
printf("\nElement Inserted Successfully");
# Insert node at the given position of the linked list.
# Prototype: void apinsert();
# Call: apinsert();
Definition:
void apinsert()
int pos, count = 0, i;
node *tmp, *tmp1;
printf("\nEnter Position to Insert:");
scanf("%d", &pos);
tmp = head;
while (tmp != NULL)
tmp = tmp->next;
count++;
if (pos < 1 || pos > count + 1)
printf("Invalid Position");
else
{
tmp1 = (node *)malloc(sizeof(node));
printf("\nEnter Data for New Node:");
scanf("%d", &tmp1->data);
tmp = head;
if (pos == 1)
tmp1->next = tmp;
head = tmp1;
else
for (i = 1; i < pos - 1; i++)
tmp = tmp->next;
tmp1->next = tmp->next;
tmp->next = tmp1;
printf("\nElement Inserted Successfully...");
# Delete Node at First Position
Prototype: void fdelete(void);
Call: fdelete();
Function Definition:
void fdelete()
node *tmp;
tmp = head;
if (tmp == NULL)
printf("\nEmpty Link List");
else
head = tmp->next;
free(tmp);
printf("\nFirst Delete Successful");
# Delete node at last position in a linked list
Prototype: void ldelete();
Calling: ldelete();
Definition:
void ldelete()
node *tmp, *tmp1;
tmp = head;
if (tmp == NULL)
printf("\nEmpty Link List");
else
while (tmp->next->next != NULL)
tmp = tmp->next;
tmp1 = tmp->next;
tmp->next = NULL;
free(tmp1);
printf("Last Delete Successful...");
}
# Delete node at a specific position in a linked list
Prototype: void apdelete()
Call: apdelete()
Definition:
void apdelete()
int pos, count = 0, i;
node *tmp, *tmp1;
tmp = head;
while (tmp != NULL)
count++;
tmp = tmp->next;
printf("\nEnter Position to Delete:");
scanf("%d", &pos);
if (pos < 1 || pos > count)
printf("\nInvalid Position");
else if (pos == 1)
tmp = head;
tmp1 = tmp->next;
head = tmp1;
free(tmp);
printf("\nDeletion Successful...");
}
else
tmp = head;
for(i = 1; i < pos - 1; i++)
tmp = tmp->next;
tmp1 = tmp->next;
tmp->next = tmp1->next;
free(tmp1);
printf("\nDeletion successful...");
Combined all the function:
############################ Singly Linked List program####################
#include <stdio.h>
#include <stdlib.h>
// Structure for node
typedef struct node {
int data;
struct node *next;
} node;
node *start = NULL;
// Function prototypes
void create();
void finsert();
void linsert();
void ainsert();
void fdelete();
void ldelete();
void adelete();
void display();
void count();
void traverse();
void search();
void sort();
int main() {
int choice;
while (1) {
printf("\n--- Singly Linked List Menu ---");
printf("\n1. Create Linked List");
printf("\n2. Insert at First");
printf("\n3. Insert at Last");
printf("\n4. Insert at Any Position");
printf("\n5. Delete from First");
printf("\n6. Delete from Last");
printf("\n7. Delete from Any Position");
printf("\n8. Display");
printf("\n9. Count Nodes");
printf("\n10. Traverse");
printf("\n11. Search");
printf("\n12. Sort");
printf("\n13. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: create(); break;
case 2: finsert(); break;
case 3: linsert(); break;
case 4: ainsert(); break;
case 5: fdelete(); break;
case 6: ldelete(); break;
case 7: adelete(); break;
case 8: display(); break;
case 9: count(); break;
case 10: traverse(); break;
case 11: search(); break;
case 12: sort(); break;
case 13: exit(0);
default: printf("\nInvalid choice!");
return 0;
// Create Linked List with n nodes
void create() {
int n, i, val;
node *tmp, *p;
printf("\nEnter number of nodes to create: ");
scanf("%d", &n);
if (n <= 0) {
printf("\nInvalid number of nodes!");
return;
for (i = 1; i <= n; i++) {
tmp = (node *)malloc(sizeof(node));
printf("Enter value for Node %d: ", i);
scanf("%d", &val);
tmp->data = val;
tmp->next = NULL;
if (start == NULL) {
start = tmp;
} else {
p = start;
while (p->next != NULL)
p = p->next;
p->next = tmp;
printf("\nLinked List Created Successfully with %d nodes.\n", n);
// Insert at first
void finsert() {
node *tmp = (node *)malloc(sizeof(node));
printf("\nEnter value for New Node: ");
scanf("%d", &tmp->data);
tmp->next = start;
start = tmp;
printf("\nFirst Insert Successful...");
// Insert at last
void linsert() {
node *tmp = (node *)malloc(sizeof(node));
printf("\nEnter value for New Node: ");
scanf("%d", &tmp->data);
tmp->next = NULL;
if (start == NULL) {
start = tmp;
} else {
node *p = start;
while (p->next != NULL)
p = p->next;
p->next = tmp;
printf("\nLast Insert Successful...");
// Insert at any position
void ainsert() {
int pos, count = 0, i;
node *tmp = (node *)malloc(sizeof(node));
node *p = start;
printf("\nEnter value for New Node: ");
scanf("%d", &tmp->data);
while (p != NULL) {
count++;
p = p->next;
printf("\nEnter Position to Insert: ");
scanf("%d", &pos);
if (pos < 1 || pos > count + 1) {
printf("\nInvalid Position!");
free(tmp);
return;
if (pos == 1) {
tmp->next = start;
start = tmp;
} else {
p = start;
for (i = 1; i < pos - 1; i++)
p = p->next;
tmp->next = p->next;
p->next = tmp;
}
printf("\nInsertion Successful...");
// Delete from first
void fdelete() {
if (start == NULL) {
printf("\nEmpty Linked List!");
return;
node *tmp = start;
start = start->next;
free(tmp);
printf("\nFirst Position Delete Successful...");
// Delete from last
void ldelete() {
if (start == NULL) {
printf("\nEmpty Linked List!");
return;
node *tmp = start, *prev = NULL;
while (tmp->next != NULL) {
prev = tmp;
tmp = tmp->next;
if (prev == NULL) {
free(start);
start = NULL;
} else {
prev->next = NULL;
free(tmp);
printf("\nLast Position Delete Successful...");
// Delete from any position
void adelete() {
if (start == NULL) {
printf("\nEmpty Linked List!");
return;
int pos, count = 0, i;
node *tmp = start, *prev = NULL;
while (tmp != NULL) {
count++;
tmp = tmp->next;
printf("\nEnter Position to Delete: ");
scanf("%d", &pos);
if (pos < 1 || pos > count) {
printf("\nInvalid Position!");
return;
tmp = start;
if (pos == 1) {
start = tmp->next;
free(tmp);
} else {
for (i = 1; i < pos; i++) {
prev = tmp;
tmp = tmp->next;
prev->next = tmp->next;
free(tmp);
printf("\nDeletion Successful...");
// Display the list
void display() {
node *tmp = start;
if (tmp == NULL) {
printf("\nEmpty Linked List!");
return;
printf("\nSingly Linked List: ");
while (tmp != NULL) {
printf("%d -> ", tmp->data);
tmp = tmp->next;
printf("NULL\n");
// Count nodes
void count() {
int cnt = 0;
node *tmp = start;
while (tmp != NULL) {
cnt++;
tmp = tmp->next;
printf("\nTotal Nodes: %d\n", cnt);
// Traverse (same as display, but kept separately for clarity)
void traverse() {
node *tmp = start;
if (tmp == NULL) {
printf("\nEmpty Linked List!");
return;
printf("\nTraversing the List: ");
while (tmp != NULL) {
printf("%d ", tmp->data);
tmp = tmp->next;
}
printf("\n");
// Search a value
void search() {
int key, pos = 1, found = 0;
node *tmp = start;
if (tmp == NULL) {
printf("\nEmpty Linked List!");
return;
printf("\nEnter value to search: ");
scanf("%d", &key);
while (tmp != NULL) {
if (tmp->data == key) {
printf("\nValue %d found at position %d\n", key, pos);
found = 1;
break;
pos++;
tmp = tmp->next;
if (!found)
printf("\nValue %d not found in the list\n", key);
// Sort the linked list (ascending order using bubble sort)
void sort() {
if (start == NULL) {
printf("\nEmpty Linked List!");
return;
node *i, *j;
int temp;
for (i = start; i->next != NULL; i = i->next) {
for (j = i->next; j != NULL; j = j->next) {
if (i->data > j->data) {
temp = i->data;
i->data = j->data;
j->data = temp;
printf("\nLinked List Sorted Successfully!\n");
Algorithms for Singly Linked List Operations
1. Create Linked List (create())
Algorithm:
1. Start.
2. Input number of nodes n.
3. Repeat steps for i = 1 to n:
o Create a new node.
o Input value for the node.
o If start == NULL, set start = new node.
o Else, traverse to last node and link last->next = new node.
4. Print "Linked List Created Successfully".
5. Stop.
2. Insert at First (finsert())
Algorithm:
1. Start.
2. Create a new node.
3. Input value for the new node.
4. Set new->next = start.
5. Set start = new.
6. Print "First Insert Successful".
7. Stop.
3. Insert at Last (linsert())
Algorithm:
1. Start.
2. Create a new node.
3. Input value for the new node.
4. Set new->next = NULL.
5. If start == NULL, set start = new.
6. Else, traverse to the last node and set last->next = new.
7. Print "Last Insert Successful".
8. Stop.
4. Insert at Any Position (ainsert())
Algorithm:
1. Start.
2. Count the total number of nodes.
3. Input position pos and value.
4. If pos < 1 or pos > count + 1, print "Invalid Position".
5. Else if pos == 1, perform first insertion.
6. Else:
o Traverse to (pos-1)th node.
o Link new->next = current->next.
o Set current->next = new.
7. Print "Insertion Successful".
8. Stop.
5. Delete from First (fdelete())
Algorithm:
1. Start.
2. If start == NULL, print "Empty List".
3. Else:
o Set tmp = start.
o Set start = start->next.
o Free(tmp).
4. Print "First Delete Successful".
5. Stop.
6. Delete from Last (ldelete())
Algorithm:
1. Start.
2. If start == NULL, print "Empty List".
3. Else if only one node:
o Free start.
o Set start = NULL.
4. Else:
o Traverse to last node with a prev pointer.
o Set prev->next = NULL.
o Free last node.
5. Print "Last Delete Successful".
6. Stop.
7. Delete from Any Position (adelete())
Algorithm:
1. Start.
2. If start == NULL, print "Empty List".
3. Count total nodes.
4. Input position pos.
5. If pos < 1 or pos > count, print "Invalid Position".
6. Else if pos == 1, perform first delete.
7. Else:
o Traverse to (pos-1)th node.
o Set prev->next = tmp->next.
o Free(tmp).
8. Print "Deletion Successful".
9. Stop.
8. Display (display())
Algorithm:
1. Start.
2. If start == NULL, print "Empty List".
3. Else:
o Traverse from start to end.
o Print each node’s data.
4. Stop.
9. Count Nodes (count())
Algorithm:
1. Start.
2. Initialize cnt = 0.
3. Set tmp = start.
4. Repeat until tmp == NULL:
o Increment cnt.
o Move tmp = tmp->next.
5. Print cnt as total number of nodes.
6. Stop.
10. Traverse (traverse())
Algorithm:
1. Start.
2. If start == NULL, print "Empty List" and stop.
3. Set tmp = start.
4. Repeat until tmp == NULL:
o Print tmp->data.
o Move tmp = tmp->next.
5. Stop.
11. Search (search())
Algorithm:
1. Start.
2. If start == NULL, print "Empty List" and stop.
3. Input the value key to be searched.
4. Initialize pos = 1.
5. Set tmp = start.
6. Repeat until tmp == NULL:
o If tmp->data == key, print "Found at position pos" and stop.
o Else, move tmp = tmp->next, increment pos.
7. If traversal ends without finding key, print "Not Found".
8. Stop.
12. Sort (sort())
Algorithm:
1. Start.
2. If start == NULL, print "Empty List" and stop.
3. Use two pointers i and j.
4. For each node i from start to last node:
o For each node j from i->next to last node:
If i->data > j->data, swap the values.
5. Print "List Sorted Successfully".
6. Stop.