Skip to main content

Featured

C Program to Check Prime Number Using Efficient Logic

  Introduction A prime number is a number that has exactly two distinct positive divisors: 1 and itself. In this program, we check whether a given number is prime or not using a simple and efficient logic. This type of program is commonly used in mathematics, competitive programming, and basic algorithm learning for beginners in C programming. Problem Statement The task is to write a C program that determines whether a given integer is a prime number or not. The program takes a single integer input from the user and analyzes its divisibility. If the number has no divisors other than 1 and itself, it should be identified as a prime number; otherwise, it is not prime. This problem is important in number theory and has practical relevance in areas such as cryptography, data validation, and algorithm design.  Algorithm / Logic Explanation To check whether a number is prime, we need to verify that it is not divisible by any number other than 1 and itself. The algorithm follows a si...

Singly Linked List in C – Full Code and Explanation



Menu Driven Singly Linked List Program in C

✅ 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:
  

Comments

Popular Posts

๐ŸŒ™