Page 98 - CSharp/C#
P. 98

// initialize a LinkedList of integers
         LinkedList list = new LinkedList<int>();

         // add some numbers to our list.
         list.AddLast(3);
         list.AddLast(5);
         list.AddLast(8);

         // the list currently is 3, 5, 8

         list.AddFirst(2);
         // the list now is 2, 3, 5, 8

         list.RemoveFirst();
         // the list is now 3, 5, 8

         list.RemoveLast();
         // the list is now 3, 5


        Note that LinkedList<T> represents the doubly linked list. So, it's simply collection of nodes and
        each node contains an element of type T. Each node is linked to the preceding node and the
        following node.


        Queue


         // Initalize a new queue of integers
         var queue = new Queue<int>();

         // Add some data
         queue.Enqueue(6);
         queue.Enqueue(4);
         queue.Enqueue(9);

         // Elements in a queue are stored in "first in, first out" order.
         // The queue from first to last is: 6, 4, 9

         // View the next element in the queue, without removing it.
         Console.WriteLine(queue.Peek()); // prints 6

         // Removes the first element in the queue, and returns it.
         Console.WriteLine(queue.Dequeue()); // prints 6
         Console.WriteLine(queue.Dequeue()); // prints 4
         Console.WriteLine(queue.Dequeue()); // prints 9


              Thread safe heads up! Use ConcurrentQueue in multi-thread environments.

        Read An overview of c# collections online: https://riptutorial.com/csharp/topic/2344/an-overview-
        of-csharp-collections

















        https://riptutorial.com/                                                                               44
   93   94   95   96   97   98   99   100   101   102   103