Book Image

Everyday Data Structures

By : William Smith
Book Image

Everyday Data Structures

By: William Smith

Overview of this book

Explore a new world of data structures and their applications easily with this data structures book. Written by software expert William Smith, you?ll learn how to master basic and advanced data structure concepts. ? Fully understand data structures using Java, C and other common languages ? Work through practical examples and learn real-world applications ? Get to grips with data structure problem solving using case studies
Table of Contents (20 chapters)
Everyday Data Structures
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Free Chapter
1
Data Types: Foundational Structures

Initializing sets


Sets are not terribly commonplace in development, but each of the languages we are examining supports data structures with some form of concrete implementations. Here are some examples of initializing a set, adding a few values to the collection including one duplicate, and printing the set's count to the console after each step.

C#

C# provides a concrete implementation of the set data structure through the HashSet<T> class. Since this class is generic, the caller may define the type used for elements. For example, the following example initializes a new set where the elements will be string types:

    HashSet<string, int> mySet = new HashSet<string>(); 
    mySet.Add("green");  
    Console.WriteLine("{0}", mySet.Count); 
    mySet.Add("yellow");  
    Console.WriteLine("{0}", mySet.Count); 
    mySet.Add("red");  
    Console.WriteLine("{0}", mySet.Count); 
    mySet.Add("red");  
    Console.WriteLine("{0}", mySet...