private static void ResultNodeLinkList(DLinkList linklist) { Console.WriteLine("Result\n"); DListNode itr = linklist.m_head; for (int i = 1; i <= linklist.m_count; i++) { Console.WriteLine(itr.m_data); itr = itr.m_next; if (itr == null) { break; } } Console.WriteLine("\n"); itr = linklist.m_tail; for (int i = 1; i <= linklist.m_count; i++) { Console.WriteLine(itr.m_data); itr = itr.m_previous; if (itr == null) { break; } } }
public void Forth() { if (m_node != null) { m_node = m_node.m_next; } }
private static void ResultNodeList(DListNode list) { Console.WriteLine("Result\n"); DListNode itr = list.m_previous.m_previous; for (int i = 1; i <= 5; i++) { Console.WriteLine(itr.m_data); itr = itr.m_next; if (itr == null) { break; } } Console.WriteLine("\n"); itr = list.m_next.m_next; for (int i = 1; i <= 5; i++) { Console.WriteLine(itr.m_data); itr = itr.m_previous; if (itr == null) { break; } } }
public void Start() { if (m_list != null) { m_node = m_list.m_head; } }
public DLinkList() { DListNode itr = m_head; DListNode next; while (itr != null) { next = itr.m_next; itr = null; itr = next; } }
public void Prepend(int p_data) { if (m_tail == null) { m_tail = m_head = new DListNode(p_data); } else { m_head.InsertBefore(p_data); m_head = m_head.m_previous; } m_count++; }
public void Append(int p_data) { if (m_head == null) { m_head = m_tail = new DListNode(p_data); } else { m_tail.InsertAfter(p_data); m_tail = m_tail.m_next; } m_count++; }
public DListNode InsertBefore(int p_data) { DListNode newNode = new DListNode(p_data); newNode.m_previous = m_previous; if (m_previous != null) { m_previous.m_next = newNode; } m_previous = newNode; newNode.m_next = this; return(newNode); }
public DListNode InsertAfter(int p_data) { DListNode newNode = new DListNode(p_data); newNode.m_next = m_next; if (m_next != null) { m_next.m_previous = newNode; } m_next = newNode; newNode.m_previous = this; return(newNode); }
public void RemoveTail() { DListNode node = null; if (m_tail != null) { node = m_tail.m_previous; m_tail.m_data = null; m_tail = node; if (m_tail == null) { m_head = null; } m_count--; } }
public void RemoveHead() { DListNode node = null; if (m_head != null) { node = m_head.m_next; m_head.m_data = null; m_head = node; if (m_head == null) { m_tail = null; } m_count--; } }
static void Main(string[] args) { // 1 Console.WriteLine("1. List Node"); Console.WriteLine("InsertAfter, InsertBefore\n"); DListNode list = new DListNode(30); list.InsertAfter(50); list.InsertAfter(40); list.InsertBefore(10); list.InsertBefore(20); ResultNodeList(list); // 2 Console.WriteLine("\n///////////////////////////////////////////////"); Console.WriteLine("\n2. Linked List"); Console.WriteLine("Append, Prepend, RemoveHead, RemoveTail\n"); DLinkList linklist = new DLinkList(); linklist.Append(30); linklist.Append(40); linklist.Append(50); linklist.Prepend(20); linklist.Prepend(10); Console.WriteLine("Head is " + linklist.m_head.m_data + " and Tail is " + linklist.m_tail.m_data + "\n"); linklist.RemoveHead(); linklist.RemoveTail(); ResultNodeLinkList(linklist); CheckNullonRemoveNode(linklist); // 3 Console.WriteLine("\n///////////////////////////////////////////////"); Console.WriteLine("\n3. Iterators"); Console.WriteLine("Insert, Remove\n"); Console.WriteLine("The List Contains\n"); DListIterator itr = linklist.GetIterator(); DListNode node = linklist.m_head; linklist.Insert(itr, 25); linklist.Remove(itr, node); ResultIterator(itr); Console.ReadLine(); }
public void Insert(DListIterator p_iterator, int p_data) { if (p_iterator.m_list != this) { return; } if (p_iterator.m_node != null) { p_iterator.m_node.InsertAfter(p_data); if (p_iterator.m_node == m_tail) { m_tail = p_iterator.m_node.m_next; } m_count++; } else { Append(p_data); } }
public void Remove(DListIterator p_iterator, DListNode node) { if (p_iterator.m_list != this) { return; } if (p_iterator.m_node == null) { return; } if (p_iterator.m_node == m_head) { p_iterator.Forth(); RemoveHead(); } else { while (node.m_next != p_iterator.m_node) { node = node.m_next; } p_iterator.Forth(); if (node.m_next == m_tail) { m_tail = node; } node.m_next = null; node.m_next = p_iterator.m_node; } m_count--; }
public DListNode(int value) { m_data = value; m_next = null; m_previous = null; }
public DListIterator(DLinkList p_list = null, DListNode p_node = null) { m_list = p_list; m_node = p_node; }