Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please modify the code as follows. 1 . Add a function named findMin which finds the minimum value in the list. Thereafter, the main function

Please modify the code as follows.
1
.
Add a function named "findMin" which finds the minimum value in the list. Thereafter, the "main" function should print the value returned from the "findMin" function.
2
.
Add a function named "findMax" which finds the maximum value in the list. Thereafter, the "main" function should print the value returned from the "findMax" function.
3
.
Modify the main function to call "findMin" and "findMax".
#include
using namespace std;
struct Node
{
int data;
struct Node
*
next;
}
;
struct Node
*
head
=
NULL;
void printList
(
)
{
Node
*
curr
=
head;
while
(
curr
!
=
NULL
)
{
cout
<
<
curr
-
>
data
<
<
"
"
;
curr
=
curr
-
>
next;
}
cout
<
<
endl;
}
void insertFirst
(
int data
)
{
Node
*
newNode
=
new Node;
newNode
-
>
data
=
data;
newNode
-
>
next
=
NULL;
if
(
head
=
=
NULL
)
{
head
=
newNode;
}
else
{
newNode
-
>
next
=
head;
head
=
newNode;
}
}
void insertLast
(
int data
)
{
Node
*
newNode
=
new Node;
newNode
-
>
data
=
data;
newNode
-
>
next
=
NULL;
if
(
head
=
=
NULL
)
{
head
=
newNode;
}
else
{
Node
*
curr
=
head;
while
(
curr
-
>
next
!
=
NULL
)
curr
=
curr
-
>
next;
curr
-
>
next
=
newNode;
}
}
void insertAtPos
(
int data, int pos
)
{
Node
*
newNode
=
new Node;
newNode
-
>
data
=
data;
newNode
-
>
next
=
NULL;
int curPos
=
1
;
if
(
head
=
=
NULL
)
{
head
=
newNode;
}
else
{
Node
*
curr
=
head;
Node
*
prev;
while
(
curPos
!
=
pos && curr
-
>
next
!
=
NULL
)
{
prev
=
curr;
curr
=
curr
-
>
next;
curPos
+
+
;
}
prev
-
>
next
=
newNode;
newNode
-
>
next
=
curr;
}
}
void deleteAtPos
(
int pos
)
{
Node
*
curr
=
head;
int curPos
=
1
;
if
(
head
!
=
NULL
)
{
if
(
pos
=
=
1
)
{
head
=
curr
-
>
next;
delete curr;
}
else
{
Node
*
prev;
while
(
curPos
!
=
pos && curr
-
>
next
!
=
NULL
)
{
prev
=
curr;
curr
=
curr
-
>
next;
curPos
+
+
;
}
prev
-
>
next
=
curr
-
>
next;
delete curr;
}
}
}
int search
(
int value
)
{
int pos
=
1
;
Node
*
curr
=
head;
while
(
curr
!
=
NULL
)
{
if
(
curr
-
>
data
=
=
value
)
return pos;
pos
+
+
;
curr
=
curr
-
>
next;
}
return
-
1
;
}
int main
(
)
{
cout
<
<
"Hello World!
"
;
insertFirst
(
1
0
)
;
insertFirst
(
1
5
)
;
insertFirst
(
2
0
)
;
insertFirst
(
2
5
)
;
/
/
printList
(
)
;
insertLast
(
5
0
)
;
insertLast
(
1
3
)
;
insertAtPos
(
1
7
,
3
)
;
printList
(
)
;
deleteAtPos
(
3
)
;
printList
(
)
;
deleteAtPos
(
1
)
;
printList
(
)
;
/
/
cout
<
<
search
(
5
0
)
;
int pos
=
search
(
5
0
)
;
cout
<
<
pos;
return
0
;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals Design And Implementation

Authors: KROENKE DAVID M.

1st Edition

8120322258, 978-8120322257

Students also viewed these Databases questions

Question

What is the difference between Needs and GAP Analyses?

Answered: 1 week ago

Question

What are ERP suites? Are HCMSs part of ERPs?

Answered: 1 week ago