Exemple #1
0
        public CustomDictionary <T, U> Remove(T key)
        {
            CustomNode <KeyValuePair <T, U> > node = this.head;

            for (int i = 0; i < this.Count; i++)
            {
                if (node.Data.Key.Equals(key))
                {
                    if (node.Previous == null)
                    {
                        this.head = node.Next;
                    }
                    else
                    {
                        node.Previous.Next = node.Next;
                        if (node.Next != null)
                        {
                            node.Next.Previous = node.Previous;
                        }
                    }
                    this.Count--;
                }
                node = node.Next;
            }
            return(this);
        }
Exemple #2
0
        public T[] ToArray()
        {
            T[]            result = new T[this.Count];
            CustomNode <T> node   = this.head;

            for (int i = 0; i < this.Count; i++)
            {
                result[i] = node.Data;
                node      = node.Next;
            }
            return(result);
        }
Exemple #3
0
        public CustomQueue <T> Enqueue(T element)
        {
            CustomNode <T> node = new CustomNode <T>(element);

            if (this.tail != null)
            {
                this.tail.Next = node;
            }
            node.Previous = this.tail;
            this.tail     = node;
            this.Count++;
            return(this);
        }
Exemple #4
0
        public CustomStack <T> Push(T element)
        {
            CustomNode <T> node = new CustomNode <T>(element);

            node.Next = this.head;
            if (this.head != null)
            {
                this.head.Previous = node;
            }
            this.head = node;
            this.Count++;
            return(this);
        }
Exemple #5
0
        private void CheckKeyExists(T key)
        {
            CustomNode <KeyValuePair <T, U> > node = this.head;

            for (int i = 0; i < this.Count; i++)
            {
                if (node.Data.Key.Equals(key))
                {
                    throw new ArgumentException("This key already exists in the dictionary");
                }
                node = node.Next;
            }
        }
Exemple #6
0
        public T Dequeue()
        {
            CustomNode <T> result = this.head;

            if (head == null)
            {
                throw new NullReferenceException("Nothing to dequeue");
            }
            this.head = this.head.Next;
            if (this.head != null)
            {
                this.head.Previous = null;
            }
            this.Count--;
            return(result.Data);
        }
Exemple #7
0
        public T Pop()
        {
            CustomNode <T> result = this.head;

            if (this.head == null)
            {
                throw new NullReferenceException("No elements to pop");
            }
            this.head = this.head.Next;
            if (this.head != null)
            {
                this.head.Previous = null;
            }
            this.Count--;
            return(result.Data);
        }
Exemple #8
0
        public override string ToString()
        {
            string         s    = "[head:";
            CustomNode <T> node = this.head;

            for (int i = 0; i < this.Count - 1; i++)
            {
                s   += String.Format("{0}->", node.Data.ToString());
                node = node.Next;
            }
            if (this.Count != 0)
            {
                s += node.Data.ToString();
            }
            s += ":tail]";
            return(s);
        }
Exemple #9
0
        public override string ToString()
        {
            string s = "[";
            CustomNode <KeyValuePair <T, U> > node = this.head;

            for (int i = 0; i < this.Count - 1; i++)
            {
                s   += String.Format(" {0};", node.Data.ToString());
                node = node.Next;
            }
            if (this.Count != 0)
            {
                s += " " + node.Data.ToString();
            }
            s += " ]";
            return(s);
        }
Exemple #10
0
 public CustomDictionary <T, U> Add(KeyValuePair <T, U> element)
 {
     CheckKeyExists(element.Key);
     if (tail != null)
     {
         tail.Next = new CustomNode <KeyValuePair <T, U> >(element)
         {
             Previous = tail
         };
     }
     else
     {
         tail = new CustomNode <KeyValuePair <T, U> >(element);
         head = tail;
     }
     tail = tail.Next;
     Count++;
     return(this);
 }
Exemple #11
0
 public U this[T key]
 {
     get
     {
         CustomNode <KeyValuePair <T, U> > node = this.head;
         for (int i = 0; i < this.Count; i++)
         {
             if (node.Data.Key.Equals(key))
             {
                 return(node.Data.Value);
             }
             node = node.Next;
         }
         throw new KeyNotFoundException();
     }
     set
     {
         CheckKeyExists(key);
         this.Add(new KeyValuePair <T, U>(key, value));
     }
 }
Exemple #12
0
 public CustomQueue(T head)
 {
     this.head  = new CustomNode <T>(head);
     this.tail  = this.head;
     this.Count = 1;
 }
Exemple #13
0
 public CustomStack(T head)
 {
     this.head = new CustomNode <T>(head);
     //this.tail = this.head;
     this.Count = 1;
 }
Exemple #14
0
 public CustomDictionary(KeyValuePair <T, U> firstElement)
 {
     head = new CustomNode <KeyValuePair <T, U> >(firstElement);
     tail = head;
     Count++;
 }
Exemple #15
0
 public CustomNode(T data, CustomNode <T> previous = null, CustomNode <T> next = null)
 {
     this.Data     = data;
     this.Previous = previous;
     this.Next     = next;
 }