Ds Assign. No #4: Maria Sarfraz Shaikh 21510102 Sy Cse S6

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

DS ASSIGN.

No #4

MARIA SARFRAZ SHAIKH


21510102
SY CSE
S6

Doubly Linked List

#include<bits/stdc++.h>
#define jyot main
#define lupin(a,b,c) for(int(a)=(b);(a)<(c);(a)++)
#define lupim(a,b,c) for(int(a)=(b);(a)<=(c);(a)++)
#define emp emplace_back
#define MARIASHAIKH ios_base::sync_with_stdio(false) ;
#define Patil cin.tie(0);cout.tie(0) ;
#define khtm cout<<endl ;
#define int long long int
using namespace std;

class Node{
    public:
        int data;
        Node* prev;
        Node* next;

        Node(int d){
            this->data=d;
            this->next=NULL;
            this->prev=NULL;
        }
};

void print(Node* &head){


    Node* ptr=head;
    while(ptr!=NULL){
        cout<<ptr->data<<" ";
        ptr=ptr->next;
    }
    cout<<endl;
}

int32_t len(Node* &head){


    int l=0;
    Node* ptr=head;
    while(ptr!=NULL){
        l++;
        ptr=ptr->next;
    }
    return l;
}

void inathead(Node* &head, int d){


    Node* ptr=new Node(d);
    ptr->next=head;
    head->prev=ptr;
    head=head->prev;
}

void delathead(Node* &head){


    head=head->next;
    head->prev=NULL;
}

void delattail(Node* head,Node* tail){


    Node* ptr=head;
    while(ptr->next!=NULL){
        ptr=ptr->next;
    }
    ptr=ptr->prev;
    ptr->next=NULL;
    tail=ptr;
}

void inatmid(Node* &head, int val, int pos){


    Node* ptr=head;
    pos--;
    while(pos--){
        ptr=ptr->next;
    }
    Node* i=new Node(val);
    Node* after=ptr->next;
    ptr->next=i;
    i->prev=ptr;
    i->next=after;
    after->prev=i;
}

void inattail(Node* &head, int d,Node* tail){


    Node* ptr=head;
    Node* temp=new Node(d);
    while(ptr->next!=NULL){
        ptr=ptr->next;
    }
    ptr->next=temp;
    temp->prev=ptr;
    tail=temp;
}
void deleteatmid(Node* &head, int pos){
    pos--;
    if(head==NULL){
        return;
    }
    Node* temp=head;
    while(pos--){
        temp=temp->next;
    }
    Node* cur=temp->next;
    temp->next=NULL;
    cur->prev=temp->prev;
    temp->prev->next=cur;
    temp->next=NULL;
    temp->prev=NULL;
}

int32_t maria(){
    Node* node1=new Node(50);
    Node* head=node1;
    Node* tail=node1;
    inathead(head,98);
    inathead(head,5);
    print(head);                       /************/

    inattail(head,5,tail);
    inattail(head,7,tail);
    inattail(head,8,tail);
    inattail(head,15,tail);
    print(head);                       /************/

    delathead(head);
    print(head);                      /************/

    delattail(head,tail);
    print(head);                       /************/

    inatmid(head,87,3);
    print(head);                       /************/

    deleteatmid(head,5);
    print(head);                       /************/

    return 0;
}
OUTPUT

You might also like