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

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...
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1,...
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1, 2, 3, 4}; get_value(data, 2) = 100; } Write ????? that returns the value in the array at index: Question Blank type your answer...
C++ How to make this code take a class object instead of int for the vector...
C++ How to make this code take a class object instead of int for the vector queue and be able to enqueue and dequeue the object? #include <iostream> #include <float.h> #include <bits/stdc++.h> using namespace std; void print_queue(queue<int> Q) //This function is used to print queue { while (!Q.empty()) { cout<< Q.front() << " "; Q.pop(); } } int main() { int n = 10; vector< queue<int> > array_queues(n); //Create vector of queues of size 10, each entry has a queue...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT