Пример #1
0
 public LinkedMapItem(string key, string value, LinkedMapItem prev, LinkedMapItem next = null)
 {
     this.key   = key;
     this.value = value;
     this.prev  = prev;
     this.next  = next;
 }
Пример #2
0
        public static void Solve()
        {
            using (var input = new StreamReader("linkedmap.in"))
            {
                using (var output = new StreamWriter("linkedmap.out"))
                {
                    MyLinkedMap   map      = new MyLinkedMap();
                    LinkedMapItem lastItem = null;
                    LinkedMapItem deleted  = null;
                    while (true)
                    {
                        string[] line = input.ReadLine()?.Trim().Split();
                        if (line == null)
                        {
                            break;
                        }
                        switch (line[0])
                        {
                        case "put":
                            lastItem = map.Put(new LinkedMapItem(line[1], line[2], lastItem));
                            break;

                        case "delete":
                            deleted = map.Delete(line[1]);
                            if (deleted?.key == lastItem?.key)
                            {
                                lastItem = deleted?.prev;
                            }
                            break;

                        case "get":
                            output.WriteLine(map.Get(line[1])?.value ?? "none");
                            break;

                        case "prev":
                            output.WriteLine(map.Prev(line[1])?.value ?? "none");
                            break;

                        case "next":
                            output.WriteLine(map.Next(line[1])?.value ?? "none");
                            break;
                        }
                    }
                }
            }
        }
Пример #3
0
        public LinkedMapItem Delete(string key)
        {
            int hashCode = Hash(key);

            if (hashTable[hashCode] == null || !hashTable[hashCode].Exists(i => i.key == key))
            {
                return(null);
            }
            LinkedMapItem item = hashTable[hashCode].Find(i => i.key == key);

            if (item.prev != null)
            {
                item.prev.next = item.next;
            }
            if (item.next != null)
            {
                item.next.prev = item.prev;
            }
            item = hashTable[hashCode].Find(i => i.key == key);
            hashTable[hashCode].RemoveAll(i => i.key == key);
            return(item);
        }
Пример #4
0
        public LinkedMapItem Put(LinkedMapItem item)
        {
            int hashCode = Hash(item.key);

            if (hashTable[hashCode] == null)
            {
                hashTable[hashCode] = new List <LinkedMapItem>();
            }
            if (!hashTable[hashCode].Exists(i => i.key == item.key))
            {
                hashTable[hashCode].Add(item);
                if (item.prev != null)
                {
                    item.prev.next = item;
                }
                return(item);
            }
            else
            {
                hashTable[hashCode].Find(i => i.key == item.key).value = item.value;
                return(item.prev);
            }
        }