Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct node
- {
- int val;
- node *next;
- node *prev;
- };
- struct DLList
- {
- node *head ,*tail;
- DLList();
- void insert (int x);
- void addToHead(int x);
- void addToTail (int x);
- void delTail();
- void delHead();
- void Maximum();
- void Minimum();
- void del(int t);
- void print();
- };
- #include <iostream>
- using namespace std;
- DLList::DLList(){
- head=tail=NULL;
- }
- void DLList::insert (int x){
- node *n=new node();
- n->val=x;
- if(head == NULL){
- head=tail=n;
- head->prev=NULL;
- tail->next=NULL;
- return;
- }
- tail->next=n;
- n->prev=tail;
- tail=n;
- tail->next=NULL;
- }
- void DLList::del(int x){
- node *cur =head;
- if(cur==NULL)return;
- while(cur!=NULL){
- if(cur->val==x)break;
- cur=cur->next;
- }
- if(cur==NULL)return;
- if(cur==head && cur ==tail){
- head=tail=NULL;
- delete cur;
- return;
- }
- if(cur==head){
- head=head->next;
- delete cur;
- head->prev=NULL;
- return;
- }
- if(cur==tail){
- tail=tail->prev;
- delete cur;
- tail->next=NULL;
- return;
- }
- cur->next->prev=cur->prev;
- cur->prev->next=cur->next;
- delete cur;
- }
- void DLList::print(){
- node *cur=head;
- while(cur!=NULL){
- cout<<cur->val<<" ";
- cur=cur->next;
- }
- cout<<endl;
- }
- void DLList::addToHead(int x){
- node *n=new node();
- n->val=x;
- if(head==NULL){
- head=tail=n;
- n->prev=NULL;
- n->next=NULL;
- return;
- }
- n->next=head;
- head=n;
- n->prev=NULL;
- }
- void DLList::addToTail (int x){
- node *n=new node();
- n->val=x;
- if(head == NULL){
- head=tail=n;
- head->prev=NULL;
- tail->next=NULL;
- return;
- }
- tail->next=n;
- n->prev=tail;
- tail=n;
- tail->next=NULL;
- }
- void DLList::delHead(){
- node *cur =head;
- if(cur==NULL)
- return;
- if(head ==tail){
- head=tail=NULL;
- delete cur;
- return;
- }
- head=head->next;
- head->prev=NULL;
- delete cur;
- return;
- }
- void DLList:: delTail(){
- node *t=tail;
- if(head==NULL)
- return;
- if(head==tail){
- head=tail=NULL;
- delete t;
- return;
- }
- tail=tail->prev;
- tail->next=NULL;
- delete t;
- return;
- }
- void DLList::Maximum(){
- node *n=head;
- if(head==NULL)
- return;
- int max=head->val;
- while(n!=NULL){
- if(n->val > max)
- max=n->val;
- n=n->next;
- }
- cout<<"Maximum : "<<max<<endl;
- if(head==tail)
- return;
- int max2=head->val;
- n=head;
- while(n!=NULL){
- if(n->val>max2 && n->val<max)
- max2=n->val;
- n=n->next;
- }
- cout<<"Second Maximum : "<<max2<<endl;
- }
- void DLList::Minimum(){
- node *n=head;
- if(head==NULL)
- return;
- int min=head->val;
- while(n!=NULL){
- if(n->val < min)
- min=n->val;
- n=n->next;
- }
- cout<<"Minimum : "<<min<<endl;
- if(head==tail)
- return;
- int min2=head->val;
- n=head;
- while(n!=NULL){
- if(n->val < min2 && n->val > min)
- min2=n->val;
- n=n->next;
- }
- cout<<"Second Minimum : "<<min2<<endl;
- }
- int main(){
- DLList k;/*k=new DLList();*/
- k.insert(5);
- k.insert(7);
- /*
- k->insert(11);
- k->insert(-6);
- k->print();
- k->del(2);
- k->print();
- k->del(11);
- k->print();
- k->addToHead(12);
- */
- k.print();
- /*
- k->addToTail(-7);
- k->print();
- k->delHead();
- k->print();
- k->delTail();
- k->print();
- k->Maximum();
- k->Minimum();*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment