Home > front end >  I am not understanding why my code for Linked List is not working?
I am not understanding why my code for Linked List is not working?

Time:01-28

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.

CodePudding user response:

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);

CodePudding user response:

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

  •  Tags:  
  • Related