Thursday, August 28, 2025

DOUBLY LINKED LIST

#include <iostream>
 using namespace std;
 struct node{
    float data;
    node*prev;
    node*next;

 };
 node*head=NULL;
 void insertAtbeginning(float value){
node*NEW=new node();
NEW->data=value;
if(head==NULL){
    NEW->next=head;
    NEW->prev=NULL;
    head=NEW;
    return;
}
NEW->next=head;
NEW->prev=NULL;
head->prev=NULL;
head=NEW;

 }
 void insertatEnd(float value) {
    node* New = new node();
    New->data = value;
    if (head == NULL) {
        head=New;
        New->next=head;
        New->prev=NULL;
    }
    node* temp = head;
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = New;
    New->next = NULL;
    New->prev=temp;
}
void del(int position){
    if(head==NULL){
        cout<<"List is empty "<<endl;
        return;
    }
    if(position<1){
cout<<"invalid position";
return;
    }
    node*temp=head;
    if(position==1){
        head=head->next;
        if (head != NULL) {
            head->prev = NULL;
        }
        delete temp;
        return;
    }
    for(int i=1;temp!=NULL&&i<position;i++){
        temp=temp->next;
    }
    if(temp==NULL){
        cout<<"out of range "<<endl;
        return;
    }
    if(temp->next!=NULL){
        temp->next->prev=temp->prev;
    }
    if(temp->prev!=NULL){
temp->prev->next=temp->next;
    }
    delete temp;
}
void display() {
    node* temp = head;
    while (temp != NULL) {
        cout << temp->data << "<->";
        temp = temp->next;
    }
    cout << "NULL" << endl;
}
 int main() {
    insertAtbeginning(5.60);
    insertatEnd(67.78);
    insertatEnd(57.89);
    insertAtbeginning(87.98);
    display();
 return 0;
 }

No comments:

Post a Comment

MergeSort

  #include < iostream >   using namespace std ;   void merge ( vector < int >& n , int low , int mid , int high ){   ...