Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp5
- {
- class List<E> : IEnumerable where E : IComparable
- {
- private Node head;
- private int count;
- private Node tail;
- public int getCount()
- {
- return this.count;
- }
- public List()
- {
- this.head = null;
- this.count = 0;
- this.tail = null;
- }
- public void AddFirst(E item)
- {
- if (this.count == 0)
- {
- this.head = this.tail = new Node(item, null, null);
- ++this.count;
- }
- else
- {
- Node temp = new Node(item, null, null);
- temp.next = this.head;
- this.head.prev = temp;
- this.head = temp;
- ++this.count;
- }
- }
- public void BetterAddFirst(E item)
- {
- Node temp = new Node(item, this.head, null);
- if (this.count == 0)
- {
- this.tail = temp;
- }
- else
- {
- this.head.prev = temp;
- }
- this.head = temp;
- ++this.count;
- }
- public void AddLast(E item)
- {
- if (this.count == 0)
- {
- this.head = this.tail = new Node(item, null, null);
- ++this.count;
- }
- else
- {
- Node temp = new Node(item, null, null);
- temp.prev = this.tail;
- this.tail.next = temp;
- this.tail = temp;
- ++this.count;
- }
- }
- public void BetterAddLast(E item)
- {
- Node temp = new Node(item, null, this.tail);
- if (this.count == 0)
- {
- this.head = temp;
- }
- else
- {
- this.tail.next = temp;
- }
- this.tail = temp;
- ++this.count;
- }
- public void AddInOrder(E item)
- {
- if (this.count == 0)
- {
- this.head = this.tail = new Node(item, null, null);
- ++this.count;
- }
- else
- {
- if (item.CompareTo(this.head.item) <= 0)
- {
- this.AddFirst(item);
- }
- else
- {
- if (item.CompareTo(this.tail.item) > 0)
- {
- this.AddLast(item);
- }
- else
- {
- Node skip = this.head;
- for (; (skip != null) && (item.CompareTo(skip.item) > 0); skip = skip.next) { }
- if (skip == null)
- {
- throw new Exception("fallo en el orden de los elementos");
- }
- else
- {
- Node temp = new Node(item, skip, skip.prev);
- skip.prev.next = temp;
- skip.prev = temp;
- ++this.count;
- }
- }
- }
- }
- }
- public E RemoveFirst()
- {
- if (this.count == 0)
- {
- throw new Exception("LA LISTA ESTA VACIA!");
- }
- E item = this.head.item;
- this.head = this.head.next;
- if (this.head == null)
- {
- this.tail = null;
- }
- else
- {
- this.head.prev = null;
- }
- --this.count;
- return item;
- }
- public E RemoveLast()
- {
- if (this.count == 0)
- {
- throw new Exception("LA LISTA ESTA VACIA");
- }
- E item = this.tail.item;
- if (this.head.next == null)
- {
- this.head = this.tail = null;
- }
- else
- {
- this.tail = this.tail.prev;
- this.tail.next = null;
- }
- --this.count;
- return item;
- }
- public bool FindAndRemove(E item)
- {
- if (this.count == 0)
- {
- return false;
- }
- Node skip = this.head;
- for (; (skip != null) && !(item.CompareTo(skip.item) == 0); skip = skip.next) { }
- if (skip == null)
- {
- return false;
- }
- else
- {
- if (skip.prev == null)
- {
- this.RemoveFirst();
- return true;
- }
- else
- {
- if (skip.next == null)
- {
- this.RemoveLast();
- return true;
- }
- else
- {
- skip.prev.next = skip.next;
- skip.next.prev = skip.prev;
- skip.prev = skip.next = null;
- return true;
- }
- }
- }
- }
- private class Node
- {
- public E item;
- public Node next;
- public Node prev;
- public Node()
- {
- this.item = default(E);
- this.next = null;
- this.prev = null;
- }
- public Node(E item)
- {
- this.item = item;
- this.next = null;
- this.prev = null;
- }
- public Node(E item, Node next)
- {
- this.item = item;
- this.next = next;
- this.prev = null;
- }
- public Node(E item, Node next, Node prev)
- {
- this.item = item;
- this.next = next;
- this.prev = prev;
- }
- public override string ToString()
- {
- return this.item.ToString();
- }
- }
- public IEnumerator GetEnumerator()
- {
- return MyEnumerator();
- }
- private IEnumerator MyEnumerator()
- {
- for (Node skip = head; skip != null; skip = skip.next)
- {
- yield return skip.item;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement