Description

Doubly Linked list

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

struct node{
    struct node *prev;
    int num;
    struct node *next;
};
typedef struct node NODE;

int main()
{
    NODE *start=NULL, *ptr, *current;
    int item;   
    char ch;
    do{
        ptr = (NODE*)malloc(sizeof(NODE));
        printf("Enter the item : ");
        scanf("%d", &item);
        ptr->prev = NULL;
        ptr->num = item;
        ptr->next =NULL;

        if(start==NULL){
            start = ptr;
            current = ptr;
        }
        else{
            current->next = ptr;
            ptr->prev = current;
            current = current -> next;
        }
        printf("Do You want to Enter more Node Press[y/Y] : ");
        scanf("%s", &ch);

    }while(ch=='y' || ch=='Y');

    NODE *temp;
    temp = start;

    // Print element
    printf("\nPrint Entered Element : \n");
    while(temp!=NULL){
        printf("%d ", temp->num);
        temp = temp->next;
    }
    return 0;
}

Insertion in Being in Doubly Linked list in C

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

struct node
{
    struct node *prev;
    int num;
    struct node *next;
};

typedef struct node NODE;
NODE *start=NULL, *ptr, *current;
void display(NODE *head){
    NODE *temp;
    temp = head;
    while(temp!=NULL){
        printf("%d ", temp->num);
        temp = temp->next;
    }
}
int main(){
    int item;
    char ch;
    do
    {
        printf("\nEnter the Node Item : ");
        scanf("%d", &item);
        ptr = (NODE*)malloc(sizeof(NODE));
        ptr->prev = NULL;
        ptr->num = item;
        ptr->next = NULL;

        if(start==NULL){
            start = ptr;
            current = ptr;
        }
        else{
            current -> next = ptr;
            ptr->prev = current;
            current = current->next;
        }

        printf("\nDo You want to More NODE press[y/Y] : ");
        scanf("%s", &ch);

    } while (ch=='y' || ch=='Y');

    // insert In Begin the item in dubly linked list
    printf("\nBefore Insertion \n");
    display(start);

    printf("\nEnter the element do you want to insert the NODE : ");
    scanf("%d", &item);
    ptr = (NODE*)malloc(sizeof(NODE));

    ptr->prev = NULL;
    ptr->num = item;
    ptr->next = start;
    start->prev = ptr;

    start = ptr;
    printf("\nAfter Insertion \n");
    display(start);

    
    
    return 0;
}

Insertion Last in Doubly Linked in C

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

struct node
{
    struct node *prev;
    int num ;
    struct node *next;
};

typedef struct node NODE;
NODE *start=NULL, *ptr, *current;

void Display(NODE *head){
    NODE *temp;
    temp = start;

    while(temp!= NULL){
        printf("%d ", temp->num);
        temp = temp->next;
    }
}

int main(){

    int item;
    char ch;

    do
    {
        ptr = (NODE*)malloc(sizeof(NODE));
        printf("Enter the Item in the node : ");
        scanf("%d", &item);

        ptr->prev = NULL;
        ptr->num = item;
        ptr->next = NULL;


        if(start == NULL){
            start = ptr;
            current = ptr;
        }
        else{
            current->next = ptr;
            ptr->prev = current;
            current = current -> next;
        }

        printf("\nDo You want to add more NODE press[y/Y] : ");
        scanf("%s", &ch);
    } while (ch=='y' || ch=='Y');


    //  print before insertion
    Display(start);


    // insertion Start here

    ptr = (NODE*)malloc(sizeof(NODE));
    printf("\nEnter the NODE Element : ");
    scanf("%d", &item);

    ptr->prev = NULL;
    ptr->num = item;
    ptr->next = NULL;



    NODE *p = start;

    while (p->next!=NULL)
    {
        p = p->next;
    }

    

    p->next = ptr;
    ptr -> prev = p;
    ptr->next = NULL;


    Display(start);
    
    return 0;
}

Insertion At the Particular Position in Doubly Linked list in c

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct node
{
    struct node *prev;
    int num;
    struct node *next;
};

typedef struct node NODE;

NODE *start=NULL, *ptr, *current;

void Display(NODE *head){
    NODE *temp;
    temp = head;
    while (temp!=NULL)
    {
        printf("%d ", temp->num);
        temp = temp -> next;
    }
    
}


int main(){

    int item;
    char ch;

    do
    {
        ptr = (NODE*)malloc(sizeof(NODE));
        printf("\nEnter the Element of NODE : ");
        scanf("%d", &item);

        ptr->prev = NULL;
        ptr->num = item;
        ptr->next = NULL;

        if(start==NULL){
            start = ptr;
            current = ptr;
        }
        else{
            current -> next = ptr;
            ptr -> prev = current;
            current = current -> next;
        }


        printf("\nDo You want to Add more NODE press[y/Y] : ");
        ch = getche();
    } while (ch=='y' || ch=='Y');

    // print the element before insertion
    printf("\nBefore Insertion \n");
    Display(start);

    // insertion the node

    ptr = (NODE*)malloc(sizeof(NODE));
    printf("\nEnter the Element of the Inserted NODE : ");
    scanf("%d", &item);
    ptr->prev = NULL;
    ptr->num = item;
    ptr->next = NULL;

    int loc;
    printf("\nEnter the location where you want to insert the NODE : ");
    scanf("%d", &loc);


    int i=1;
    NODE *temp = start;
    while(i<loc-1){
        temp = temp->next;
        i++;
    }

    ptr -> next = temp -> next;
    temp -> next -> prev = ptr;
    ptr -> prev = temp;
    temp -> next = ptr;


    // Display linked list after insertion
    printf("\nAfter Insertion\n");
    Display(start);
    
    return 0;
}

All Operation of Insertion and Deletion in Doubly linked List in C

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

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

typedef struct node NODE;

NODE* insert_beg(NODE *start,int item);
NODE* insert_end(NODE *start, int item);
NODE* search_element(NODE *start, int key);
NODE* delete_beg(NODE *start);
NODE* delete_end(NODE *start);
NODE* delete_entire(NODE *start);
NODE* insert_pos(NODE *start, int loc, int item);
NODE* delete_pos(NODE *start, int loc);
NODE* display(NODE *start);

int main(){
    NODE *start=NULL;

    int item, choice, key, loc;
    char ch;

    do{

        printf("Please Choose Any Option\n\n1.Insert Begin\n2.Insert End\n3.Search Element in Linked List\n4. Delete Begin\n5.Delete End\n6.Delete Entire Linked List\n7.Insert Position\n8.Deletr Position\n9.Display Linked List\n\nChoose Option : ");
        scanf("%d", &choice);
        switch(choice){
            case 1:
            printf("Enter the element : ");
            scanf("%d", &item);
            start = insert_beg(start, item);
            break;

            case 2:
            printf("Enter the element : ");
            scanf("%d", &item);
            start = insert_end(start, item);
            break;

            case 3:
            printf("Enter the Element do you want ot search : ");
            scanf("%d", &key);
            search_element(start, key);
            break;

            case 4:
            start = delete_beg(start);
            break;

            case 5:
            start = delete_end(start);
            break;

            case 6:
            start = delete_entire(start);
            break;

            case 7:
            printf("Enter the element do you want to insert : ");
            scanf("%d", &item);
            printf("Enter the location where you want to insert : ");
            scanf("%d", &loc);
            start = insert_pos(start, loc, item);
            break;

            case 8:
            printf("Enter the location where you want to delete : ");
            scanf("%d", &loc);
            start = delete_pos(start, loc);
            break;

            case 9:
            display(start);
            break;

        }
        printf("\nDo You want to perform more operation : ");
        ch = getch();
    }while(ch=='y' || ch=='Y');
    return 0;
}

NODE *insert_beg(NODE *start, int item){
    NODE *ptr;
    ptr = (NODE*)malloc(sizeof(NODE));
    ptr->prev = NULL;
    ptr->data = item;
    ptr->next = NULL;

    if(start==NULL){
        start = ptr;
    }
    else{
        ptr->next = start;
        start->prev = ptr;
        start = ptr;
    }
    return start;
}

NODE *insert_end(NODE *start, int item){
    NODE *temp = start, *ptr;
    ptr = (NODE*)malloc(sizeof(NODE));
    ptr->prev = NULL;
    ptr->data = item;
    ptr->next = NULL;

    if(start==NULL){
        start = ptr;
    }
    else{
        while(temp->next!=NULL){
            temp = temp->next;
        }
        temp->next = ptr;
        ptr->prev = temp;
    }

    return start;
}
NODE *search_element(NODE *start, int key){
    NODE *temp = start;
    int i=1, f=0;
    while(temp!=NULL){
        if(temp->data == key){
            f = 1;
            break;
        }
        i++;
        temp = temp->next;
    }
    if(f==1){
    printf("Present Element %d, at loc %d", key, i);
    }
    else{
        printf("Element Not present");
    }
}
NODE *delete_beg(NODE *start){
    NODE *temp = start;
    start = temp->next;
    start ->prev = NULL;
    free(temp);
    return start;
}
NODE *delete_end(NODE *start){
    NODE *temp = start;

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

    temp -> prev -> next = NULL;
    temp -> prev = NULL;
    free(temp);
    return start;
}
NODE *delete_entire(NODE *start){
    NODE *temp = start;
    NODE *p;
    while(temp!= NULL){
        p = temp ;
        temp = p->next;
        free(p);
        start = temp;
    }
    return start;
}
NODE *insert_pos(NODE *start, int loc, int item){
    NODE *ptr, *temp = start;
    ptr = (NODE*)malloc(sizeof(NODE));
    ptr->prev = NULL;
    ptr->data = item;
    ptr->next = NULL;

    int i = 1;

    while(i<loc-1){
        temp = temp->next;
        i++;
    }

    temp -> next -> prev = ptr;
    ptr -> next = temp -> next;
    temp -> next = ptr;
    ptr -> prev = temp;

    return start;
}

NODE *delete_pos(NODE *start, int loc){
    NODE *temp = start;
    int i=1;
    while(i<loc){
        temp = temp->next;
        i++;
    }

    temp -> prev-> next = temp -> next ;
    temp -> next -> prev = temp -> prev;
    temp->next = NULL;
    temp->prev = NULL;

    free(temp);

    return start;
}

NODE *display(NODE *start){
    NODE *temp=start;
    while(temp!=NULL){
        printf("%d " , temp->data);
        temp = temp->next;
    }

}

 Please Comment...

Comments

Login is mandatory to comment Please login

Recommended Posts

C program to check eligible for vote

Write a program to check that person is eligible for voting or not.

Voting program in C language using switch case

Write a program to check that person is eligible for voting or not using switch case.

C program to convert decimal to binary without Array

We can convert any decimal number into binary number by c program without using array.

A C program for checking whether a given line is a comment

Write a program in c to check whether a given line is comment or not.

Linear search in c | Algorithm of Linear search | c programming

It is the easiest search algorithm because it starts at the beginning and works through each element in a list until the desired element is located.