0

I have LinkedList there is Remove(item) method that get as param item.

I would like to know what I have to override to delete item by specific param?

For example I have LinkedList<MyClass>, in MyClass I have variable int index and I would like to compare by this value...

Like, I am looking for some override compare(Obj) method that I can override and compare objects when I delete items from LinkedList by value that I need...

EDIT

Method Where does not fit, because actually I have a generic LinkedList implementation and actually my LinkedList looks like LinkedList<TYPE>, and it could be any of type. Because of this I can't use where because actually I don't know the exact type

Aleksey Timoshchenko
  • 3,987
  • 2
  • 40
  • 80

2 Answers2

0

The most simple way is to set a variable that will check if each value in a linked way is the one you're looking to delete and ALSO set a variable that is one value/step behind whatever the first variable is tracking.

That way, when the main variable (the one doing the checking) spots the list value you want to remove, you set the behind variable's "next value" equal to the main variable's "next value" thus overwriting and deleting it.

Each time you increment the main variable, also increment the behind variable so that you can keep it one step behind.

DIAGRAM (left is before, right is after): https://gyazo.com/4d989b6ff6249249c9d63a17a830a8c1

Basically, just set two variables: one that is checking each linked list value to find the one you're looking to delete and one BEHIND it so that when you find the value you want to delete, you set the variable that's behind it to the value in front of the main variable thus overwriting the deleted part.

0
using System;
using System.Collections.Generic;

namespace ConsoleAppCore
{
    public static class Extension
    {
        public static List<dynamic> Where(this IEnumerable<dynamic> list, Func<dynamic, bool> func)
        {
            List<dynamic> result = new List<dynamic>();
            foreach(dynamic item in list) {
                try {
                    if (func(item))
                        result.Add(item);
                }
                catch {
                    continue;
                }
            }
            return result;
        }
    }
    class YourClass
    {
        public int x = 5;
    }
    class Program
    {
        static void Main(string[] args)
        {
            LinkedList<dynamic> list = new LinkedList<dynamic>();
            list.AddAfter(list.AddAfter(list.AddAfter(list.AddAfter(
                list.AddAfter(list.AddAfter(list.AddAfter(list.AddFirst(
                    (decimal)1), 2), (double)3), "Hello"), 5), new YourClass()), (float)7), 8);

            var newlist = list.Where(i => i == "Hello");
            // only one logical operation at a time (caused exceptions break the logic)
            newlist.AddRange(list.Where(i => i.x == 5));
            newlist.AddRange(list.Where(i => i > 5));

            foreach(var i in newlist)
                Console.WriteLine(i);
        }
    }
}

Output

Hello, ConsoleAppCore.YourClass, 7, 8

Arthur Grigoryan
  • 356
  • 3
  • 11