Book Image

C# Data Structures and Algorithms

By : Marcin Jamro
Book Image

C# Data Structures and Algorithms

By: Marcin Jamro

Overview of this book

Data structures allow organizing data efficiently. They are critical to various problems and their suitable implementation can provide a complete solution that acts like reusable code. In this book, you will learn how to use various data structures while developing in the C# language as well as how to implement some of the most common algorithms used with such data structures. At the beginning, you will get to know arrays, lists, dictionaries, and sets together with real-world examples of your application. Then, you will learn how to create and use stacks and queues. In the following part of the book, the more complex data structures will be introduced, namely trees and graphs, together with some algorithms for searching the shortest path in a graph. We will also discuss how to organize the code in a manageable, consistent, and extendable way. By the end of the book,you will learn how to build components that are easy to understand, debug, and use in different applications.
Table of Contents (14 chapters)

Sorted lists


In this chapter, you have already learned how to store data using arrays and lists. However, do you know that you can even use a data structure that ensures that the elements are sorted? If not, let's get to know the SortedList generic class (from the System.Collections.Generic namespace), which is a collection of key-value pairs, sorted by keys, without the necessity of sorting them on your own. It is worth mentioning that all keys must be unique and cannot be equal to null.

You can easily add an element to the collection using the Add method, and remove a specified item using the Remove method. Among other methods, it is worth noting ContainsKey and ContainsValue for checking whether the collection contains an item with a given key or value, as well as IndexOfKey and IndexOfValue for returning an index of a given key or value within the collection. As the sorted list stores the key-value pairs, you have also access to the Keys and Values properties. Particular keys and values...