Showing posts with label delete before specified position. Show all posts
Showing posts with label delete before specified position. Show all posts

Tuesday, October 7, 2025

Doubly Linked List

 Write a program to implement the following operations on the doubly linked list. 
(a) Insert a node at the front of the linked list.
(b) Insert a node at the end of the linked list.
(c) Delete the last node of the linked list.
(d) Delete a node before the specified position.


#include<stdio.h>
struct node{
    int data;
    struct node *next;
    struct node *prev;
};
struct node *start=NULL, *ptr=NULL, *preptr,*nn;
int value;
void create_dll(){
    printf("Enter -1 to stop; Enter a Value:");
    scanf("%d",&value);
    while(value!=-1){
        if(start==NULL){
            nn = (struct node*)malloc(1*sizeof(struct node));
            if(nn == NULL){
                printf("Memory Not Available");
                exit(0);
            }
            nn->data = value;
            nn->next = NULL;
            nn->prev = NULL;
            start=nn;
        }
        else
               insert_end(value);
        printf("Enter -1 to stop; Enter a Value:");
        scanf("%d",&value);
    }
}
void display(){
    if(start==NULL)
            printf("List is Empty.");
    else{
        for(ptr=start;ptr!=NULL;ptr=ptr->next)
                printf("%d <--> ",ptr->data);
    }
}

void insert_front(int value){
    nn = (struct node*)malloc(1*sizeof(struct node));
    if(nn == NULL){
        printf("Memory Not Available");
        exit(0);
    }
    nn->data = value;
    nn->prev = NULL;
    nn->next = start;
    if(start!=NULL)
        start->prev = nn;
    start = nn;
}


insert_end(int v){
    nn = (struct node*)malloc(1*sizeof(struct node));
    if(nn == NULL){
        printf("Memory Not Available");
        exit(0);
    }
    nn->data = v;
    nn->next = NULL;
    nn->prev = NULL;
    ptr=start;
    while(ptr->next!=NULL)
        ptr=ptr->next;
    nn->prev = ptr;
    ptr->next=nn;
}


void delete_first(){
    if(start==NULL){
        printf("List is Empty.");
        return;
    }
    ptr=start;
    printf("%d is deleted.",ptr->data);
    start=start->next;
    start->prev=NULL;
    free(ptr);
}


void delete_last(){
    if(start==NULL){
        printf("List is Empty.");
        return;
    }
    ptr=start;
    if(start->next==NULL){
        start=NULL;
        printf("%d is delete.",ptr->data);
        free(ptr);
    }
    else{
        while(ptr->next!=NULL){
            preptr=ptr;
            ptr=ptr->next;
        }
        preptr->next=NULL;
        printf("%d is delete.",ptr->data);
        free(ptr);
    }
}





void delete_before_position(){
    int count,position;
    struct node *cur;
    if(start==NULL){
        printf("List is Empty.");
        return;
    }
    printf("Enter a Position:");
    scanf("%d",&position);
      if(position<=1){
        printf("Enter Valid position before to delete a Node.");
        return;
    }
   if(position==2){
        delete_first();
        return;
    }
    ptr=start;preptr=start;cur=start;
    for(count=1;cur!=NULL&&count<position;count++){
            preptr=ptr;
            ptr=cur;
            cur=cur->next;
    }
    preptr->next=cur;
    cur->prev=preptr;
    printf("%d is deleted.",ptr->data);
    free(ptr);
}

void main(){
    int ch;
    while(1){
        printf("\n\nDoubly Linked List");
        printf("\n1. Create a List\n2. Display\n3. Insert at Front");
        printf("\n4. Insert a Node at last");
        printf("\n5. Delete a First Node");
        printf("\n6. Delete a Last Node");
        printf("\n7. Delete a node before specified position");
        printf("\n8. Exit\nEnter Your Choice:");
        scanf("%d",&ch);
        switch(ch){
            case 1: create_dll();   break;
            case 2: display();  break;
            case 3: printf("Enter a Value:");
                    scanf("%d",&value);
                    insert_front(value);
                    break;
            case 4: printf("Enter a Value:");
                    scanf("%d",&value);
                    insert_end(value);
                    break;
            case 5: delete_first();  break;
            case 6: delete_last();  break;
            case 7: delete_before_position();   break;
            case 8: exit(0);
            default: printf("Invalid Choice..");
        }
    }
}

Friday, August 29, 2025

Singly Linked List

Write a menu-driven program to implement the following operations on the singly linked list:

(a) Insert a node at the front of the linked list. 

(b) Insert a node at the end of the linked list.

(c) Insert a node such that the linked list is in ascending order. (According to the info. field)

(d) Delete the first node of the linked list.

(e) Delete the last node of the linked list.

  (f) Delete a node before the specified position


#include<stdio.h>
struct node{
    int data;
    struct node *next;
};
int value;
struct node *start=NULL,*ptr=NULL,*nn=NULL;
void create_list(){
    int stop_value;
    start=NULL;
    printf("Enter -1 to stop; Enter a value:");
    scanf("%d",&stop_value);
    while(stop_value!=-1){
        nn=(struct node*)malloc(sizeof(struct node));
        if(nn==NULL){
            printf("Memory Overflow.");
            exit(0);
        }
        nn->data=stop_value;
        nn->next=NULL;
        if(start==NULL)
            start=nn;
        else{
            for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
            ptr->next=nn;
        }
       printf("Enter -1 to stop; Enter a value:");
       scanf("%d",&stop_value);
    }
}
void insert_front(int value){
    nn=(struct node*)malloc(1*sizeof(struct node));
    if(nn==NULL){
        printf("Memory Overflow");
        return;
    }
    nn->data=value;
    nn->next=start;
    start=nn;
    printf("Insertion Done at Front.");
}
void insert_end(int value){
    nn=(struct node*)malloc(1*sizeof(struct node));
    if(nn==NULL){
        printf("Memory Overflow");
        return;
    }
    nn->data=value;
    nn->next=NULL;
    if(start==NULL)
        start=nn;
    else{
            ptr=start;
         while(ptr->next!=NULL)
                    ptr=ptr->next;


            ptr->next=nn;
    }
    printf("Insertion Done at End.");
}
void display(){
    if(start==NULL)
        printf("List is Empty.");
    else{
        printf("List is: ");
        for(ptr=start;ptr!=NULL;ptr=ptr->next)
            printf("%d --> ",ptr->data);
    }
}

void sort(){
    int temp;
    struct node *p1,*p2;
    for(p1=start;p1!=NULL;p1=p1->next){
        for(p2=p1->next;p2!=NULL;p2=p2->next){
            if(p1->data > p2->data){
                temp=p1->data;
                p1->data = p2->data;
                p2->data = temp;
            }
        }
    }
}
void insert_in_sorted(){
    struct node *preptr;
    sort();
    nn=(struct node*)malloc(1*sizeof(struct node));
    if(nn==NULL){
        printf("Memory Overflow");
        return;
    }
    nn->data=value;
    nn->next=NULL;
    if(start==NULL || start->data > nn->data){
            nn->next=start;
            start=nn;
            return;
    }
    ptr=start;
    preptr=start;
    while(ptr!=NULL && ptr->data < nn->data){
            preptr=ptr;
            ptr=ptr->next;
    }
    nn->next=ptr;
    preptr->next=nn;
}
void delete_first(){
    if(start==NULL){
        printf("List is Empty.");
        return;
    }
    ptr=start;
    printf("%d is deleted.",ptr->data);
    start=start->next;
    free(ptr);
}
void delete_last(){
    struct node *preptr;
    if(start==NULL){
        printf("List is Empty.");
        return;
    }
    for(preptr=start,ptr=start;ptr->next!=NULL;ptr=ptr->next)
            preptr=ptr;
    preptr->next=NULL;
    printf("%d is deleted.",ptr->data);
    if(start->next==NULL)
        start=NULL;
    free(ptr);
}
void delete_before_position(){
    int pos,count;
    struct node *preptr,*cur;
    if(start==NULL){
        printf("List is already Empty.");
        return;
    }
    printf("Enter a Position");
    scanf("%d",&pos);
    if(pos<=1){
        printf("Enter Valid position before to delete a Node.");
        return;
    }
    if(pos==2){
        delete_first();
        return;
    }
    ptr=start;preptr=start;cur=start;
    for(count=1;cur!=NULL&&count<pos;count++){
            preptr=ptr;
            ptr=cur;
            cur=cur->next;
    }
    preptr->next=cur;
    printf("%d is deleted.",ptr->data);
    free(ptr);
}

void main(){
    int ch;
    while(1){
        printf("\n\nSingly Linked List");
        printf("\n1. Create a Linked List");
        printf("\n2. Display a Linked List");
        printf("\n3. Insert a node at the front of Linked List");
        printf("\n4. Insert a node at the end of Linked List");
        printf("\n5. Insert a node such that the linked list is in ascending order.");
        printf("\n6. Delete the first node of the linked list.");
        printf("\n7. Delete the last node of the linked list.");
        printf("\n8. Delete a node before the specified position.");
        printf("\n9. Exit");
        printf("\nEnter your choice:");
        scanf("%d",&ch);
        switch(ch){
            case 1: create_list();
                    break;
            case 2: display();
                    break;
            case 3: printf("Enter a Value:");
                    scanf("%d",&value);
                    insert_front(value);
                    break;
            case 4: printf("Enter a Value:");
                    scanf("%d",&value);
                    insert_end(value);
                    break;
            case 5: printf("Enter a Value:");
                    scanf("%d",&value);
                    insert_in_sorted(value);
                    break;
            case 6: delete_first();
                    break;
            case 7: delete_last();
                    break;
            case 8: delete_before_position();
                    break;
            case 9: exit(0);
            default: printf("Invalid choice");
        }
    }
}