all_operation_on_singly_linked_list

 // all basic operation retleted to singly linked list

#include<bits/stdc++.h>
using namespace std;
class node{
    public:

    int data;
    node* next;
    node(int val){
        data=val;
        next=NULL;
    }

};

void insertAtTail(node* &head, int val){
    node* n=new node(val);
    node* temp=head;
    if(head==NULL){
       head=n;
       return;
    }
    while(temp->next!=NULL){
        temp=temp->next;
    }
    temp->next=n;
}

void display(node* head){
    node* temp=head;
    while(temp!=NULL){
        cout<<temp->data<<"->";
        temp=temp->next;
    }
    cout<<"NULL"<<endl;
}

void insertAtHead(node* &head, int val){
    node* n=new node(val);
    node* temp=head;
    if(temp==NULL){
        head=n;
        return;
    }
    n->next=head;
    head=n;

}

void insertAtAnywhere(node* &head, int preVal, int val){
    node* n=new node(val);
   
    node* temp=head;
    while(temp->data!=preVal){
        temp=temp->next;

    }

    node* temp1=temp->next;

    temp->next=n;
    n->next=temp1;

}

void deleteAtHead(node* &head){

    if(head==NULL){
        cout<<"linked list is empty no element here to delete"<<endl;
    }

    node* todelete=head;

    head=head->next;
     
    delete(todelete);

}

void deleteAtTail(node* &head){
    if(head==NULL){
        return;
    }
    if(head->next==NULL){
        delete(head);
        return;
    }
    node* second_last=head;
    while(second_last->next->next!=NULL){
        second_last=second_last->next;
    }

    delete(second_last->next);

    second_last->next=NULL;


}

void deleteAnyWhere(node* &head, int val){

    if(head==NULL){
        return;
    }
    if(head->next==NULL){
        delete(head);
        return;
    }

    node* temp=head;
    while(temp->next->data!=val){
        temp=temp->next;
    }

    node* todelete=temp->next;
    temp->next=temp->next->next;

    delete(todelete);    



}

bool searchElement(node* &head, int key){
    if(head==NULL){
        return false;
    }
    bool flag=false;
    node* temp=head;

    while(temp!=NULL){
        if(temp->data==key){
            flag=true;
            break;
        }
        temp=temp->next;
    }
   

    return flag;

}

void reverseElement(node* &head){
    node* current=head;
    node* prev=NULL;
    node* next=NULL;
    while(current!=NULL){
        next=current->next;
        current->next=prev;
        prev=current;
        current=next;
    }
    head=prev;

}

int main(){

    node* head=NULL;
    insertAtTail(head, 1);
    insertAtTail(head, 2);
    insertAtTail(head, 3);
    insertAtTail(head, 4);

    display(head);

    insertAtHead(head, 5);

    display(head);

    insertAtAnywhere(head, 2, 6);

    display(head);

    cout<<"from here all deletion program will be performed"<<endl;

    deleteAtHead(head);

    display(head);


    deleteAtTail(head);

    display(head);

    deleteAnyWhere(head, 6);

    display(head);

    cout<<searchElement(head, 3)<<endl;

    reverseElement(head);

    display(head);

    return 0;

}

Comments

Popular posts from this blog

priority_queue

My first website