Shining Star Services LLC
C# Custom LinkedList Main() method - Learning C# - Part 7
In Part 1 of this series, we created a console application and an abstract class. In Part 2 we setup our LinkedList derived class and defined our properties and setup our constructor. In Part 3, we defined our Push and Pop methods. We implemented both a PopLifo and PopFifo method. In part 4 we defined our PrintLifo method to print nodes from the top of the stack. We also discussed C# Operators. In Part 5 we defined a PrintFifo() method, which used a GetNode() helper method to find specific nodes, allowing us to print nodes from the bottom of the stack. In Part 6 we created a FindItems() method which printed a listing to the screen of all items based on the category or item name.

In this final article, we will look at our Main() program and how to use our new LinkedList class.

   class Program
   {
   static void Main(string[] args)
   {

   LinkedList ListItems = new LinkedList();
   ListItems.Push("Kitchen", "Dining Room Set");
   ListItems.Push("Living Category", "Sofa and Recliner");
   ListItems.Push("Living Category", "Stereo and Sound System");
   ListItems.Push("Electronics", "PS3");
   ListItems.Push("Electronics", "Wii");
   ListItems.Push("Electronics", "Nook");
   ListItems.Push("Electronics", "Big Screen TV");
   ListItems.PrintFifo();
   Console.WriteLine("Pop");
   ListItems.PopFifo();
   ListItems.PopLifo();
   ListItems.PrintLifo();
   ListItems.FindItems("Electronics");
   Console.ReadLine();
   }
   }
}


When we create a console application, our Program class and Main() method are setup by default. Our Main() method is defined as a static method. The "static" keyword instructs the system to create only one instance of the method regardless of how many instances of its class are created.

In our Main() method, we create a new LinkedList and name it ListItems. Rather than using this to create nodes, as we did with the Head and Next nodes which were also LinkedLists, we are going to use this to call the methods defined in the LinkedList class. When we type "ListItems." intellisense will kick in and display all public methods defined in our LinkedList class. Now we can use this to Push() inventory items to our list, PrintFifo() or PrintLifo(), PopFifo() or PopLifo(), and FindItems(). We want to follow up with Console.ReadLine(); to keep the text from zooping off the screen and returning to the program. Console.ReadLine(); will allows us to stop and wait for input, thus allowing us to see our results.




In our example above, we print our items in LIFO order, then we remove the top and bottom nodes, then print our remaining items in FIFO order. Then we search for items in the "Electronics" category and display them to the screen. Play around with it, put in break points and step through the code and see what is happening. And be sure to try the PrintFifo() method as well!

Nannette


© Copyright 1997-2017 Shining Star Services LLC, Nannette Thacker. All Rights Reserved.