Professional Documents
Culture Documents
============================================
#include <iostream.h>
struct node {
node *back;
int data;
node* next;
};
class DoublyList {
public :
DoublyList () { h=t=NULL;}
void addTail(int d) {
if (isEmpty()){
h=new node;
t=h;
t->data=d;
t->next=NULL;
h->back=NULL;
}
else {
node *c=new node;
c->data=d;
t->next=c;
c->back=t;
t=c;
t->next=NULL;
}
}
void addHead(int d) {
if (isEmpty()){
h=new node;
t=h;
h->data=d;
h->back=NULL;
t->next=NULL;
}
else{
node *c=new node;
c->data=d;
c->next=h;
h->back=c;
h=c;
h->back=NULL;
}
x=t->data;
delete t;
t=c;
t->next=NULL;
return true;
}//end else
}// end if
else
return false;
}
bool removeHead(int &x) {
if (!isEmpty()){
if (h==t){
x=h->data;
delete h;
h=t=NULL;
return true;
}
else
{ node *c=h;
x=h->data;
h=h->next;
delete c;
h->back=NULL;
return true;
}//end else
}// end if
else
return false;}
}
bool RemovedIndex(int index){
int i=0;
if(isEmpty())
return false;
else
{
if(index==0)
removeHead(index);
else
{
node *p=NULL;
node *c=h;
while((c!=NULL)&&(i!=index)){
p=c;
c=c->next;
i++;
}
if(c==NULL)
i--;
if(i!=index)
return false;
else if(c!=NULL){
p->next=c->next;
delete c;
return true;
}
}
}
return true;
}
void print (){
node *c;
c=t;
while(c!=NULL){
cout<<c->data<<endl;
c=c->next;
}
}
DoublyList ls;
ls.addHead(10);
ls.addTail(20);
ls.addTail(30);
ls.insert(5);
ls.print();
}
http://www.ravianeducation.blogspot.com
FARHAN: 03008855006