Reputation: 325
The following code will not print the last line which displays the maximum element in the linked list below.
In the following code, I sum the linked list, print the entire linked list, print the length of the linked list, and would like to print the maximum value found in the linked list.
For some reason, the compiler does not print the last line.
#include <iostream>
#include <stdio.h>
using namespace std;
struct Node{ //if you want to convert to class, you need to assign access specifiers to public since they're private on default
int data;
struct Node *next;
}*first = NULL;
void create(int A[], int n)
{
struct Node *t; //create a temporary pointer t
struct Node *last; //pointer, points to the last node - helps me add a new node at the END of a linked list
//as of now, the linked list is empty, so we must create the first node!
first = new Node;//create new node on the heap, and first will be pointing on that new node
first -> data = A[0]; // Assign first the first element on the array
first -> next = NULL;//Should point to a null value as it is the only element on the list to start/currently
last = first; //last points on first node
for (int i = 1; i <n; i++)// i starts at element 1 since first has been assigned the 1st element in the array
{
t = new Node; //create a new node
t->data = A[i]; //fill up the data of t from A[i] which will be iterated through and assigned to data
t->next = NULL; // the next node should be pointing to NULL, as there is nothing at the moment when the iteration begins that it is initially pointing to
last -> next = t;
last = t;
}
}
int length (struct Node *p)
{
int l = 0;
while (p)
{
l++;
p = p->next;
}
return l;
}
int sum (struct Node *p){
int s= 0;
while(p){
s+=p->data;
p = p->next;
}
return s;
}
int max (struct Node *p){
int max = -32767;
while(p){
if(p->data > max){
max = p->data;
p = p->next;
}
}
return max;
}
void display (struct Node *p)
{
while (p != 0 )
{
cout<<p->data<<" ";
cout<<p->next<<" ";
p = p->next;
}
}
int main() {
int A [] = {1,2,3,18,5, 6, 7};
create (A,7);
display(first);
cout<<endl;
cout<<"The length of the linked list (the number of nodes) is: "<< length(first)<<endl;
cout<<"The sum of the linked list values (nodes) is: "<< sum(first)<<endl;
cout<<"The maximum value in the linked list (of the nodes) is: "<< max(first)<<endl;
return 0;
}
What am I doing wrong?
Upvotes: 0
Views: 124
Reputation: 1834
You have a little problem in your max
function,your function never goes through the entire list since you put the p = p->next
inside of the if
block, you must edit it as below
int max (struct Node *p){
int max = -32767;
while(p !=nullptr ){
if(p->data > max){
max = p->data;
}
p = p->next;
}
return max;
}
It's because of that you put the p = p->next
inside of the maximum checking scope and because of that if max
set to 18
, you couldn't point to the next
of the list, since you don't have any data in the list that is bigger than 18
,so list never finished and your iterator will be stopped in a fixed point and while loop will run forever!!
Upvotes: 3
Reputation: 596332
Your max()
function is not incrementing the node pointer on every iteration, like your other functions do. If it encounters a data
value that is not greater than the current max
then it gets stuck in an endless loop.
You need to move the iteration out of the inner if
block:
int max (struct Node *p){
int m = -1;
while (p){
if (p->data > max){
max = p->data;
}
p = p->next;
}
return m;
}
Upvotes: 0