Created
August 3, 2015 05:33
-
-
Save zainulhasan/70de836be6d5139bad29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************** | |
DLinkedList.cpp | |
Auther:Syed Zain Ul hasan | |
*****************************/ | |
#include <iostream> | |
using namespace std; | |
struct Node | |
{ | |
int data; | |
Node* next; | |
Node* previous; | |
}; | |
class DLinkList | |
{ | |
private: | |
Node* first; | |
Node* last; | |
public: | |
DLinkList() | |
{ | |
first=last=NULL; | |
} | |
void push_back(int x) | |
{ | |
Node* tmp=new Node(); | |
tmp->data=x; | |
if(first==NULL){ | |
tmp->next=NULL; | |
tmp->previous=NULL; | |
first=last=tmp; | |
}else{ | |
last->next=tmp; | |
tmp->previous=last; | |
tmp->next=NULL; | |
last=tmp; | |
} | |
} | |
void push_front(int x) | |
{ | |
Node* tmp=new Node(); | |
tmp->data=x; | |
if(first==NULL){ | |
tmp->next=NULL; | |
tmp->previous=NULL; | |
first=last=tmp; | |
}else{ | |
first->previous=tmp; | |
tmp->next=first; | |
tmp->previous=NULL; | |
first=tmp; | |
} | |
} | |
void pop_back(){ | |
if(first!=NULL){ | |
Node* tmp=last; | |
last=last->previous; | |
last->next=NULL; | |
delete tmp; | |
} | |
} | |
void pop_front(){ | |
if(first!=NULL){ | |
Node* tmp=first; | |
first=first->next; | |
first->previous=NULL; | |
delete tmp; | |
} | |
} | |
void display(){ | |
Node* tmp=first; | |
while(tmp!=NULL){\ | |
cout<<tmp->data<<" "; | |
tmp=tmp->next; | |
} | |
cout<<endl; | |
} | |
}; | |
int main() | |
{ | |
DLinkList dl; | |
int val; | |
char choice; | |
while(true) | |
{ | |
cout<<"Enter The Number : "; | |
cin>>val; | |
dl.push_back(val); | |
cout<<"Do you want to enter another number(y/n): "; | |
cin>>choice; | |
if(choice!='y') | |
break; | |
} | |
cout<<"The Number you enteres are "<<endl; | |
dl.display(); | |
int x; | |
cin>>x; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment