User enters the integers. The input 9 is the end, and unless it is the first int
ID: 3885601 • Letter: U
Question
User enters the integers. The input 9 is the end, and unless it is the first integer, it is not to be considered the part of the input. Create a linked list from the numbers that the user has entered and print it. Then after that the user is asked to enter another integer. If this integer is in the list, delete its first occurrence from the list and print the remaining list. If it is not in the list, just print the original list. USING ONLY C++ , NOT C ,NOR C# .
THIS IS THE BEGINNING BUT CAN'T DO THE REST.
#include <iostream>
class ListNode{
public:
int content;
ListNode *pointerToNext;
};
void freeTheMemory(ListNode *runner){
if( runner!=nullptr){
freeTheMemory((*runner).pointerToNext);
delete runner;
}
}
int main(){
int userInput;
ListNode *head;
std::cout<<"Insert the first element of the list: ";
std::cin>>userInput;
head=new ListNode;
(*head).content=userInput;
(*head).pointerToNext=nullptr;
ListNode *runner;
runner=head;
while(userInput!=-9){
std::cout<<"Insert an element of the list (-9 for the end): ";
std::cin>>userInput;
if(userInput!=-9){
// *runner is the last node in the list (tail)
// (*runner).pointerToNext is currently nullptr
// We now allocate new memory for ListNode and make
// (*runner).pointerToNext to contain the address
// of this new ListNode
(*runner).pointerToNext=new ListNode;
// runner is no more pointing to the tail
// The next line updates the runner to point
// to the newly created tail
runner=(*runner).pointerToNext;
// We now set the content of the tail
(*runner).content=userInput;
// and make sure that the tail‘s pointer
// to the the next is set to nullptr
(*runner).pointerToNext=nullptr;
}
}
std::cout<<"List printout: "<<std::endl;
runner=head;
while(runner!=nullptr){
std::cout<<(*runner).content<<" ";
runner=(*runner).pointerToNext;
}
std::cout<<std::endl;
// FREEING THE MEMORY
freeTheMemory(head);
return 0;
}
Explanation / Answer
//Please see the updated code below
#include <iostream>
class ListNode{
public:
int content;
ListNode *pointerToNext;
};
void freeTheMemory(ListNode *runner){
if( runner!=NULL){
freeTheMemory((*runner).pointerToNext);
delete runner;
}
}
// If this integer is in the list, delete its first occurrence from the list
void deleteFirstOccList(ListNode **head_ref, int key)
{
ListNode* temp = *head_ref, *prev;
// If head node itself holds the key to be deleted
if (temp != NULL && temp->content == key)
{
*head_ref = temp->pointerToNext;
free(temp);
return;
}
// Search for the key to be deleted, keep track of the
// previous node as we need to change 'prev->next'
while (temp != NULL && temp->content != key)
{
prev = temp;
temp = temp->pointerToNext;
}
// If key was not present in linked list
if (temp == NULL) return;
// Unlink the node from linked list
prev->pointerToNext = temp->pointerToNext;
free(temp); // Free memory
}
int main(){
int userInput;
ListNode *head;
std::cout<<"Insert the first element of the list: ";
std::cin>>userInput;
head=new ListNode;
(*head).content=userInput;
(*head).pointerToNext=NULL;
ListNode *runner;
runner=head;
while(userInput!=-9){
std::cout<<"Insert an element of the list (-9 for the end): ";
std::cin>>userInput;
if(userInput!=-9){
// *runner is the last node in the list (tail)
// (*runner).pointerToNext is currently nullptr
// We now allocate new memory for ListNode and make
// (*runner).pointerToNext to contain the address
// of this new ListNode
(*runner).pointerToNext=new ListNode;
// runner is no more pointing to the tail
// The next line updates the runner to point
// to the newly created tail
runner=(*runner).pointerToNext;
// We now set the content of the tail
(*runner).content=userInput;
// and make sure that the tail‘s pointer
// to the the next is set to nullptr
(*runner).pointerToNext=NULL;
}
}
std::cout<<"List printout: "<<std::endl;
runner=head;
while(runner!=NULL){
std::cout<<(*runner).content<<" ";
runner=(*runner).pointerToNext;
}
std::cout<<std::endl;
int anotherInteger;
std::cout<<"Please enter another integer";
std::cout<<std::endl;
std::cin>>anotherInteger;
deleteFirstOccList(&head,anotherInteger);
std::cout<<"List printout: "<<std::endl;
runner=head;
while(runner!=NULL){
std::cout<<(*runner).content<<" ";
runner=(*runner).pointerToNext;
}
std::cout<<std::endl;
// FREEING THE MEMORY
freeTheMemory(head);
return 0;
}
OUTPUT:
Insert the first element of the list: 2
Insert an element of the list (-9 for the end): 3
Insert an element of the list (-9 for the end): 4
Insert an element of the list (-9 for the end): 5
Insert an element of the list (-9 for the end): 7
Insert an element of the list (-9 for the end): -9
List printout:
2 3 4 5 7
Please enter another integer
3
List printout:
2 4 5 7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.