Book Image

Everyday data structures

By : Smith
Book Image

Everyday data structures

By: 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 (14 chapters)
Free Chapter
1
1. Data Types: Foundational Structures

Revisiting users logged in to a service


In Chapter 2, Arrays: Foundational Structures, we created an app to keep track of users logged into a web service, using an array as the underlying data structure containing the User objects. However, this design can be greatly improved upon by using a list data structure. Let's revisit the users logged into a service problem here, and by replacing the class array with a list, we will see that our original code is both abbreviated and more readable in most cases.

C#

In this example, we have replaced the User[] object with a List<User> object. Much of this refactor is obvious, but three lines of code should be noted. First, in the CanAddUser() method, we have replaced 15 lines of code with 2 lines of code by leveraging the List<T>.Contains() method and condensing our logic loop. Next, in the UserAuthenticated() method, we have leveraged the List<T>.Add() method, which replaced the call to Array.Resize() and the error-prone approach to...