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

Please Use C++ to finish as the requirements. Implement a class called SinglyLinkedList. In the main...
Please Use C++ to finish as the requirements. Implement a class called SinglyLinkedList. In the main function, instantiate the SinglyLinkedList class. Your program should provide a user loop and a menu so that the user can access all the operators provided by the SinglyLinkedList class. DestroyList InitializeList GetFirst InsertFirst, InsertLast, Insert DeleteFirst, DeleteLast, Delete IsEmpty Length Print, ReversePrint
what is the output? int main ( ) { int a = 3, b= 2, c=...
what is the output? int main ( ) { int a = 3, b= 2, c= 1, d, e, f, g; d = a&b;    e = a | c; f = a >> 1, g = a << 1; cout << “d= “ << d << “ e = “ << e << “ f = “ << f << “g = “ << g << endl; }
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.
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”);} 4. Translate the following C code to MIPS assembly (in two separate files). Run the program step by step and observe the order of instructions being executed and the value of $sp. int main() { int x=2; z=Subfunc(x); printf(“Value of z is: %d”, z); } int Subfunc(int x) { return x+1;}
Complete the following functions in the Stack.java/Stack.h files (ATTACHED BELOW): a. void push(int val) b. int...
Complete the following functions in the Stack.java/Stack.h files (ATTACHED BELOW): a. void push(int val) b. int pop() c. int getSize() public class Stack {    private int maxStackSize, topOfStack; private int[] stack;    public Stack(int maxStackSize) { if (maxStackSize <= 0) System.out.println("Stack size should be a positive integer."); else { this.maxStackSize = maxStackSize; topOfStack = -1; stack = new int[maxStackSize]; } }    public void push(int val) { // complete this function }    public int pop() { // complete...
C++ Write a program with the following elements: in main() -opens the 2 files provided for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...
This is a JAVA assignment and i dont have the SinglyLinkedList class   Exercise 1 In this...
This is a JAVA assignment and i dont have the SinglyLinkedList class   Exercise 1 In this exercise, you will add a method swapNodes to SinglyLinkedList class. This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. Hint: You may need to traverse the list. Exercise 2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT