Skip to content

Instantly share code, notes, and snippets.

@zainulhasan
Created August 3, 2015 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zainulhasan/70de836be6d5139bad29 to your computer and use it in GitHub Desktop.
Save zainulhasan/70de836be6d5139bad29 to your computer and use it in GitHub Desktop.
/******************************
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