You are on page 1of 9

#include"Node.

cpp"
#include<stdlib.h>
#include<iostream>
class list{
public:
list(){

headnode=new Node();
headnode->setnext(NULL);
headnode->setprevious(NULL);
currentNode=NULL;
lastcurrentnode=NULL;
size=0;

};
Node* getheadnode(){return headnode;};
Node* getcurrentnode(){return currentNode;};
void setcurrentnode(Node* cn){this->currentNode=cn;};
int Get(){
if(currentNode!=NULL)
return currentNode->get();
};
void add(int addobj){
Node* newnode=new Node();
newnode->set(addobj);
if(currentNode!=NULL){
newnode->setnext(currentNode->getnext());
newnode->setprevious(currentNode);
//
(currentNode->getnext())->setprevious(newnode);
currentNode->setnext(newnode);
currentNode=newnode;
}
else{
newnode->setnext(NULL);
newnode->setprevious(headnode);
headnode->setnext(newnode);
lastcurrentnode=currentNode;
currentNode=newnode;
}
size++;
};
void print(){
Node* p;
p=headnode->getnext();
if(p==NULL)
{
cout<<"list is empty"<<endl;
return;
}
cout<<"list value is:"<<endl;
while(p!=NULL)
{
cout<<p->get()<<endl;

p=p->getnext();
}
cout<<endl;
};
void insertvalue(int x)
{
Node* pNode = headnode;
while(pNode!=NULL){
if(pNode->object!=x)
{
lastcurrentnode=pNode;
pNode=pNode->nextNode;
}
else
{
int s;

cout<<"enter the value of new node"<<endl;


cin>>s;
Node* nn=new Node();
nn->set(s);
nn->setnext(pNode->getnext());
nn->setprevious(pNode->getprevious());
pNode->setnext(nn);
pNode=pNode->nextNode;
size++;
}

};
Node* search(int val)
Node* pNode = headnode;
/* traverse the list */
while (pNode != NULL) {
/* Target! */
if(pNode->object == val)
{
return pNode;
}
/* move to the next one */
pNode = pNode->nextNode;
}
return NULL;

};

void remove(int key)


{
currentNode = headnode->getnext();
while(currentNode!=NULL){
if(currentNode->object!=key)
{
lastcurrentnode=currentNode;
currentNode=currentNode->nextNode;

}
else
{

(currentNode->getprevious())->setnext(currentNode->getnext());
currentNode->getnext()->setprevious(currentNode->getprevious());
Node* temp;
temp=currentNode;
currentNode=currentNode->getnext();
cout<<"this value is remove"<<temp->object<<endl;
delete temp;
size--;
}

}
};

void update(int val)


{
Node* pNode = headnode;
while(pNode!=NULL){
if(pNode->object!=val)
{
lastcurrentnode=pNode;
pNode=pNode->nextNode;
}
else
{
cout<<"enter the new value"<<endl;
int up;
cin>>up;
pNode->set(up);
pNode=pNode->nextNode;
}
}
};
int sizelist()
{
cout<<"link list size is";
return size;
}
void copy1( list& l){
list copylist;
l.currentNode=l.headnode->getnext();
if(l.currentNode!=NULL)
{
Node* nn=new Node();
nn->set(l.currentNode->get());
nn->setnext(NULL);
copylist.headnode->setnext(nn);
copylist.currentNode=nn;
l.currentNode=l.currentNode->getnext();
}

while(l.currentNode!=NULL)
{
Node* nn=new Node();
nn->set(l.currentNode->get());
nn->setnext(NULL);
nn->setprevious(l.currentNode->getprevious());
copylist.currentNode->setnext(nn);
copylist.currentNode=nn;
l.currentNode=l.currentNode->getnext();
}
copylist.print();
};
public:

Node* headnode;
Node* currentNode;
Node* lastcurrentnode;
int size;

};
enum choice {add=1,insert,search,remove1,update,print,copy1,size,END};
int enterChoice()
{
cout
<< "\nEnter your choice" << endl
<<"1-to enter number in list"<<endl
<< "2 to insert_at number in list" << endl
<< "3 - search a number in list" << endl
<< "4 -to remove the list" << endl
<< "5 -to update" << endl
<< "6 -to get vaalues "<<endl
<< "7-to copy values"<<endl
<<"8- to get the size of array"<<endl;
int menuChoice;
cin >> menuChoice;
return menuChoice;
}
void main(){
list list1;
int choice;
while ( ( choice = enterChoice() ) !=END)
{
switch ( choice )
{
case add:
cout<<"enter new node"<<endl;
int nn;
cin>>nn;
list1.add(nn);
break;

case insert:
cout<<"after which value u wanted to enter nodde"<<endl;
int val;
cin>>val;
list1.insertvalue(val);
break;
case search:
cout<<"which value you wanted to search"<<endl;
int x;
cin>>x;
list1.search(x);
break;
case remove1:
cout<<"what value you wanted to remove"<<endl;
int z;
cin>>z;
list1.remove(z);
break;
case update:
cout<<"what value you wanted to update"<<endl;
int s;
cin>>s;
list1.update(s);
break;
case print:
list1.print();
break;
case copy1:
list1.copy1(list1);
case size:
cout<<list1.sizelist();
}
}
}

system("pause");

You might also like