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

template<class Type> struct Node{ Type value; Node<Type> *linkNext; Node<Type> *linkPrev; }; template <class Type> class linkedlist{

private: Node<Type> *first; Node<Type> *last; public: linkedlist(); void search(Type data); void deletes(Type data); void addfront(Type data); void addlast(Type data); void printbackward(); void printforward(); void insert(Type data); void insertAfter(Type data, Node<Type>*node1) {Node<Type> *newNode=new Node<Type>; newNode->linkNext =node1->linkNext ; newNode->linkPrev =node1; newNode->value =data; if(node1->linkNext==NULL) { last=newNode; } node1->linkNext =newNode; } void insertBefore(Type data,Node <Type>*node1) {Node<Type> *newNode=new Node<Type>; newNode->linkPrev =node1->linkPrev ; newNode->linkNext =node1; newNode->value =data; if(node1->linkPrev ==NULL) { first=newNode; } node1->linkPrev =newNode; } }; template<class Type> linkedlist<Type>::linkedlist(){ first=NULL; last=NULL; } template<class Type> void linkedlist<Type>::addfront(Type data) { Node<Type> *newNode=new Node<Type>; if(first==NULL) { first=newNode; last=newNode; newNode->value=data; newNode->linkNext=NULL;

newNode->linkPrev=NULL; } else{ insertBefore(data,first); } } template<class Type> void linkedlist<Type>::addlast(Type data) { if(last==NULL) { addfront(data); } else{ insertAfter(data,last); } } template<class Type> void linkedlist<Type>::printforward() { if(first==NULL) cout<<"sorry the list is empty\n"; else{ Node<Type>*current=first; while(current!=NULL) { cout<<current->value;cout<<","; current=current->linkNext; } } } template<class Type> void linkedlist<Type>::printbackward() { if(first==NULL) cout<<"sorry the list is empty\n"; else{ Node<Type>*current=last; while(current!=NULL) { cout<<current->value;cout<<","; current=current->linkPrev; } } } template<class Type> void linkedlist<Type>::deletes(Type data) { if(first==NULL) cout<<"sorry the list is empty"; else if(first->value==data) { Node<Type> *temp=first; first=first->linkNext; first->linkPrev=NULL;

delete temp; if(first==NULL) last=NULL; } else if(last->value=data) { Node<Type> *temp=last; last=last->linkPrev; last->linkNext=NULL; delete temp; if(first==NULL) last=NULL; } else{ Node<Type> *current; Node<Type> *trailcurrent; bool found=false; current=first->linkNext; trailcurrent=first; while(!found && current !=NULL) { if(current->value==data) found=true; trailcurrent->linkNext=current->linkNext; delete current; if(trailcurrent->linkNext==NULL) last=trailcurrent; else{ trailcurrent=current; current=current->linkNext; } }

if(found) cout<<"the value has been deleted"; else cout<<"the value does not exist"; } } int main(void){ linkedlist<int> list; int choice,data; while(1){ cout<<"\n1.addfront\n" <<"2.addlast\n" <<"3.print Forward\n" <<"4.Print Backward \n" <<"5.delete\n" <<"enter your choice:"; cin>>choice; switch (choice){

case 1: cout<<"enter your value:\n"; cin>>data; list.addfront(data); break; }

You might also like