0

I don't know what is wrong but the program is just not working. I will be really grateful if you help me.

using System;
using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        LinkedList<ulong> integers = new LinkedList<ulong>();
        
        for(ulong i = 1; i <= 10; i++)
        {
            integers.AddLast(i);
        }
        
        Console.Write("Enter an integer: ");
        ulong x = ulong.Parse(Console.ReadLine());
        Console.WriteLine();
        
        foreach(ulong integer in integers)
        {
            if(integer > x)
            {
                integers.Remove(integer);
            }
        }
        
        integers.AddLast(x);
        integers.AddLast(10);
        
        foreach(ulong integer in integers)
        {
            Console.WriteLine(integer);
        }
    }
}

The condition of my task is: Create a LinkedList of 10 positive integers. The finished list is displayed on the screen. Enter a positive integer x on the keyboard, delete all items greater than x from the list, and add an item with a value of 10 after the last item in the x-value list. Display the changed list.

  • 1
    welcome to stackoverflow. can you please be more specific about "not working"? what exactly is happening, what _should_ be happening, and what error messages are you getting, if any? i recommend [taking the tour](https://stackoverflow.com/tour), as well as reading [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and [what's on topic](https://stackoverflow.com/help/on-topic). – Franz Gleichmann Jan 28 '22 at 12:35
  • The console opens and allows me to enter the specified number of integers, but after entering them gives me an error but does not show me exactly what it is. I'm new to programming so I can't explain exactly what I mean. – Денислав Цанков Jan 28 '22 at 12:39
  • "but does not show me exactly what it is" that's not something c# usually does. do you mean: _the console closes_ instantly? also: what numbers are you inserting? and please ***do*** read the articles i've linked to, _thank you very much_. and i also recommend you learn how to use the debugger - so your program _halts_ when it encounters an error, and you can inspect it at the exact point of execution. – Franz Gleichmann Jan 28 '22 at 12:43
  • Do you want your list to contain unique sorted items. Your code implies this to a certain degree but atleast the inserted x can be in the list twice currently. You might want to rethink your if statement. – Ralf Jan 28 '22 at 13:01

2 Answers2

1

The first thing that calls my attention is that inside the foreach loop you are modifying the list that you are iterating over: integers

Try storing the list of items you want to remove in a separate list first. Then iterate through that list and delete all.

var toBeDeleted=new List<ulong>;

foreach(ulong integer in integers)
        {
            if(integer > x)
            {
                toBeDeleted.Add(integer);
            }
        }

foreach(ulong item in toBeDeleted)
    {
         integers.Remove(item);
    }

You can also use the Remove(LinkedListNode) method. In that case in the code would be something like this(didn't test it so you might need to touch it to make it compile)

var toBeDeleted=new List<LinkedListNode<ulong>>;

foreach(LinkedListNode<ulong> integer in integers)
        {
            if(integer > x) //might need to cast it to ulong if it doesn't happen automatically.
            {
                toBeDeleted.Add(integer);
            }
        }

integers.Remove(toBeDeleted);
Martín La Rosa
  • 790
  • 4
  • 17
0

I ran your code. You are removing items in foreach loop. Try using "for" loop and iterating from the last element, to the first.

class Program
    {
        static void Main(string[] args)
        {
            LinkedList<ulong> integers = new LinkedList<ulong>();

            for (ulong i = 1; i <= 10; i++)
            {
                integers.AddLast(i);
            }

            Console.Write("Enter an integer: ");
            ulong x = ulong.Parse(Console.ReadLine());
            Console.WriteLine();

            for (int i = integers.Count-1; i >= 0; i--)
            {
                ulong a = integers.ElementAt(i);
                if (a > x)
                {
                    integers.Remove(a);
                }
            }

            integers.AddLast(x);
            integers.AddLast(10);

            foreach (ulong integer in integers)
            {
                Console.WriteLine(integer);
            }
            Console.ReadKey();
        }
    }

C# List - Removing items while looping / iterating

PolarnyMis
  • 45
  • 5
  • 1
    When changing the direction of iteration using Remove might be wrong. LinkedList.Remove removes the first occurrence of a value. – Ralf Jan 28 '22 at 12:58