Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.6 kB
1
Indexable
Never
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
struct node{
    int data;
    node *llink;
    node *rlink;
};
node *head = NULL;
node *ptr = NULL;
node *ptr1 = NULL;
void display(){
    ptr = head;
    while(ptr != NULL)
        {
            printf("%d\t", ptr->data);
            ptr = ptr->rlink;
        }
}
void front(){
    node *n = new node;
    n->llink = NULL;
    n->rlink = head;
    printf("\nEnter the data: \n");
    scanf("%d", &n->data);
    head = n;
}
void end(){
    node *n = new node;
    if(head == NULL)
    {
        printf("\nNo set found.Inserting At front\n");
        front();
        return;
    }
    ptr = head;
    while(ptr->rlink != NULL)
        ptr = ptr->rlink;
    ptr->rlink = n;
    n->llink = ptr;
    n->rlink = NULL;
    printf("\nEnter the data: \n");
    scanf("%d", &n->data);
}
void middle(){
    node *n = new node;
    int key, flag = 0;
    if(head == NULL){
        printf("\nNo Set found, Inserting at front.\n");
        front();
        return;
    }
    ptr = head;
    printf("\nEnter the key: \n");
    scanf("%d", &key);
    while(ptr->rlink!=NULL)
    {
        if(ptr->data == key){
            n->rlink = ptr->rlink;
            n->llink = ptr;
            ptr->rlink = n;
            ptr->rlink->llink = n;
            printf("\nEnter the Data: \n");
            scanf("%d", &n->data);
            flag = 1;
            break;
        }
        ptr = ptr->rlink;
    }
    if(ptr->rlink==NULL && ptr->data == key){
        flag = 1;
        printf("\nWrong Operation.\n");
        return;
    }
    if(flag == 0)
        printf("\nNo Key Found.");
}
void d_front(){
    ptr = head;
    if(ptr == NULL)
        printf("No Set found.");
    else
        head = ptr->rlink;
}
void d_end(){
    ptr = head;
    if(ptr == NULL)
        printf("No Set found.");
    else
        {
            while(ptr->rlink != NULL)
            {
                ptr1 = ptr;
                ptr = ptr->rlink;
            }
        ptr1->rlink = NULL;
        }
}
void d_middle(){
    int key, flag = 0;
    ptr = head;
    if(ptr == NULL)
        printf("No Set Found.");
    else
        {
            printf("Enter the key: ");
            scanf("%d", &key);
            while(ptr->rlink!=NULL)
            {
                if(ptr->data == key){
                    ptr1->rlink = ptr->rlink;
                    flag = 1;
                    break;
                }
                ptr1 = ptr;
                ptr = ptr->rlink;
            }
        }
        if(flag == 0)
            printf("No key Found.");
}
int main(){
    int ch;
    while(1)
    {
        printf("\n1.Insertion At Front. \n2.Insertion At End. \n3.Insertion At Middle. \n4.Deletion At Front \n5.Deletion At Middle \n6.Deletion At End \n7.Exit Operation. \nEnter a choice below: \n");
        scanf("%d", &ch);
        if(ch == 1)
        {
            front();
            display();
        }else if(ch == 2)
        {
            end();
            display();
        }
        else if(ch == 3)
        {
            middle();
            display();
        }else if(ch == 4){
            d_front();
            display();
        }else if(ch == 5){
            d_middle();
            display();
        }else if(ch == 6){
            d_end();
            display();
        }else if (ch == 7)
            exit(0);
        else 
        printf("Invalid Choice.");
    }
}