Question

In: Computer Science

How to combine these 2 main functions of c++ files in 1 main class? //SinglyLinkedList int...

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;
}

Solutions

Expert Solution

/*
//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***


Related Solutions

Given main(), complete the Car class (in files Car.h and Car.cpp) with member functions to set...
Given main(), complete the Car class (in files Car.h and Car.cpp) with member functions to set and get the purchase price of a car (SetPurchasePrice(), GetPurchasePrice()), and to output the car's information (PrintInfo()). Ex: If the input is: 2011 18000 2018 where 2011 is the car's model year, 18000 is the purchase price, and 2018 is the current year, the output is: Car's information: Model year: 2011 Purchase price: 18000 Current value: 5770 Note: printInfo() should use three spaces for...
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} Submission file: Lab4_3a.asm for the main routine and Lab4_3b.asm for the sub-routine.
Change the following C++ functions to Assembly x86: int problem1_ ( ) { int numberArray [3]...
Change the following C++ functions to Assembly x86: int problem1_ ( ) { int numberArray [3] = {1, -2, 15 }; int result = 0, index = 0; int numElements = 3; while (index < numElements) { if ( index >= 1 && numberArray[index] > 3 ) { result += numberArray[index] ; } else { result -= 3; } index++; } return result; } int problem2_ ( ) { int a, modulo int answer = 0, b = 3; for...
Change the following C++ functions to Assembly x86: int problem1_ ( ) { int numberArray [3]...
Change the following C++ functions to Assembly x86: int problem1_ ( ) { int numberArray [3] = {1, -2, 15 }; int result = 0, index = 0; int numElements = 3; while (index < numElements) { if ( index >= 1 && numberArray[index] > 3 ) { result += numberArray[index] ; } else { result -= 3; } index++; } return result; } int problem2_ ( ) { int a, modulo int answer = 0, b = 3; for...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
C++ Need to add the following functions to my templatized class linked list. (main is already...
C++ Need to add the following functions to my templatized class linked list. (main is already set to accommodate the functions below) --void destroy_list () deletes each node in the list, and resets the header to nullptr --bool search_list (key value) searches the list for a node with the given key. Returns true if found, false if not. --bool delete_node (key value) deletes the node which contains the given key. If there is more than one node with the same...
IN C++, ONE FILE Also include the main class that just runs these functions, thanks. Write...
IN C++, ONE FILE Also include the main class that just runs these functions, thanks. Write a recursive function to produce a pattern of n lines of asterisks. The first line contains one asterisk, the next line contains two, and so on, up to the nth line, which contains n asterisks. For example, if the non-negative integer is 5, the pattern generated is: * ** *** **** ***** Prototype: void pattern(unsigned n); Write a recursive function, sum_range that finds the...
In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main()...
In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main() method Build the ItemToBuy class with the following specifications: Private fields String itemName - Initialized in the nor-arg constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 No-arg Constructor (set the String instance field to "none") Public member methods (mutators & accessors) setName() & getName() setPrice() & getPrice() setQuantity() & getQuantity() toString()...
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha...
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha so the main function is working properly. #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// class alpha { private: int data; public: //YOUR CODE }; //////////////////////////////////////////////////////////////// int main() { alpha a1(37); alpha a2; a2 = a1; cout << "\na2="; a2.display(); //display a2 alpha a3(a1); //invoke copy constructor cout << "\na3="; a3.display(); //display a3 alpha a4 = a1; cout << "\na4="; a4.display(); cout << endl; return 0;...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT