Linked List Collection Example

This demonstration is going over how a linked list collection can be used. Linked lists give the ability to insert and remove elements quickly. Granted there isn’t much of a performance increase versus a properly sized array or a normal List. This example is just to show how a LinkedList can be used if there’s ever a need.LinkedListProgram LinkedListSolution

 
Program.cs code contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinkedList
{
    class Program
    {
        /// <summary>
        /// Demonstrating how a linked list can work.
        /// Normally there would be some error correction if this was production code.
        /// However this is just a proof of concept example.
        /// </summary>
        /// <param name="args"></param>

        static void Main(string[] args)
        {
            // Creating a new empty link list to add elements to.
            Console.WriteLine("****Blank Linked List Results****");
            LinkedList<string> blankPresidents = createLinkedList();
            blankPresidents.AddLast("JFK");
            blankPresidents.AddLast("Lyndon B Johnson");
            blankPresidents.AddLast("Richard Nixon");
            blankPresidents.AddLast("Jimmy Carter");

            foreach (string item in blankPresidents)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();

            // Creating an already populated list and using methods to do things.
            Console.WriteLine("****Initialized Linked List Results****");
            LinkedList<string> initializedPresidents = createInitializedLinkedList();

            // Changing the value of the first element.
            removeFirstElement(initializedPresidents);
            addFirstElements(initializedPresidents, "John F. Kennedy");

            // Adding an element to the middle of the list.
            LinkedListNode<string> findNixon = findElements(initializedPresidents, "Richard Nixon");
            addElementAfter(initializedPresidents, findNixon, "Gerald Ford");

            foreach (string item in initializedPresidents)
            {
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }

        // Private methods
        //
        // Method used to create a normal blank link list.
        private static LinkedList<string> createLinkedList()
        {
            return new LinkedList<string>();
        }

        // Method used for creating an initialized linked list with pre-existing items.
        private static LinkedList<string> createInitializedLinkedList()
        {
            LinkedList<string> presidents = new LinkedList<string>();
            presidents.AddLast("JFK");
            presidents.AddLast("Lyndon B Johnson");
            presidents.AddLast("Richard Nixon");
            presidents.AddLast("Jimmy Carter");

            return presidents;
        }

        // Method used to remove string elements at the beginning of a linked list.
        private static void removeFirstElement(LinkedList<string> list)
        {
            list.RemoveFirst();
        }

        // Method used to add string elements to the beginning of a linked list.
        private static void addFirstElements(LinkedList<string> list, string itemToAdd)
        {
            list.AddFirst(itemToAdd);
        }

        // Method used to add a string element after another one in the linked list.
        private static void addElementAfter(LinkedList<string> list, LinkedListNode<string> elementBefore, string elementToAddAfter)
        {
            list.AddAfter(elementBefore, elementToAddAfter);
        }

        // Method used to find string elements in a linked list.
        private static LinkedListNode<string> findElements(LinkedList<string> list, string itemToFind)
        {
            LinkedListNode<string> foundItem = list.Find(itemToFind);
            return foundItem;
        }
    }
}

 

There are a couple things going on here. One method is creating an empty LinkedList. This is a simple example is using the AddLast() method to add elements to the list one after another. Then iterating thru the collection and displaying the elements.

The other example is creating a LinkedList with the same elements as earlier except they are already added when the mthod is called on. From here I have methods that are removing the first element, adding an element after another existing one, and adding an element to the beginning of the collection. Take notice of how I’m adding an element in the middle of the list. In adding Gerald Ford after Richard Nixon. I’m having to locate the Richard Nixon element location to add Gerald Ford. This is done using the Find() method that the LinkedList class has. Then using the AddAfter() method that takes the before and after elements to add. Then simply iterating thru the collection and displaying the elements.

You will notice that I’m using some helper methods I’ve created to automate some things. It may seem like I’m jumping around. I’m really not. This is just my continuing way of showing you how things can be accomplished.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s