#include
using namespace std;
class List
{
public:
int item;
int pos=0;
List* nextNode;
};
void Print_Reverse_List(List* node)
{
if (node == NULL)
return;
Print_Reverse_List(node->nextNode);
cout << node->item << " ";
}
void Insert_Element(List** head, char data)
{
List* node = new List();
node->item = data;
node->nextNode = (*head);
(*head) = node;
}
void PrinList(List *node){
List *temp = node;
while(temp !=NULL){
cout<item<<" ";
node->pos ++;
temp = temp->nextNode;
}
cout<<"\n";
}
void search_Element(List *node, int x){
List *temp = node;
while(temp !=NULL){
int data = temp->item;
if (data==x){
cout<<"\nElement found\n";
}
temp = temp->nextNode;
}
cout<<"\n";
}
int Length(List *node){
return node->pos;
}
YOUR TASK:
Create default, parameterized and copy constructor for the above code and create a manu to test the functions inside the main function.
#include <iostream>
using namespace std;
class List{
public:
int item;
int pos;
List* nextNode;
List(){
this->pos=0;
}
List(int pos){
this->pos=pos;
}
List(int item,int pos){
this->item=item;
this->pos=pos;
}
List(List &list){
this->item=list.item;
this->pos=list.pos;
this->nextNode=list.nextNode;
}
};
void Print_Reverse_List(List* node){
if (node == NULL)
return;
Print_Reverse_List(node->nextNode);
cout << node->item << " ";
}
void Insert_Element(List** head, int data){
List* node = new List();
node->item = data;
node->nextNode=(*head);
(*head) = node;
}
void PrinList(List* node){
List *temp = node;
while(temp !=NULL){
cout<<temp->item<<" ";
node->pos++;
temp = temp->nextNode;
}
cout<<"\n";
}
void search_Element(List *node, int x){
List *temp = node;
while(temp !=NULL){
int data = temp->item;
if (data==x){
cout<<"\nElement found\n";
}
temp = temp->nextNode;
}
cout<<"\n";
}
int Length(List *node){
return node->pos;
}
int main(){
List* linkList=NULL;
int ch=-1;
int number;
int position;
while(ch!=6){
cout<<"1. Insert Element\n";
cout<<"2. Print List\n";
cout<<"3. Search Element\n";
cout<<"4. Display Length of List\n";
cout<<"5. Print Reverse List\n";
cout<<"6. Exit\n";
cout<<"Your choice: ";
cin>>ch;
cout<<"\n";
switch(ch){
case 1:
cout<<"Enter the number to be inserted: ";
cin>>number;
Insert_Element(&linkList,number);
break;
case 2:
PrinList(linkList);
break;
case 3:
{
cout<<"Enter the number to be searched: ";
cin>>number;
search_Element(linkList,number);
}
break;
case 4:
cout<<"Length of List is: "<<Length(linkList);
break;
case 5:
Print_Reverse_List(linkList);
break;
case 6:
//exit
delete linkList;
break;
default:
break;
}
cout<<"\n\n";
}
return 0;
}
Comments
Leave a comment