0

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;

        }
    }
ThePejnMan
  • 79
  • 6
  • What _are_ you allowed to use? Are you, for example, allowed to sort the array? Are you allowed to use LINQ? Is https://stackoverflow.com/a/457501/34092 an option? What if the max number is there three times - do you want all three instances removed? – mjwills Aug 04 '20 at 13:36
  • Ideal solution would be as basic as possible (if, for, while etc) even if longer this is just to learn basics of functions. We sorting via loops is possible. If more there is more than one occurence then remove the first one. – ThePejnMan Aug 04 '20 at 13:45
  • Please share a [mcve] of your progress so far. Fundamentally, this is an odd question to ask of us - because the constraints are not _usual_ (i.e. no-one answering questions on SO is actually going to write code like this in their real life). – mjwills Aug 04 '20 at 13:46
  • Is this a custom linked list implementation? You need to find the node before the node to remove then re-link it like `prev.next = found.next;` Handling special case where `found` is the head node. – 001 Aug 04 '20 at 13:48
  • Added relevant portion of the code. It is probably very unusual problem with restrictions but hopefuly I will find a way to do it. Otherwise I will scrap it down and start over and try to find different approach. – ThePejnMan Aug 04 '20 at 14:01
  • @JohnnyMopp Yes, I am able to find the number I want to delete and also I am able to delete it but right now I have these 2 things separate and I am not sure how to combine them. First code is to find the max and second one is to delete the max but since I have them in 2 loops I dont know how to join it together. – ThePejnMan Aug 04 '20 at 14:02

2 Answers2

0

If you would use another variable then you could save a counter to know where is the highest num located in the array.

Just like this:

int tmp = 0;
int location = 0;
int counter = 0;
while (s != null)
{
    if (tmp < s.data)
    {
        tmp = s.data;
        location = counter;
    }
    s = s.next;
    counter = counter + 1;
}
Console.WriteLine(tmp);
return null;

After that you can find that one and remove it easily.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
sssalpert
  • 11
  • 2
  • My first loop is kinda similar to this but my point is probably if its possible to delete the number in the same loop where I find the number or do I have to do it somewhere else? I tried to use 2 while loops in one method but that didnt work out. – ThePejnMan Aug 04 '20 at 14:04
0
public static void DeleteMax(Seznam s)
{
    int tmp = 0;
    Seznam max = s;
    while (s != null)
    {
        if (tmp < s.data)
        {
            tmp = s.data;
            max = s;
        }
        s = s.next;
    }
    if (max != null)
    {
        max.prev.next = max.next;
        max.next.prev = max.prev;
    }            
}
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49