static void Main(string[] args) { //Make a LinkedList and an iterator for the linkedlist LinkedList LinkedList = new LinkedList(); ListIter iter = new ListIter(LinkedList); //Add data with the iterator in the linkedlist iter.InsertAfter("Test1"); iter.InsertAfter("Test2"); iter.InsertAfter("Tets3"); iter.InsertAfter("Test4"); //Test if the getCurrent method works Console.WriteLine(iter.GetCurrent()); iter.InsertAfter("Test5"); iter.InsertBefore("Test6"); iter.InsertAfter("Test7"); //Test if the remove method works iter.Remove(); //Print the list of the linkedlist LinkedList.PrintList(); Console.WriteLine(LinkedList.getFirst()); Console.ReadLine(); }
static void Main() { LinkedList MyList = new LinkedList(); ListIter iter = new ListIter(MyList); string choice, value; try { iter.InsertAfter("David"); iter.InsertAfter("Mike"); iter.InsertAfter("Raymod"); iter.InsertAfter("Bernica"); iter.InsertAfter("Jennifer"); iter.InsertAfter("Donnie"); iter.InsertAfter("Michael"); iter.InsertAfter("Terril"); iter.InsertAfter("Mayo"); iter.InsertAfter("Clayton"); while (true) { Console.WriteLine("(n) Move to next node"); Console.WriteLine("(g) Get value in current node"); Console.WriteLine("(r) Reset iterator"); Console.WriteLine("(s) Show complete list"); Console.WriteLine("(a) Insert after"); Console.WriteLine("(b) Insert before"); Console.WriteLine("(c) Clear the screen"); Console.WriteLine("(x) Exit"); Console.WriteLine(); Console.Write("Enter your choice: "); choice = Console.ReadLine(); choice = choice.ToLower(); char[] onechar = choice.ToCharArray(); switch (onechar[0]) { case 'n': if (!(MyList.IsEmpty()) && (!(iter.AtEnd()))) { iter.NextLink(); } else { Console.WriteLine("Can't move to next link."); } break; case 'g': if (!(MyList.IsEmpty())) { Console.WriteLine("Element: " + iter.GetCurrent().Element); } else { Console.WriteLine("List is empty."); } break; case 'r': iter.Reset(); break; case 's': if (!(MyList.IsEmpty())) { MyList.ShowList(); } else { Console.WriteLine("List is Empty"); } break; case 'a': Console.WriteLine(); Console.Write("Enter value to insert"); value = Console.ReadLine(); iter.InsertAfter(value); break; case 'b': Console.WriteLine(); Console.Write("Enter value to insert"); value = Console.ReadLine(); iter.InsertBefore(value); break; case 'c': //clear the screen break; case 'x': //end of program break; } } } catch (InsertBeforeHeaderException e) { Console.WriteLine(e.Message); } }