DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • LLMops: The Future of AI Model Management
  • Transforming Customer Feedback With Automation of Summaries and Labels Using TAG and RAG
  • Vector Tutorial: Conducting Similarity Search in Enterprise Data
  • A Guide to Vector Embeddings for Product and Software Engineers

Trending

  • Microsoft Azure Synapse Analytics: Scaling Hurdles and Limitations
  • Why Database Migrations Take Months and How to Speed Them Up
  • *You* Can Shape Trend Reports: Join DZone's Software Supply Chain Security Research
  • Build Your First AI Model in Python: A Beginner's Guide (1 of 3)
  1. DZone
  2. Data Engineering
  3. Data
  4. What Are Linear Data Structures?

What Are Linear Data Structures?

We say a data structure is 'linear' if the items inside it are stored in a sequence. Arrays, linked lists, and stacks are all linear data structures.

By 
Dave Saunders user avatar
Dave Saunders
·
Mar. 20, 22 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
7.7K Views

Join the DZone community and get the full member experience.

Join For Free

Why should I care?

We use these data structures every day in programming.

Even if you're already familiar with them, it's helpful to recap them occasionally.

In 5 Minutes or Less:

As we said in the introduction, a data structure is 'linear' if the elements form a sequence.

That means that the data structure has a first and last element, and each element is connected to its previous and next element.

  • An 'array' is a linear data structure; the items are stores sequentially.
  • A 'graph' is not a linear data structure; any node can be linked to any other node in the graph - there is no fixed 'sequence'.

(if you're not familiar with graphs, don't worry - there's a newsletter coming up that explores them in detail).

linear data structures represented as a series of boxes. Non-linear structures look like a 'graph'

Let's take a look at some common linear data structures...

Arrays

If you've done any programming, you're almost certainly familiar with the concept of arrays.

An array is like a bookcase; the items are stored next to each other, but we can jump to any item we like to read it.

The items in the array have an 'index' that allows us to reference them directly.

image on an array with some elements in

The ability to jump to any item we like to read its value is called 'random access', and is a huge advantage of an array.

We take this for granted, but this is not a property that many other 'linear data structures' have, as you'll see below.

When we allocate an array, we have to determine up-front how much space we need.

If we fill our array, we have to stop and allocate some more space. That means that while normal inserts into the array are very fast, occasionally we have to pause for a short time to make the array bigger - which takes some time.

Linked Lists

A linked list is a data structure where each item points to the next. We can't jump to any element directly like we can with an array. Instead, we have to access them in turn:

an image of a linked list

Linked lists are useful because, unlike an array, we don't need to decide up-front how much space we need. If we need to add a new item, we simply add it to the end.

That means that adding the 200th item has the same cost as adding the 2nd item. This predictable performance is an advantage of a linked list.

It's also easy to add or remove an item from the middle of the linked list, by simply changing some 'next' pointers.

Here's how we'd remove an item from a linked list:

removing an item from a linked list

This would be more difficult to do in an array, as we'd have to shift all of the remaining items to account for the new or removed item.

A variant of the linked list is the 'doubly-linked list', where each element not only points to the next element, but also the previous one.

This means we can traverse the data structure in either order, but still have the advantages of a linked list.

an image of a doubly-linked list


Queues

The queue is a 'First In First Out' (FIFO) data structure. That means that items are read in the same order that they are inserted.

This is just like the queue at the store, the first person to join the queue is the first person to be served.

an image of a queue

A printer queue is a good example of this data structure in use. The printer will print items in the order in which they were queued. If you send your document to the printer last, it will be the last thing to be printed.

Queues are also useful as 'buffers'.

Suppose we have two separate systems, one that reads messages and one that processes them. We don't want the system that reads messages to have to wait for each message to be processed before listening for another.

Putting a queue between them allows us to 'de-couple' those systems. The read process can keep adding items to the queue, safe in the knowledge that the processing side will pull the items off of the queue and process them eventually - no waiting required.

Stacks

The stack is a 'Last In First Out' data structure; the last item to be added is the first item to be read.

You can think of this like a stack of plates, the last plate to be added to the pile is the first plate we'd take off again:

an image of a stack

You might use a stack to implement 'undo' functionality, for example. The last task the user performed is the first one to be reversed when they click the 'undo' button.

The 'call stack' we see every day when running code is also a great example of a stack, but that's a topic for a future newsletter!

Want to Know More?

Check out these links:

  • A list of linear data structures from Wikipedia
  • A more in-depth article on queues
  • An introduction to the call stack (this article is specifically about JavaScript, but is applicable to most languages)
Data (computing) Data structure

Published at DZone with permission of Dave Saunders. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • LLMops: The Future of AI Model Management
  • Transforming Customer Feedback With Automation of Summaries and Labels Using TAG and RAG
  • Vector Tutorial: Conducting Similarity Search in Enterprise Data
  • A Guide to Vector Embeddings for Product and Software Engineers

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!