#include<iostream>
using namespace std;
class Node
{
private :
int data;
Node* next;
public:
Node() {};
void SetData(int aData) { data = aData; };
void SetNext(Node* aNext) { next = aNext; };
int Data() { return data; };
Node* Next() { return next; };
};
class List
using namespace std;
class Node
{
private :
int data;
Node* next;
public:
Node() {};
void SetData(int aData) { data = aData; };
void SetNext(Node* aNext) { next = aNext; };
int Data() { return data; };
Node* Next() { return next; };
};
class List
{
Node *head;
public:
List() { head = NULL; };
void Print();
void Add(int data);
void Append();
void find();
void Delete();
};
void List::Print()
{
Node *tmp = head;
while ( tmp != NULL )
{
cout << tmp->Data();
cout << " --> ";
tmp = tmp->Next();
}
cout << "NULL" << endl;
}
void List::Add(int data)
{
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(NULL);
Node *tmp = head;
if ( tmp != NULL )
{
while ( tmp->Next() != NULL )
{
tmp = tmp->Next();
}
tmp->SetNext(newNode);
}
else
{
head = newNode;
}
}
void List::Append()
{
Node* newNode = new Node();
Node *tmp = head;
Node *temp = head;
int n,data;
cout<<"Enter the node number : ";
cin>>n;
cout<<"Enter the data : ";
cin>>data;
newNode->SetData(data);
for( int i=1; i<n; i++)
{ tmp = tmp->Next(); }
temp = tmp->Next();
tmp->SetNext(newNode);
newNode->SetNext(temp);
}
void List::find()
{
int n, count = 0;
cout<<"Enter data to find in the list : ";
cin>>n;
Node *tmp = head;
while ( tmp != NULL )
{
count = count + 1;
if( n == tmp->Data())
{
cout<<"The data found on node number "<< count <<endl ;
break;
}
tmp = tmp->Next();
}
}
void List::Delete()
{
int n, count = 0;
cout<<"Enter data to delete : ";
cin>>n;
Node *tmp = head;
Node *temp = head;
while ( tmp != NULL )
{
count = count + 1;
if( n == tmp->Data())
{
break;
}
tmp = tmp->Next();
}
for( int j = 1; j < count-1; j++ )
{
temp = temp->Next();
}
temp->SetNext(tmp->Next());
delete tmp;
}
int main()
{
int n;
char c;
List mylist;
mylist.Add(10);
mylist.Add(20);
mylist.Add(30);
mylist.Add(40);
mylist.Add(50);
mylist.Add(60);
mylist.Add(70);
mylist.Add(80);
mylist.Add(90);
Choice:
cout<<"\n*********************** Please enter your choice ***********************\n\n ";
cout<<" A) To add node at the end of list \n ";
cout<<" B) To add node anywhere in the list \n ";
Node *head;
public:
List() { head = NULL; };
void Print();
void Add(int data);
void Append();
void find();
void Delete();
};
void List::Print()
{
Node *tmp = head;
while ( tmp != NULL )
{
cout << tmp->Data();
cout << " --> ";
tmp = tmp->Next();
}
cout << "NULL" << endl;
}
void List::Add(int data)
{
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(NULL);
Node *tmp = head;
if ( tmp != NULL )
{
while ( tmp->Next() != NULL )
{
tmp = tmp->Next();
}
tmp->SetNext(newNode);
}
else
{
head = newNode;
}
}
void List::Append()
{
Node* newNode = new Node();
Node *tmp = head;
Node *temp = head;
int n,data;
cout<<"Enter the node number : ";
cin>>n;
cout<<"Enter the data : ";
cin>>data;
newNode->SetData(data);
for( int i=1; i<n; i++)
{ tmp = tmp->Next(); }
temp = tmp->Next();
tmp->SetNext(newNode);
newNode->SetNext(temp);
}
void List::find()
{
int n, count = 0;
cout<<"Enter data to find in the list : ";
cin>>n;
Node *tmp = head;
while ( tmp != NULL )
{
count = count + 1;
if( n == tmp->Data())
{
cout<<"The data found on node number "<< count <<endl ;
break;
}
tmp = tmp->Next();
}
}
void List::Delete()
{
int n, count = 0;
cout<<"Enter data to delete : ";
cin>>n;
Node *tmp = head;
Node *temp = head;
while ( tmp != NULL )
{
count = count + 1;
if( n == tmp->Data())
{
break;
}
tmp = tmp->Next();
}
for( int j = 1; j < count-1; j++ )
{
temp = temp->Next();
}
temp->SetNext(tmp->Next());
delete tmp;
}
int main()
{
int n;
char c;
List mylist;
mylist.Add(10);
mylist.Add(20);
mylist.Add(30);
mylist.Add(40);
mylist.Add(50);
mylist.Add(60);
mylist.Add(70);
mylist.Add(80);
mylist.Add(90);
Choice:
cout<<"\n*********************** Please enter your choice ***********************\n\n ";
cout<<" A) To add node at the end of list \n ";
cout<<" B) To add node anywhere in the list \n ";
cout<<" C) To find data in the list \n ";
cout<<" D) To delete data from the list \n ";
cout<<" E) To display the list \n ";
cout<<" F) To exit \n ";
cin>>c;
switch(c)
{
case 'a':
case 'A':
cout<<"Enter data you want to enter in the last node : ";
cin>>n;
mylist.Add(n);
goto Choice;
break;
case 'b':
case 'B':
mylist.Append();
goto Choice;
break;
case 'c':
case 'C':
mylist.find();
goto Choice;
break;
case 'd':
case 'D':
mylist.Delete();
goto Choice;
break;
case 'e':
case 'E':
mylist.Print();
goto Choice;
break;
case 'f':
case 'F':
return 0;
break;
default:
cout<<"\n\nWrong Choice .";
goto Choice;
}
}
cout<<" E) To display the list \n ";
cout<<" F) To exit \n ";
cin>>c;
switch(c)
{
case 'a':
case 'A':
cout<<"Enter data you want to enter in the last node : ";
cin>>n;
mylist.Add(n);
goto Choice;
break;
case 'b':
case 'B':
mylist.Append();
goto Choice;
break;
case 'c':
case 'C':
mylist.find();
goto Choice;
break;
case 'd':
case 'D':
mylist.Delete();
goto Choice;
break;
case 'e':
case 'E':
mylist.Print();
goto Choice;
break;
case 'f':
case 'F':
return 0;
break;
default:
cout<<"\n\nWrong Choice .";
goto Choice;
}
}
Post A Comment:
0 comments: