Question

In: Computer Science

C++ Using your completed LinkedList template class, developed in homework and lab, write a program that...

C++

Using your completed LinkedList template class, developed in homework and lab, write a program that implements and properly tests the following functions (these functions are not member functions of the class, you will write these function in mainTest.cpp )

1. Function compare that receives two LinkedList objects and compares the data in the nodes of the two lists to check if they are the same. The lists are equal only if they have the same number of nodes and corresponding nodes contain the same data. The function returns true or false to main.

2. Function mergeLists that receives two LinkedList objects. The function merges the sorted lists into a new list then returns the new list object back to main. Display the returned list.

Linkedlist.h link: https://pastebin.com/L4UUT3V8

Solutions

Expert Solution

Hello dude , Here I am giving your the full completed code , copy it as it is , it is running perfectly fine here so copy

paste carefully ,. And one more thing your code have many errors initially even some functions are implemeted wrong

I corrected them and also implemented main function and also written the code for compare_list and merge_two_sorted_list and one thing i want to clear that I made my own Linked List data as you have mentioned

nothing about it so you can change it depending on how you want to take input ; I just took one list odd numbers 1 to 10 and other even number between 1 to 10 to test these functions and they are woriking fine .

and one more thing whenever you call a function in main make new linkedList for it don't use previously used list;

Here is the Code copy paste it carefully and first run it, then you cna change whatever you want in input taking;

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
using namespace std;

template <class T>
class LinkedList
{
private:
// Declare a structure for the list
struct ListNode
{
T value;
struct ListNode *next;
};

ListNode *head; // List head pointer

public:
LinkedList() // Constructor
{ head = nullptr; }
~LinkedList(); // Destructor
void appendNode(T);
void insertNode(T);
void deleteNode(T);
void displayList();
int search(T); // search function
T getTotal();
int numNodes();
T getAverage();
T getLargest();
int getLargestPosition();
T getSmallest();
int getSmallestPosition();
T pop_back();

void push_front(T value);
void push_back(T value);
T pop_front();
};


//**************************************************
// appendNode appends a node containing the value *
// pased into newValue, to the end of the list. *
//**************************************************

template <class T>
void LinkedList<T>::appendNode(T newValue)
{
ListNode *newNode, *nodePtr = nullptr;

// Allocate a new node & store newValue
newNode = new ListNode;
newNode->value = newValue;
newNode->next = nullptr;

// If there are no nodes in the list
// make newNode the first node
if (!head)
head = newNode;
else // Otherwise, insert newNode at end
{
// Initialize nodePtr to head of list
nodePtr = head;

// Find the last node in the list
while (nodePtr->next)
nodePtr = nodePtr->next;

// Insert newNode as the last node
nodePtr->next = newNode;
}
}

//**************************************************
// displayList shows the value *
// stored in each node of the linked list *
// pointed to by head. *
//**************************************************

template <class T>
void LinkedList<T>::displayList()
{
ListNode *nodePtr = nullptr;

nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}

//**************************************************
// The insertNode function inserts a node with *
// newValue copied to its value member. *
//**************************************************

template <class T>
void LinkedList<T>::insertNode(T newValue)
{
ListNode *newNode, *nodePtr, *previousNode = nullptr;

// Allocate a new node & store newValue
newNode = new ListNode;
newNode->value = newValue;

// If there are no nodes in the list
// make newNode the first node
if (!head)
{
head = newNode;
newNode->next = nullptr;
}
else // Otherwise, insert newNode
{
// Initialize nodePtr to head of list and
// previousNode to a null pointer.
nodePtr = head;
previousNode = nullptr;

// Skip all nodes whose value member is less
// than newValue.
while (nodePtr != nullptr && nodePtr->value < newValue)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}

// If the new node is to be the 1st in the list,
// insert it before all other nodes.
if (previousNode == nullptr)
{
head = newNode;
newNode->next = nodePtr;
}
else // Otherwise, insert it after the prev. node.
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}

//*****************************************************
// The deleteNode function searches for a node *
// with searchValue as its value. The node, if found, *
// is deleted from the list and from memory. *
//*****************************************************

template <class T>
void LinkedList<T>::deleteNode(T searchValue)
{
ListNode *nodePtr, *previousNode = nullptr;

// If the list is empty, do nothing.
if (!head)
return;

// Determine if the first node is the one.
if (head->value == searchValue)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
// Initialize nodePtr to head of list
nodePtr = head;

// Skip all nodes whose value member is
// not equal to searchValue.
while (nodePtr != nullptr && nodePtr->value != searchValue)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}

// If nodePtr is not at the end of the list,
// link the previous node to the node after
// nodePtr, then delete nodePtr.
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}

//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************

template <class T>
LinkedList<T>::~LinkedList()
{
ListNode *nodePtr, *nextNode = nullptr;

nodePtr = head;
while (nodePtr != nullptr)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}

//*****************************************************
// search member function *
//This function performs a linear search of the list. *
//*****************************************************

template <class T>
int LinkedList<T>::search(T val)
{
int count = 1;
ListNode *nodePtr = nullptr;

nodePtr = head;
while (nodePtr)
{
if( nodePtr->value == val)
{
return count;
}
else
{
nodePtr = nodePtr->next;
count++;
}
}
return 0;
}

//*************************************************
// The getTotal function returns the total of *
// all the nodes in the list. *
//*************************************************
template <class T>
T LinkedList<T>::getTotal()
{
//write the function definition
T total = 0;
ListNode *nodePtr;   
nodePtr = head;
while (nodePtr != nullptr)
{
total = total + nodePtr->value;
nodePtr = nodePtr->next;
}
return total;
}

//************************************************
// The numNodes function returns the number of *
// nodes in the list. *
//************************************************
template <class T>
int LinkedList<T>::numNodes()
{
//write the function definition
int count = 0;
ListNode *nodePtr;
nodePtr = head;
while (nodePtr != nullptr)
{
count++;
nodePtr = nodePtr->next;
}
return count;
}
//*****************************************************
// The getAverage function returns the average *
// of the values in the list. *
//*****************************************************
template <class T>
T LinkedList<T>::getAverage()
{
//write the function definition
return getTotal()/numNodes();
}

//*************************************************
// The getLargest function returns the largest *
// value in the list. *
//*************************************************
template <class T>
T LinkedList<T>::getLargest()
{
//write the function definition
T largest;
ListNode *nodePtr = head;
if (nodePtr == nullptr)
{
return nullptr;
}
largest = head->value;
while (nodePtr != nullptr)
{
if (nodePtr->value > largest)
{
largest = nodePtr->value;
}
nodePtr = nodePtr->next;
}
return largest;

}

//*************************************************
// The getLargestPosition function returns the *
// position of the largest value in the list. *
//*************************************************
template <class T>
int LinkedList<T>::getLargestPosition()
{
//write the function definition
ListNode *nodePtr = head;
if (nodePtr = nullptr)
{
return -1;
}
int position = 0;
T largest = head->value;
int i = 0;
while (nodePtr != nullptr)
{
if (nodePtr->value > largest)
{
position = i;
largest = nodePtr->value;
}
i++;
nodePtr = nodePtr->next;
}
return largest;

return position;   
}

//*************************************************
// The getSmallest function returns the smallest *
// value in the list. *
//*************************************************
template <class T>
T LinkedList<T>::getSmallest()
{
//write the function definition
ListNode *nodePtr = head;
if (nodePtr == nullptr)
{
return nullptr;
}
T smallest = head->value;
while (nodePtr != nullptr)
{
if (nodePtr != nullptr)
{
smallest = nodePtr->value;
}
nodePtr = nodePtr->value;
}
return smallest;
  
}

//*************************************************
// The getSmallestPosition function returns the *
// position of the smallest value in the list. *
//*************************************************
template <class T>
int LinkedList<T>::getSmallestPosition()
{
//write the function definition
ListNode *nodePtr = head;
if(nodePtr == nullptr)
{
return -1;
}
int position = 0;
T smallest = head->value;
int i = 0;
while (nodePtr != nullptr)
{
if (nodePtr->value < smallest)
{
position = i;
smallest = nodePtr->value;
}
i++;
nodePtr = nodePtr->next;
}
return smallest;

return position;
}

//*************************************************

//Removes the last element and returns its value *

//*************************************************

template <class T>

T LinkedList<T>::pop_back()

{

ListNode *nodePtr = head;

if(nodePtr == nullptr)

return nullptr;

while(nodePtr->next)

{

nodePtr = nodePtr->next;

}

ListNode *lastPtr = nodePtr;

delete nodePtr;

return lastPtr->value;

}

//*************************************************

//Removes the first element and returns its value *

//*************************************************

template <class T>

T LinkedList<T>::pop_front()

{

ListNode *nodePtr = head;

if(nodePtr == nullptr)

return -1;

head=head->next;

ListNode *firstPtr = nodePtr;

int front= firstPtr->value;

delete firstPtr;
  
return front;

}

//*************************************************

//Inserts an element at the front of the list *

//*************************************************

template <class T>

void LinkedList<T>::push_front(T value)

{

ListNode *newNode, *nodePtr = head;

newNode = new ListNode;

newNode->value = value;

if (!nodePtr)

{

head = newNode;

newNode->next = nullptr;

}

else

{

newNode->next=head;

head=newNode;

}

}

//*************************************************

//Inserts an element at the back of the list *

//*************************************************

template <class T>

void LinkedList<T>::push_back(T value)

{

ListNode *newNode, *nodePtr = head;

newNode = new ListNode;

newNode->value = value;

newNode->next = nullptr;

if (!nodePtr)

{

head = newNode;

}

else

{

while(nodePtr->next)

{

nodePtr=nodePtr->next;

}

nodePtr->next=newNode;

}

}

#endif

void make_list(LinkedList<int> *l1, LinkedList<int> *l2)
{

for(int i =0; i<=10; i++)
{
if(i%2)
l1->push_back(i);
else l2->push_back(i);
}

cout<<"List 1 :"<<endl;
l1->displayList();
cout<<endl;
cout<<"List 2 :"<<endl;
l2->displayList();
cout<<endl;

return ;
}

bool compare_List(LinkedList<int> *l1, LinkedList<int> *l2)
{
if(l1->numNodes() != l2->numNodes())
{
return false;
}
  
int n= l1->numNodes();
while(n--)
{

if(l1->pop_front()!=l2->pop_front())
{
return false;
}
}
  
  
return true;
}

LinkedList<int> * merge_two_sorted_list(LinkedList<int> *l1, LinkedList<int> *l2)
{

LinkedList<int> *final_list= new LinkedList<int>();
  
  
  
while(l1->numNodes()>0&&l2->numNodes()>0)
{
// cout<<l1->numNodes()<<" "<<l2->numNodes()<<endl;

int a = l1->pop_front();
int b = l2->pop_front();
  
//cout<<a<<" "<<b<<endl;
if(a<b)
{
final_list->push_back(a);

l2->push_front(b);
}
else
{
final_list->push_back(b);
l1->push_front(a);
}
}

while(l1->numNodes()>0)
{
final_list->push_back(l1->pop_front());
}


while(l2->numNodes()>0)
{
final_list->push_back(l2->pop_front());
}

return final_list;
}

int main()
{

LinkedList<int> *l1=new LinkedList<int>();
LinkedList<int> *l2= new LinkedList<int>();

make_list(l1,l2);
  
cout<<"comparing list 1 with List 2"<<endl;

if(compare_List(l1,l2))
{
cout<<"True"<<endl;
}
else
cout<<"false"<<endl;

LinkedList<int> *ll1=new LinkedList<int>();
LinkedList<int> *ll2= new LinkedList<int>();

make_list(ll1,ll2);

cout<<"merging list 1 and list 2 :"<<endl;


LinkedList<int> *final_list= merge_two_sorted_list(ll1,ll2);

final_list->displayList();

return 0;

}


Related Solutions

USING a LOOP for C++ In this lab the completed program should print the numbers 0...
USING a LOOP for C++ In this lab the completed program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. You should accomplish this using a for loop instead of a counter-controlled while loop. Instructions Write a for loop that uses the loop control variable to take on the values 0 through 10. In the body of the loop, multiply the value of the loop control variable by 2 and by 10....
In C++ Please, using only the libraries given in this homework prompt, Write a program that...
In C++ Please, using only the libraries given in this homework prompt, Write a program that (1) prompts for two integers, (2) prints out their sum, (3) prints out the first divided by the second, and (4) prints out the natural log of the first number raised to the power of the second number. #include <iostream> #include <iomanip> using namespace std; int main() { <your answer goes here> return 0; }
To be Completed in C# Write a program that creates an exception class called ManyCharactersException, designed...
To be Completed in C# Write a program that creates an exception class called ManyCharactersException, designed to be thrown when a string is discovered that has too many characters in it. Consider a program that takes in the last names of users. The driver class of the program reads the names (strings) from the user until the user enters "DONE". If a name is entered that has more than 20 characters, throw the exception. Design the program such that it...
c++ class homework Topics If/Else If statement Description Write a program that determines a student’s final...
c++ class homework Topics If/Else If statement Description Write a program that determines a student’s final grade in the course. The course had three tests (100 points each) and four assignments (also 100 points each). All the test scores make up 70% of the grade and all the assignments 30% of the grade. The program asks the user to input one by one each of the test scores and each of the assignment scores. From these scores, it computes the...
In C++ Complete the template program. ADD to your c++ program as a comment the PARTIAL...
In C++ Complete the template program. ADD to your c++ program as a comment the PARTIAL output from executing your program - Only copy the last 6 lines of output. There is no input data for this problem. // Find Pythagorean triples using brute force computing. #include <iostream> using std::cout; using std::endl; int main() { int count = 0; // number of triples found long int hypotenuseSquared; // hypotenuse squared long int sidesSquared; // sum of squares of sides cout...
C++ Please write a exmaple of class template RedBlackTree.h.(It must be template!). I do not mind...
C++ Please write a exmaple of class template RedBlackTree.h.(It must be template!). I do not mind about details but please include the basic functions that are necessary for a RedBlackTree.
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print all elements of a linked list in order. B. Method to print all elements of a linked list in reverse order. C. Method to print all elements of a linked list in order starting from specific position. D. Method to join two linked lists into the first list in the parameters. E. Method to clone a linked list. The copy list has to be...
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template....
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template. And rewrite your main function to test your template for integer array and double array. Following is my complete code: #include <iostream> using namespace std; class Array { private: // Pointer to memory block to store integers int* data; // Maximum size of memory block int cap; // Stores number of integers in an array int num; public: // Constructor Array(int size); // Default...
Write a Java program/method that takes a LinkedList and returns a new LinkedList with the integer...
Write a Java program/method that takes a LinkedList and returns a new LinkedList with the integer values squared and reversed. Example: if the LinkedList has (9, 5,4,6), then the returned list will have (36, 16,25,81). What is the time-complexity of your code? You must use listIterator for full credit. public LinkedList getReverseSquaredList (LinkedList list) { }
1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class....
1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class. Create a method to find a node with given value in a LinkedList. Return the value is this value exists in the LinkedList. Return null if not exists. Use these two examples to test your method. Example 1: Input: 1 -> 2 -> 3, and target value = 3 Output: 3 Example 2: Input: 1 -> 2 -> 3, and target value = 4...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT