static void printDLL(Node node, bool fwd) { Node cur = node; if (fwd) { Console.Write("DLL fwd: "); while (true) { Console.Write("{0} ", cur.data); if (cur.next == node) break; cur = cur.next; } } else { Console.Write("DLL back: "); while (true) { Console.Write("{0} ", cur.data); if (cur.prev == node) break; cur = cur.prev; } } Console.WriteLine("\n"); }
public void InsertAt(int index, Object item) { Node aux = GetItem(index); if (aux == null) { Console.WriteLine("Index not found. *Item added in the end."); Insert(item); // Insert } else if (aux == head) // Insert in the head { head.prev = new Node(item, head, null); head = head.prev; } else if (aux == tail) // Insert in the tail { tail.next = new Node(item, null, tail); tail = tail.next; } else // Insert in the middle { Node prev = aux.prev; prev.next = new Node(item, aux, prev); aux.prev = prev.next; } length++; }
public void Insert(Object item) { if (length == 0) head = tail = new Node(item, null, null); else { tail.next = new Node(item, null, tail); tail = tail.next; } length++; }
public void Add(object item) { if (head == null) { head = new Node(item); tail = head; } else { Node newNode = new Node(item, tail); tail = newNode; } count++; }
public void DllContains() { var list = new DlList<Int>(); list.AddLast(new Int(1)); list.AddLast(new Int(2)); list.AddLast(new Int(3)); list.AddLast(new Int(4)); var trueNode = new Node<Int>(new Int(4)); var falseNode = new Node<Int>(new Int(5)); Assert.IsTrue(list.Contains(trueNode)); Assert.IsFalse(list.Contains(falseNode.Data)); }
public bool Add(int value) { Node curNode = _headNode; while (curNode.Next != null) { curNode = curNode.Next; } Node newNode = new Node(value); curNode.Next = newNode; newNode.Prev = curNode; return true; }
static Node rev(Node dll) { Node prev = dll.prev; Node next; Node cur = dll; Node save = cur; while (prev.prev != save) { next = cur.next; cur.next = prev; cur.prev = next; prev = cur; cur = next; } return prev; }
static void Main(string[] args) { Node node = new Node(1); node.next = new Node(2); node.next.next = new Node(3); node.prev = node.next.next; node.next.prev = node; node.next.next.prev = node.next; node.next.next.next = node; Console.WriteLine("Before reversing"); printDLL(node, true); printDLL(node, false); Console.WriteLine("After reversing"); node = rev(node); printDLL(node, true); printDLL(node, false); Console.ReadLine(); }
public LinkedList() { _headNode = new Node(); }
public void InsertAt(int index, object item) { if (index < 0 || index > count) throw new ArgumentOutOfRangeException("Index is out of range"); Node newNode = new Node(item); if (index == 0) { if (count == 0) { this.Add(item); return; } else { newNode.Prev = head.Prev; newNode.Next = head; head.Prev = newNode; head = newNode; } } else if (index == count) { newNode.Prev = tail; tail.Next = newNode; tail = newNode; } else { Node current = head; for (int i = 0; i < index; i++) current = current.Next; newNode.Prev = current.Prev; current.Prev = newNode; newNode.Next = current.Prev.Next; current.Prev.Next = newNode; } count++; }
public DoublyLinkedList() { this.head = null; this.tail = null; this.count = 0; }
public void RemoveAll() { if (!IsEmpty()) head = tail = new Node(); }
public DoublyLinkedList() { length = 0; head = tail = new Node(); }
public void RemoveAt(int index) { if (index < 0 || index >= count) throw new ArgumentOutOfRangeException("Index is out of range"); if (index == 0) { if (count == 1) { head = null; tail = null; } else { head = head.Next; head.Prev = null; } } else if (index == count - 1) { tail.Prev.Next = tail.Next; tail = tail.Prev; } else { Node current = head; for (int i = 0; i < index; i++) current = current.Next; current.Prev.Next = current.Next; current.Next.Prev = current.Prev; } count--; }
public Node(int data, Node prev, Node next) { this.data = data; this.prev = prev; this.next = next; }
public void RemoveAt(int index) { if (!IsEmpty()) { Node aux = GetItem(index); // Search and get the nod if (aux == null) Console.WriteLine("Index not found."); else if (aux == head) // Remove from the head { if (head == tail) head = tail = new Node(); else { aux = aux.next; aux.prev = null; head = aux; } } else if (aux == tail) // Remove from the tail { if (head == tail) head = tail = new Node(); else { tail = aux.prev; tail.next = null; aux.prev = aux.next = null; } } else // Remove from the middle { Node prev = aux.prev; Node next = aux.next; prev.next = next; next.prev = prev; aux.next = aux.prev = null; } length--; } }
public Node(object data) { this.data = data; this.next = null; this.prev = null; }
public Node(object data, Node prevNode) { this.data = data; this.prev = prevNode; prevNode.next = this; }
/* - Alternativna versiq na metoda Remove napisan po-dolu, izpolzvaiki metoda RemoveAt, napisan po-gore public void Remove(object item) { int currentIndex = 0; Node current = head; while (current != null) { if (current.Data.Equals(item)) { this.RemoveAt(currentIndex); return; } current = current.Next; currentIndex++; } throw new ArgumentException("There is no such item."); } */ public void Remove(object item) { int itemIndex = 0; Node current = head; for (int i = 0; i < count; i++) { if (current.Data.Equals(item)) { if (itemIndex == 0) { if (count == 1) { head = null; tail = null; } else { head = current.Next; head.Prev = null; } } else if (itemIndex == count - 1) { current.Prev.Next = current.Next; tail = current.Prev; } else { current.Prev.Next = current.Next; current.Next.Prev = current.Prev; } count--; return; } current = current.Next; itemIndex++; } throw new ArgumentException("There is no such item!"); }
public Node(Object item, Node next, Node prev) { this.item = item; this.next = next; this.prev = prev; }
public Node() { item = next = prev = null; }