Search This Blog
Welcome to 1printf(), your ultimate destination for C, C++, Linux, Data Structures, and Microcontroller programming! ๐ ๐นLearn advanced coding techniques in C& C++ ๐นMaster Linux internals & shell scripting ๐นDeep dive into Data Structures & Algorithms ๐นExplore Embedded Systems & Microcontrollers (8051,UART, RTOS) ๐นGet hands-on coding tutorials, project ideas,and interview preparation tips Whether you're a beginner or an experienced programmer, this channel will help you
Featured
- Get link
- X
- Other Apps
Singly Linked List in C – Full Code and Explanation
๐ Singly Linked List Menu
- 1. Insert Node at Beginning
- 2. Insert Node at End
- 3. Insert Node at Specific Position
- 4. Delete Node at Beginning
- 5. Delete Node at End
- 6. Delete Node at Specific Position
- 7. Display the List
- 8. Exit Program
✅ Menu Driven Program for Singly Linked List in C
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
struct node {
int data;
struct node *next;
};
void inseart_at_begining(struct node**head,int data) {
struct node* newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL) {
printf("Memory allocation faild!!");
return;
}
newnode->data=data;
newnode->next=*head;
*head=newnode;
}
void inseart_at_end(struct node** head,int data) {
struct node* newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL) {
printf("Memory allocation faild!!");
return;
}
newnode->data=data;
newnode->next=NULL;
if(*head==NULL) {
*head=newnode;
return;
}
struct node *temp=*head;
while(temp->next!=NULL) {
temp=temp->next;
}
temp->next=newnode;
}
void inseart_at_spicific_position(struct node** head,int data,int pos) {
struct node* newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL) {
printf("Memory allocation faild!!");
return;
}
newnode->data=data;
newnode->next=NULL;
if(*head==NULL&& pos!=1) {
printf("List is empty:\n");
free(newnode);
return;
}
if(pos==1) {
newnode->next=*head;
*head=newnode;
return;
}
struct node*temp=*head;
for(int i=0;i<pos-1 && temp!=NULL;i++) {
temp=temp->next;
}
if(temp==NULL) {
printf("%d IS OUT OF RANGE!\n",pos);
free(newnode);
return;
}
newnode->next = temp->next;
temp->next = newnode;
}
void delete_node_at_first(struct node**head) {
if(*head==NULL) {
printf("List is Empty:\n");
return;
}
struct node*temp=*head;
printf("%d is deleted at first:\n",temp->data);
*head=(*head)->next;
free(temp);
}
void delete_node_at_end(struct node**head) {
if(*head==NULL) {
printf("List is empty:\n");
return;
}
if((*head)->next==NULL) {
printf("%d is deleted at last:\n",(*head)->data);
free(*head);
*head=NULL;
return;
}
struct node*temp=*head;
while(temp->next->next!=NULL) {
temp=temp->next;
}
printf("%d is deleted at last:",temp->next->data);
free(temp->next);
temp->next=NULL;
}
void delete_node_at_position(struct node **head, int pos) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
if (pos == 1) {
struct node *temp = *head;
*head = (*head)->next;
printf("%d is deleted at position %d.\n", temp->data, pos);
free(temp);
return;
}
struct node *temp = *head;
for (int i = 1; temp != NULL && i < pos - 1; i++) {
temp = temp->next;
}
if (temp == NULL || temp->next == NULL) {
printf("Position %d is out of range.\n", pos);
return;
}
struct node *node_to_delete = temp->next;
temp->next = node_to_delete->next;
printf("%d is deleted at position %d.\n", node_to_delete->data, pos);
free(node_to_delete);
}
void display(struct node **head) {
if(*head==NULL) {
printf("List is empty:\n");
return;
}
struct node* temp=*head;
while(temp!=NULL) {
printf("%d->",temp->data);
temp=temp->next;
}
printf("NULL\n");
}
int main( ) {
int data,pos,choice;
struct node *head=NULL;
do {
printf("=========MENU=============\n");
printf("1).ADD NODE AT BEGIN:\n");
printf("2).ADD NODE AT END:\n");
printf("3).ADD NODE AT SPECIFIC POSITION:\n");
printf("4).DELETE NODE AT FIRST:\n");
printf("5).DELETE NODE AT LAST:\n");
printf("6).DELETE NODE AT POSITION:\n");
printf("7).DISPLAY LIST:\n");
printf("8).EXIT THE PROGRAM:\n");
printf("===========================\n");
printf("Enter the choice:\n");
scanf("%d",&choice);
switch(choice) {
case 1:
printf("Enter the element you want to add first:\n");
scanf("%d",&data);
inseart_at_begining(&head,data);
break;
case 2:
printf("Enter the element you want to inseart at end:\n");
scanf("%d",&data);
inseart_at_end(&head,data);
break;
case 3:
printf("Enter the position you want to add element:\n");
scanf("%d",&pos);
printf("Enter the element you want to add:\n");
scanf("%d",&data);
inseart_at_spicific_position(&head,data,pos);
break;
case 4:
delete_node_at_first(&head);
break;
case 5:
delete_node_at_end(&head);
break;
case 6:
printf("Enter the position to delete:\n");
scanf("%d", &pos);
delete_node_at_position(&head, pos);
break;
case 7:
display(&head);
break;
case 8:
printf("wait a minute:\n");
sleep(3);
printf("Exit successfuly:\n");
exit(0);
default:
printf("Invalid choice try again:\n");
}
}while(choice!=9);
}
๐ Explanation:
This is a menu-driven C program that implements a singly linked list. It allows you to insert nodes at the beginning, end, or at a specific position, and to delete nodes from the beginning, end, or a specific position. The display function prints the current state of the list. This kind of program is useful in understanding dynamic memory allocation and pointer operations in C programming.
๐ Keywords:
Singly Linked List in C, C Linked List Program, Add and Delete Nodes in C, Dynamic Memory Allocation in C, Linked List Menu Program, Data Structures in C, Menu Driven Program in C, Linked List Operations, Insert Node at Position, Delete Node from List, Linked List Interview Program, Linked List Beginners Tutorial
๐ Hashtags:
#CProgramming #SinglyLinkedList #LinkedListOperations #DataStructuresInC #DynamicMemory #PointerOperations #MenuDrivenProgram #CodingBeginners #CCodeBlogger
๐ฅ️ Sample Output:
=========MENU============= 1).ADD NODE AT BEGIN: 2).ADD NODE AT END: 3).ADD NODE AT SPECIFIC POSITION: 4).DELETE NODE AT FIRST: 5).DELETE NODE AT LAST: 6).DELETE NODE AT POSITION: 7).DISPLAY LIST: 8).EXIT THE PROGRAM: =========================== Enter the choice: 1 Enter the element you want to add first: 10 1 Enter the element you want to add first: 20 2 Enter the element you want to inseart at end: 5 3 Enter the position you want to add element: 2 Enter the element you want to add: 15 7 20->15->10->5->NULL 4 20 is deleted at first: 7 15->10->5->NULL 5 5 is deleted at last: 7 15->10->NULL 6 Enter the position to delete: 2 10 is deleted at position 2. 7 15->NULL 8 wait a minute: Exit successfuly:
Popular Posts
C++ Program for Hybrid Inheritance (All Types Together)
- Get link
- X
- Other Apps
C++ Program for Function Overloading Example
- Get link
- X
- Other Apps
Comments
Post a Comment