I am trying to figure out how to find and delete max number in list. (I am not allowed to use Dictionary, List, LinkedList, SortedList and similar ones).
The list of numbers is converted to array { 1055, 2, 29, 8, 7, 2000, 29, 8, 22, 6, 29, } the "s" is the list.
I am able to find the highest number in list and I feel like the code for deleting it is also correct but the problem is the way it works in my head is 1st loop to find the highest number:
int tmp = 0;
while (s != null)
{
if(tmp < s.data)
{
tmp = s.data;
}
s = s.next;
}
Console.WriteLine(tmp);
return null;
This will correctly return me 2000.
And 2nd loop should basically move to the next number so the highest one does not show.
while (s != null)
{
if (s.data == tmp)
{
s = s.next;
}
Console.Write(s.data + " ");
s = s.next;
}
I tried to play around with the loops but cant find out how to combine this together to make it work.
Thank you for any advice. I am open to a different approach.
Adding all relevant code to the case
class Seznam
{
public int data;
public Seznam next;
public Seznam prev;
static void Main(string[] args)
{
int[] pole = { 1055, 2, 29, 8, 7, 2000, 29, 8, 22, 6, 29, };
Seznam s = convertArray(pole); //This is the list
s = DeleteMax(s);
Console.ReadLine();
}
static Seznam DeleteMax(Seznam s)
{
int tmp = 0;
while (s != null)
{
if(tmp < s.data)
{
tmp = s.data;
}
s = s.next;
}
Console.WriteLine(tmp);
return null;
}
}