The Learn Programming AcademyThe Learn Programming Academy
  • Home
  • Courses
  • Meet The Team
  • Blog
  • About Us
  • Contact
  • Home
  • Courses
  • Meet The Team
  • Blog
  • About Us
  • Contact

Programming

  • Home
  • Blog
  • Programming
  • Data Structures – Choosing the Right One

Data Structures – Choosing the Right One

  • Posted by Sarah Ettritch
  • Categories Programming
  • Date September 6, 2018
  • Comments 3 comments

Imagine if you walk into a library looking for a specific book, but the books in the library aren’t organised in any way. They aren’t in alphabetical order or shelved by genre. All you can see are piles of books that reach to the ceiling.

When you ask the librarian to get the book for you, she tells you to leave your phone number, so she can call you when she finds the book. There are more than 10,000 books in the library, and she has to search through each pile until she finds the book you want. It could take an hour, a day, a month, or even a year.

Fortunately, libraries do organize their books, and so finding a book usually doesn’t take very long.

Organizing a large number of items is critical when you want to retrieve an item in an efficient manner. In the world of software development, using a data structure is one way to organize data. Choosing the right data structure for your application is critical. It can be the difference between an application that flies, and one that crawls.

What is a Data Structure?

Wikipedia defines a data structure as “a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.”

There are three characteristics in that definition:

  1. A collection of data values
  2. The relationships between them
  3. The functions or operations that can be applied to the data.

A collection of data values

Data structures are a way to organize a collection of data values. For example, an array is a data structure. You can store more than one value in an array.

The relationships between them

An array is only one type of data structure. It differs from others in the way it organises the data (“the relationships between them”). When you use an array, the data values are stored sequentially in one contiguous block in memory.

Another type of data structure is a linked list. A linked list is also a sequential data structure, like the array, but the values aren’t stored as one contiguous block. In a singly-linked list, each node in the list contains the value, and a field that points to the next value in the list.

(from Wikipedia – public domain image)

This linked list contains 3 values: 12, 99, and 37. The value 12 is at the head of the list.

As you can see, the relationship between items in a linked list is different than the relationship between items in an array. They are distinct data structures.

The functions or operations that can be applied to the data

The third characteristic that describes a data structure is what operations you can perform on the values stored in the structure.

In an array, you can add, remove, and retrieve items using indices. For example, if you have a 10-element integer array that contains the values 10, 20, 30, 40, and 50, you can add 60 to the array by assigning it to index 5 (array indices are usually 0-based).

If you want to retrieve the value 40, you do so using array[3].

To remove an item from the array, you can set the value to 0 at the specified index, or you can shift the remaining elements down by one position.

You can do the same operations on a singly-linked list, but the way you add, remove, and retrieve items is different. Let’s look at the diagram again.

Unlike an array, you don’t have instant access to every item in the list. All you have is the reference to the first node in the list (the head reference). Because of that, when working with a singly-linked list, you always add items to the head of the list, and remove items from the head of the list.

Yes, you could add an item between values 99 and 37, but to do so, you’d have to start at the head of the list and follow the next pointers until you found 99. In a 3-element list, it wouldn’t take long, but if your list contained thousands of items, performance would be an issue. So one of the characteristics of a linked list is that they don’t allow insertions and deletions anywhere. They only allow them at the head of the list.

For example, in Java, a singly-linked list class wouldn’t contain a method that allows you to insert an item anywhere in the list.  (note that the LinkedList class in the JDK is a doubly-linked list, and so its behaviour is different.)

Why would you use a singly-linked list rather than an array? If you want stack (LIFO) behaviour, where the last item added is the first item removed, singly-linked lists are a perfect fit. That’s one example of when a linked list would be a better choice than an array.

On the other hand, if you want to be able to quickly access any item in the data structure, an array would be the better choice.

Data Structures in Java

Java’s JDK contains classes for most of the common data structures, so you don’t have to write your own. For example, the LinkedList class is an implementation of a doubly-linked list. HashMap is an implementation of the hashtable data structure.

It’s wonderful that the JDK contains classes for most of the data structures you’d consider using in your applications, but you shouldn’t use a data structure without having some understanding of how it’s organized, and how it works.

Advantages and Disadvantages

We’ve taken a very quick look at the array and singly-linked list data structures, and seen how each one could be the better choice to use in an application, depending on the behaviour we want. There are other data structures to choose from: trees, hashtables, graphs, and more.

It’s important for you to have some understanding of how each data structure works, meaning how it organises the data and what operations it allows. Armed with this knowledge, you’ll be able to choose the best data structure for your application. Doing so could be the difference between an application that performs well, and one that performs poorly because the data structure it’s using to organise data isn’t a good fit for the operations it frequently performs.

Conclusion

Data structures are a way to organise data. A data structure is defined by how it organises the data, and by the operations it allows us to perform on the data. It’s important to choose the right data structures and algorithms for your next project. You can blindly use the classes that Java and other languages provide, but understanding how each one works and choosing the right one for an application is a critical skill in software development.

  • Share:
mm
Sarah Ettritch
Sarah Ettritch has a computer science degree and over 25 years of software development experience. She has worked for large and small companies, including IBM, Quest Software, and SunLife of Canada. Sarah spent most of her career working on tools for developers—a demanding audience! She has worked on the development teams for several products, including a compiler and a Java profiler, and has extensive experience with Java, Swing, and JavaFX. She loves picking up programming languages and frameworks, and she’s constantly learning new skills. When Sarah isn’t programming or learning, she’s usually reading, writing, or playing computer games.

Previous post

SQL Vs NoSQL in 2018
September 6, 2018

Next post

3D Programming Made Easy!
September 9, 2018

You may also like

The Complete JavaScript Course for Developers
16 January, 2019

Recently a student in my The Complete JavaScript Course for Developers course asked about the capabilities and limitations of JavaScript. Specifically, can JavaScript collect and handle data from a user in a mobile environment. The answer is an unequivocal “yes” …

Java 11 has Arrived. Is it time to panic?
27 September, 2018
java11
Kotlin – Java Developers Have a Head Start
26 September, 2018
Kotlin – Java Developers Have a Head Start

    3 Comments

  1. mm
    shu
    February 7, 2021
    Reply

    “For example, in Java, a singly-linked list class wouldn’t contain a method that allows you to insert an item anywhere in the list.” Doesn’t this directly contradict with the very definition of LinkedList that it is an ordered sequence that allows efficient insertion and removal at any location?

    • mm
      Tim Buchalka
      February 10, 2021
      Reply

      A single linked list class, by its very design, isn’t designed for that. As pointed out in the article, Java’s implementation of LinkedList is a doubly-linked list and does allow you to efficiently insert and remove at any location.

      Regards,
      Tim

  2. mm
    Teknik Informatika
    July 21, 2023
    Reply

    What are the advantages and disadvantages of using an array as a data structure compared to other options?

Leave A Reply Cancel reply

Your email address will not be published. Required fields are marked *


Popular Courses

Java Masterclass 2025: 130+ Hours of Expert Lessons

Java Masterclass 2025: 130+ Hours of Expert Lessons

Learn Python Programming Masterclass

Learn Python Programming Masterclass

Beginning C++ Programming – From Beginner to Beyond

Beginning C++ Programming - From Beginner to Beyond

SQL for Beginners: Learn SQL using MySQL and Database Design

SQL for Beginners: Learn SQL using MySQL and Database Design

C Programming For Beginners – Master the C Language

C Programming For Beginners - Master the C Language

Data Structures and Algorithms:  Deep Dive Using Java

Data Structures and Algorithms: Deep Dive Using Java

  • Privacy Policy
  • Terms & Conditions

logo-eduma-the-best-lms-wordpress-theme

+61 422 512 549

[email protected]

Company

  • About Us
  • Blog
  • Contact
  • Become A Co-Instructor

Links

  • Courses
  • FAQs
  • Contact Us

logo-eduma-the-best-lms-wordpress-theme

+61 422 512 549

[email protected]

Company

  • About Us
  • Blog
  • Contact
  • Become A Co-Instructor

Links

  • Courses
  • FAQs
  • Contact Us

  • Privacy Policy
  • Terms & Conditions

Would you like to become a Udemy Course Co-Instructor?

Struggling to find students? Let us publish and promote your course to our students.

FIND OUT MORE NOW