Question: Hello, I try to add recursion function to find the min in this program it gives me error i dont know why if you can
Hello,
I try to add recursion function to find the min in this program it gives me error i dont know why if you can help me please? i know the Find_min funcrion it is
int Find_min(Node *head)
{ if (head == Null) return INT_Max;
else{ return min(head->data, Find_min(head->next));}}
but when i add in my LintegerLinkedList it giving me error the complete code is :
/IntegerLinkedList.h
#pragma once
#include
#include
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<
return str;
}
---------------------------------------------------------------------------
//main.cpp
//
// DO NOT EDIT THIS FILE
// WRITE YOUR CODE IN IntegerLinkedList.h and IntegerLinkedList.cpp
//
#include
#include
#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;
}
Thank you for your help.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
