Hello, Could you please complete this code for me? Thank you Chegg customer // /
ID: 3917162 • Letter: H
Question
Hello,
Could you please complete this code for me?
Thank you
Chegg customer
//
// DO NOT EDIT THIS FILE
// WRITE YOUR CODE IN IntegerLinkedList.h and IntegerLinkedList.cpp
//
#include <iostream>
#include <string>
#include "IntegerLinkedList.h"
using namespace std;
int main() {
IntegerLinkedList mylist;
cout << "Enter number of integers : ";
int n, value;
cin >> n;
cout << "Enter " << n << " integers" << endl;
for (int i = 0; i < n; i++) {
cin >> value;
mylist.addFront(value);
}
cout << "print: " << endl;
mylist.print();
cout << endl;
mylist.deleteSmallest();
cout << "print after deleteSmallest: " << mylist.print() << endl;
}
Explanation / Answer
//there is some problem in code in main
//print function is called without cout in main at one place ,, according to that it should be void function //in member function, but in other place it is called with cout which requires values returned from print //function to use it for cout to print ,, So I just designed it as return string ,, this function will add //numbers to string and print so that I don't end up changing main as it is mentioned in the beginning //that main should not be changed.
//IntegerLinkedList.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
struct node
{
int value;
node *next;
};
class IntegerLinkedList
{
node *head;
public:
IntegerLinkedList();
void addFront(int val);
void deleteSmallest();
string print();
};
------------------------------------------------
//IntegerLinkedList.cpp
#include"IntegerLinkedList.h"
IntegerLinkedList::IntegerLinkedList()
{
head = NULL;
}
void IntegerLinkedList::addFront(int val)
{
node *newNode,*next;
if (head == NULL)
{
head = new node;
head->value = val;
head->next = NULL;
}
else
{
newNode = new node;
newNode->value = val;
newNode->next = NULL;
//save next pointer
next = head;
head = newNode;
head->next = next;
}
}
void IntegerLinkedList::deleteSmallest()
{
node *cur = head, *prev = cur,*next=NULL;
int min = cur->value;
//first find smallest number in list and then delete
while (cur != NULL)
{
if (min > cur->value)
{
min = cur->value;
}
cur = cur->next;
}
//now delete the min by searching the number
//if smallest is at head
if (head !=NULL && head->value == min)
{
prev = head;
head = head->next;
delete prev;
}
else
{
cur = head;
/*if (cur != NULL)
{
while (cur->next != NULL && cur->next->value != min)
{
cur = cur->next;
prev = cur;
}
cur->next = NULL;
delete prev->next;
}*/
while (cur != NULL && cur->value != min)
{
prev = cur;
cur = cur->next;
next = cur->next;
}
prev->next = next;
delete cur;
}
}
string IntegerLinkedList::print()
{
node *cur = head;
string str;
while (cur != NULL)
{
str += to_string(cur->value);
str += ' ';
cur = cur->next;
}
cout << str<<endl;
return str;
}
---------------------------------------------------------------------------
//main.cpp
//
// DO NOT EDIT THIS FILE
// WRITE YOUR CODE IN IntegerLinkedList.h and IntegerLinkedList.cpp
//
#include <iostream>
#include <string>
#include "IntegerLinkedList.h"
using namespace std;
int main() {
IntegerLinkedList mylist;
cout << "Enter number of integers : ";
int n, value;
cin >> n;
cout << "Enter " << n << " integers" << endl;
for (int i = 0; i < n; i++) {
cin >> value;
mylist.addFront(value);
}
cout << "print: " << endl;
mylist.print();
cout << endl;
mylist.deleteSmallest();
cout << "print after deleteSmallest: " << mylist.print() << endl;
}
/*output1:
Enter number of integers : 5
Enter 5 integers
10
5
4
7
1
print:
1 7 4 5 10
7 4 5 10
print after deleteSmallest: 7 4 5 10
output2:
Enter number of integers : 10
Enter 10 integers
1
6
19
10
2
34
11
15
11
19
print:
19 11 15 11 34 2 10 19 6 1
19 11 15 11 34 2 10 19 6
print after deleteSmallest: 19 11 15 11 34 2 10 19 6
output3:
Enter number of integers : 5
Enter 5 integers
3
10
2
18
10
print:
10 18 2 10 3
10 18 10 3
print after deleteSmallest: 10 18 10 3
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.