In: Computer Science
How to combine these 2 main functions of c++ files in 1 main class?
//SinglyLinkedList
int main() {
SinglyLinkedList<std::string> list;
list.Add("Hello");
list.Print();
list.Add("Hi");
list.Print();
list.InsertAt("Bye",1);
list.Print();
list.Add("Akash");
list.Print();
if(list.isEmpty()){
cout<<"List is Empty "<<endl;
}
else{
cout<<"List is not empty"<<endl;
}
cout<<"Size = "<<list.Size()<<endl;
cout<<"Element at position 1 is
"<<list.get(1)<<endl;
if(list.Contains("X")){
cout<<"List contains X"<<endl;
}
else{
cout<<"List does not contain X"<<endl;
}
cout<<"Position of the word Akash is
"<<list.IndexOf("Akash")<<endl;
cout<<"Last Position of the word Akash is
"<<list.LastOf("Akash")<<endl;
list.RemoveElement("Akash");
cout<<"After removing Akash from the list
"<<endl;
list.Print();
return 0;
}
//DoublyLinkedList
int main() {
DoublyLinkedList<std::string> list;
list.Add("Hello");
list.Print();
list.Add("Hi");
list.Print();
list.InsertAt("Bye",1);
list.Print();
list.Add("Akash");
list.Print();
if(list.isEmpty()){
cout<<"List is Empty "<<endl;
}
else{
cout<<"List is not empty"<<endl;
}
cout<<"Size = "<<list.Size()<<endl;
cout<<"Element at position 1 is
"<<list.get(1)<<endl;
if(list.Contains("X")){
cout<<"List contains X"<<endl;
}
else{
cout<<"List does not contain X"<<endl;
}
cout<<"Position of the word Akash is
"<<list.IndexOf("Akash")<<endl;
cout<<"Last Position of the word Akash is
"<<list.LastOf("Akash")<<endl;
list.RemoveElement("Akash");
cout<<"After removing Akash from the list
"<<endl;
list.Print();
return 0;
}
/*
//changed name so that it will be eassy to understand if the variable of SinglyLinkedList or DoublyLinkedList
also due to can't have two variable of same name
Changed the message which will print to not have doubt regarding operation of different list
*/
int main() {
SinglyLinkedList<std::string> s_list;
s_list.Add("Hello");
s_list.Print();
s_list.Add("Hi");
s_list.Print();
s_list.InsertAt("Bye",1);
s_list.Print();
s_list.Add("Akash");
s_list.Print();
if(s_list.isEmpty()){
cout<<"Singly list is Empty "<<endl;
}
else{
cout<<"Singly list is not empty"<<endl;
}
cout<<"Size of Singly list = "<<s_list.Size()<<endl;
cout<<"In Singly list Element at position 1 is "<<s_list.get(1)<<endl;
if(s_list.Contains("X")){
cout<<"Singly list contains X"<<endl;
}
else{
cout<<"Singly list does not contain X"<<endl;
}
cout<<"In Singly list Position of the word Akash is "<<s_list.IndexOf("Akash")<<endl;
cout<<"In Singly list Last Position of the word Akash is "<<s_list.LastOf("Akash")<<endl;
s_list.RemoveElement("Akash");
cout<<"In Singly list After removing Akash from the list "<<endl;
list.Print();
DoublyLinkedList<std::string> d_list; //changes
d_list.Add("Hello");
d_list.Print();
d_list.Add("Hi");
d_list.Print();
d_list.InsertAt("Bye",1);
d_list.Print();
d_list.Add("Akash");
d_list.Print();
if(d_list.isEmpty()){
cout<<"Doubly List is Empty "<<endl;
}
else{
cout<<"Doubly List is not empty"<<endl;
}
cout<<"Size of Doubly List= "<<list.Size()<<endl;
cout<<"In Doubly List Element at position 1 is "<<list.get(1)<<endl;
if(d_list.Contains("X")){
cout<<"Doubly List contains X"<<endl;
}
else{
cout<<"Doubly List does not contain X"<<endl;
}
cout<<"In Doubly List Position of the word Akash is "<<list.IndexOf("Akash")<<endl;
cout<<"In Doubly List Last Position of the word Akash is "<<list.LastOf("Akash")<<endl;
d_list.RemoveElement("Akash");
cout<<"In Doubly List After removing Akash from the list "<<endl;
d_list.Print();
return 0;
}
***Doubt? Ask in comment box***