0% found this document useful (0 votes)
71 views

"Node - CPP" : #Include #Include #Include Class Public New

This C++ program defines a linked list class with methods to add, insert, search, remove, update, print, copy, and get the size of nodes in the linked list. A main function allows a user to select these operations on a list object and pass in parameters like values to add, search for, remove etc. The list class uses Node objects that hold the data values and pointers to the next and previous nodes in the linked list.

Uploaded by

Bella Angel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

"Node - CPP" : #Include #Include #Include Class Public New

This C++ program defines a linked list class with methods to add, insert, search, remove, update, print, copy, and get the size of nodes in the linked list. A main function allows a user to select these operations on a list object and pass in parameters like values to add, search for, remove etc. The list class uses Node objects that hold the data values and pointers to the next and previous nodes in the linked list.

Uploaded by

Bella Angel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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