public void PrintList(MyLLNode llist) { Console.WriteLine(); while (llist != null) { Console.Write("{0}, ", llist.Data); llist = llist.Next; } }
public MyLLNode FindFirst(MyLLNode head, int data) { while (head != null && head.Data != data) { head = head.Next; } return(head); }
public void InsertAfter(MyLLNode llist, int precedingData, int data) { MyLLNode preceedingNode = FindFirst(llist, precedingData); if (preceedingNode == null) { throw new IndexOutOfRangeException(string.Format("Cannot find node '{0}'", precedingData)); } MyLLNode newNode = new MyLLNode(data); newNode.Next = preceedingNode.Next; preceedingNode.Next = newNode; }
public MyLLNode InsertAtStart(MyLLNode llist, int data) { if (llist == null) { llist = new MyLLNode(data); } else { MyLLNode newNode = new MyLLNode(data); newNode.Next = llist; llist = newNode; } return(llist); }
public MyLLNode InsertAtEnd(MyLLNode llist, int data) { if (llist == null) { llist = new MyLLNode(data); } else { MyLLNode head = llist; MyLLNode newNode = new MyLLNode(data); while (head.Next != null) { head = head.Next; } head.Next = newNode; } return(llist); }
static void Main(string[] args) { MyLinkedList myList = new MyLinkedList(); //MyLLNode head = myList.CreateList(2); MyLLNode head = myList.InsertAtEnd(null, 2); //2 myList.PrintList(head); head = myList.InsertAtStart(head, 4); //4, 2 myList.PrintList(head); head = myList.InsertAtStart(head, 6); //6, 4, 2 myList.PrintList(head); head = myList.InsertAtEnd(head, 1); //6, 4, 2, 1 myList.PrintList(head); myList.InsertAfter(head, 2, 5); //6, 4, 2, 5, 1 myList.PrintList(head); myList.InsertAfter(head, 1, 3); //6, 4, 2, 5, 1 myList.PrintList(head); Console.Read(); }