Can someone help me with C++ Coding Exercise2? thanks! below is the google doc l
ID: 3575595 • Letter: C
Question
Can someone help me with C++ Coding Exercise2? thanks! below is the google doc link with the information!
: https://docs.google.com/document/d/1xqg6I7F_CdXkS3tezYq-IIFQLYeNBj9sxGtcYHC5wmw/edit?usp=sharing
Below is the code I wrote so far:
https://docs.google.com/document/d/1kv9jjxFw_JeRikzztCs0Q71BOhQ3sfbwJ3kG7kknMtU/edit?usp=sharing
(Exercise 2) In this part of the lab, you will finish implementing your Linked List by implementing two more functions. You shou a new main file (Exercise2.cpp), which you can find on Cataes. Again, DO NOT modify the class declaration (LinkedList.h) or main file (Exercise2.cpp) The two new functions are as follows (use your LinkedList.cpp file from Exercise 1, and DO NOT create a new LinkedList.cpp file): . insertAtFront(int): inserts a new element (i.e., a new Node) at the beginning of the list. . removeFromFront(): removes the first element of the list. The function should return true if an element was successfully removed and false otherwise Example The first list is empty! The second list is empty! The size of the first list is: 0 The size of the second list is: O Here is the first list: [5,4,3,2,1] Here is the second list: [] Here is the first list: [5,4,3,2,1] Here is the second list: [25] Here is the first list: [4,3,2,1] Here is the second list: [] Here is the first list: [] Here is the second list: [10,5,0,-5] The size of the first list is: 0 The size of the second list is: 4 The first list is empty! The second list is NOT empty Here is the second list: [10,5,0,-5] Successfully removed an item from the list Here is the second list: [5,0,-5] Successfully removed an item from the list Here is the second list: [0,-5] Successfully removed an item from the list... Here is the second list: [-5] Successfully removed an item from the list... Here is the second list: [] COULD NOT remove an item from the list! Here is the second list: [I COULD NOT remove an item from the list! The size of the first list is: 0 The size of the second list is: 0 The first list is empty! The second list is empty!Explanation / Answer
Changed LinkedList.cpp
There was logical error when finding size of linked list using size method, I changed it as below
//changed by chegg as count+1 is not correct ,need to return count
return count;
Added functionality for 2 methods
//implemetation added by chegg EA
void LinkedList::insertAtFront(int valueToInsert)
{
Node *temp ;
//create new node and insert value
temp = new Node;
temp->val = valueToInsert;
temp->next = first;
first = temp;
}
//implemetation added by chegg EA
bool LinkedList::removeFromFront()
{
if (first != NULL)
{
Node *temp = first->next;
free(first);
first = temp;
return true;
}
else
return false;
}
------------------------------------------------------------------
output:
The first list is empty!
The second list is empty!
The size of the first list is: 0
The size of the second list is: 0
Here is the first list: [5 4 3 2 1 ]
Here is the second list: []
Here is the first list: [5 4 3 2 1 ]
Here is the second list: [25 ]
Here is the first list: [4 3 2 1 ]
Here is the second list: []
Here is the first list: []
Here is the second list: [10 5 0 -5 ]
The size of the first list is: 0
The size of the second list is: 4
The first list is empty!
The second list is NOT empty...
Here is the second list: [10 5 0 -5 ]
Successfully removed an item from the list...
Here is the second list: [5 0 -5 ]
Successfully removed an item from the list...
Here is the second list: [0 -5 ]
Successfully removed an item from the list...
Here is the second list: [-5 ]
Successfully removed an item from the list...
Here is the second list: []
COULD NOT remove an item from the list!
Here is the second list: []
COULD NOT remove an item from the list!
The size of the first list is: 0
The size of the second list is: 0
The first list is empty!
The second list is empty!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.