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...

Doubly Linked List Implementation in C

 

PROGRAM



#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
struct node
{
        struct node *pre;
        int data;
        struct node *next;
};
void insert_node_at_begining(struct node**head,int data)
{
        struct node* newnode=(struct node*)malloc(sizeof(struct node));
        if(newnode==NULL)
        {
                printf("Memory allocation faild!!\n");
                return;
        }
        newnode->pre=NULL;
        newnode->data=data;
        newnode->next=*head;
        if(*head!=NULL)
        {

                (*head)->pre=newnode;

        }
        *head=newnode;


}
void insert_node_at_end(struct node**head,int data)
{
        struct node*newnode=(struct node*)malloc(sizeof(struct node));
        struct node*temp=*head;
        if(newnode==NULL)
        {
                printf("Memory allocation failed!!\n");
                return;
        }
        newnode->data=data;
        newnode->pre=NULL;
        newnode->next=NULL;
        if(*head==NULL)
        {
                *head=newnode;
                return;
        }
        while(temp->next!=NULL)
        {
                temp=temp->next;
        }
        newnode->pre=temp;
        temp->next=newnode;


}
void insert_node_at_specific_position(struct node**head,int data,int pos)
{
        struct node*newnode=(struct node*)malloc(sizeof(struct node));
        if(newnode==NULL)
        {
                printf("Memory allocation failed!!\n");
                return;
        }
        newnode->pre=NULL;
        newnode->data=data;
        newnode->next=NULL;
        if(pos<=0)
        {
                printf("invalid position!\n");
                free(newnode);
                return;
        }
        if(pos==1)
        {
        newnode->next=*head;
        if(*head!=NULL)
        (*head)->pre=newnode;
        *head=newnode;
        return;
        }
        struct node*temp=*head;
        for(int i=1;i<pos-1&&temp!=NULL;i++)
        {
                temp=temp->next;
        }
        if(temp==NULL)
        {
                printf("position %d is out of range:\n",pos);
                free(newnode);
                return;
        }
        newnode->next=temp->next;
        newnode->pre=temp;

        if (temp->next != NULL)
        temp->next->pre=newnode;

    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;
        if(*head!=NULL)
        {
                (*head)->pre=NULL;
        }
        temp->next=NULL;
        free(temp);
}
void delete_node_at_end(struct node **head) {
    if (*head == NULL) {
        printf("List is empty.\n");
        return;
    }

    struct node *temp = *head;

    // If only one node is present
    if (temp->next == NULL) {
        printf("%d is deleted at the end.\n", temp->data);
        free(temp);
        *head = NULL;
        return;
    }

    // Traverse to the last node
    while (temp->next != NULL) {
        temp = temp->next;
    }

    printf("%d is deleted at the end.\n", temp->data);
    temp->pre->next = NULL;
    free(temp);
}
void delete_node_at_position(struct node **head, int pos) {
    if (*head == NULL) {
        printf("List is empty.\n");
        return;
    }

    if (pos <= 0) {
        printf("Invalid position.\n");
        return;
    }

    struct node *temp = *head;

    // Delete the first node
    if (pos == 1) {
        printf("%d is deleted from position %d.\n", temp->data, pos);
        *head = temp->next;
        if (*head != NULL) {
            (*head)->pre = NULL;
        }
        free(temp);
        return;
    }

    // Traverse to the node at (pos - 1)
    for (int i = 1; i < pos && temp != NULL; i++) {
        temp = temp->next;
    }

    if (temp == NULL) {
        printf("Position %d is out of range.\n", pos);
        return;
    }

    printf("%d is deleted from position %d.\n", temp->data, pos);

    if (temp->pre != NULL) {
        temp->pre->next = temp->next;
    }

    if (temp->next != NULL) {
        temp->next->pre = temp->pre;
    }

    free(temp);
}



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,choice,pos;
        struct node*head=NULL;
do
{
        printf("============MENU============\n");
        printf("1).INSERT NODE AT BEGGINING:\n");
        printf("2).INSERT NODE AT END\n");
        printf("3).INSERT NODE AT SPECIFIC:\n");
        printf("4).DELETE NODE AT BEGINING:\n");
        printf("5).DELETE NODE AT END:\n");
        printf("6).DELETE NODE AT SPECIFIC:\n");
        printf("7).DISPLAY THE LIST:\n");
        printf("8).EXIT PROGRAM:\n");
        printf("=============================\n");
        printf("Enter choice:\n");
        scanf("%d",&choice);
        switch(choice)
        {
                case 1:
                        printf("Enter Data You Want To add at Begining:\n");
                        scanf("%d",&data);
                        insert_node_at_begining(&head,data);
                        break;
                case 2:
                        printf("Enter data you want to add at end:\n");
                        scanf("%d",&data);
                        insert_node_at_end(&head,data);
                        break;
                case 3:
                        printf("Enter position you want to insert node:\n");
                        scanf("%d",&pos);
                        printf("Enter data you want add:\n");
                        scanf("%d",&data);
                         insert_node_at_specific_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 position you want to delete:\n");
                        scanf("%d",&pos);
                        delete_node_at_position(&head,pos);
             break;
                case 7:
                        display(&head);
                        break;
                case 8:

                        printf("Wait 3 seconds:\n");
                        sleep(3);
                        printf("Exit program successfully:\n");
                        exit(0);
                        break;
                default:
                        printf("!invalid choice try again:\n");
        }


}while(choice!=9);
}



๐Ÿ’ก Doubly Linked List Implementation in C

This project implements a full-featured Doubly Linked List in C, supporting insertion and deletion at various positions, along with display functionality. Each node contains three parts: a pointer to the previous node, data, and a pointer to the next node.


๐Ÿ“Œ Structure Definition

struct node {
    struct node *pre;
    int data;
    struct node *next;
};

This structure defines a single node in the doubly linked list.


๐ŸŸข insert_node_at_begining()

This function inserts a new node at the beginning of the list.

  • Allocates memory for a new node using malloc()
  • Updates the next of new node to point to the current head
  • Updates the pre of the old head (if it exists)
  • Makes the new node the new head

๐ŸŸข insert_node_at_end()

Inserts a new node at the end of the list.

  • If list is empty, sets the new node as head
  • Otherwise, traverses to the last node and updates its next
  • Sets the new node’s pre to the previous last node

๐ŸŸข insert_node_at_specific_position()

Inserts a node at a user-specified position.

  • Handles special case when position = 1 (inserts at beginning)
  • Traverses up to (position - 1)
  • Inserts node between temp and temp->next
  • Handles position validation and edge cases

๐Ÿ”ด delete_node_at_first()

Deletes the node at the beginning of the list.

  • Updates the head to point to the next node
  • Frees memory of the old head
  • Handles empty list case

๐Ÿ”ด delete_node_at_end()

Deletes the node at the end of the list.

  • Handles single-node case
  • Traverses to last node
  • Updates pre->next to NULL and frees last node

๐Ÿ”ด delete_node_at_position()

Deletes a node at a specific position.

  • If position is 1, deletes head
  • Otherwise, traverses to the node at position
  • Updates the pre and next links accordingly

๐Ÿ‘€ display()

This function displays the entire list from beginning to end.

  • Traverses using next pointer
  • Prints each node in the format: data <->

๐Ÿง  main()

The main() function contains a menu-driven interface to allow user interaction.

  • Options: Insert, Delete, Display, Exit
  • Uses a do-while loop and switch-case for selection
  • Performs sleep before graceful exit

๐Ÿ“Ž Summary

This program is a strong example of how doubly linked lists work. Understanding how each node connects forward and backward provides deeper insight into pointer manipulation and memory handling in C.

If you're preparing for a technical interview or practicing DSA in C, mastering linked lists like this is a must!



Comments

Popular Posts

๐ŸŒ™