Arrays & Linked Lists Data Structures Explained with Examples

Zaheer Ahmad 5 min read min read
Python
Arrays & Linked Lists Data Structures Explained with Examples

Introduction

Arrays & Linked Lists are two of the most fundamental data structures in programming. If you are starting your journey in Data Structures & Algorithms (DSA), understanding these concepts is essential. In this tutorial, “Arrays & Linked Lists: Data Structures Explained with Examples”, we will break down both structures in a beginner-friendly way with practical examples.

In simple terms:

  • An array stores elements in a fixed, continuous block of memory.
  • A linked list stores elements as separate nodes connected using pointers.

Why should Pakistani students learn this?

Whether you're studying in Lahore, Karachi, or Islamabad, mastering these structures will help you:

  • Crack university exams
  • Prepare for coding interviews
  • Build efficient real-world applications like student systems or shopping apps

Prerequisites

Before starting this tutorial, you should have:

  • Basic understanding of programming (Python, C++, or Java)
  • Knowledge of variables and loops
  • Familiarity with functions
  • Basic idea of memory (helpful but not mandatory)

If you’re new, consider starting with a beginner-friendly DSA tutorial on theiqra.edu.pk.


Core Concepts & Explanation

Arrays in Programming — Fixed and Fast Access

An array is a collection of elements stored in contiguous memory locations.

Example:

If Ahmad stores marks of 5 students:

marks = [85, 90, 78, 92, 88]

Each element has an index:

  • marks[0] = 85
  • marks[1] = 90

Key Features:

  • Fixed size (in most languages)
  • Fast access using index (O(1))
  • Easy to use

Real-Life Analogy:

Think of an array like seats in a classroom. Each seat has a fixed position and number.


Linked List Tutorial — Flexible and Dynamic Storage

A linked list is a collection of nodes where each node contains:

  • Data
  • Pointer to the next node

Example:

Fatima stores a list of tasks:

Task1 → Task2 → Task3 → NULL

Each task points to the next.

Key Features:

  • Dynamic size
  • Efficient insertion/deletion
  • No contiguous memory required

Real-Life Analogy:

Think of a train where each compartment is connected to the next.


Array vs Linked List — Key Differences

FeatureArrayLinked List
MemoryContiguousNon-contiguous
SizeFixedDynamic
Access SpeedFast (O(1))Slow (O(n))
Insert/DeleteSlowFast

Dynamic Arrays in Modern Languages

Languages like Python, Java, and C++ provide dynamic arrays:

  • Python: list
  • Java: ArrayList
  • C++: vector

Practical Code Examples

Example 1: Array Operations in Python

# Create an array
marks = [85, 90, 78]

# Access element
print(marks[1])

# Add element
marks.append(95)

# Remove element
marks.remove(78)

# Print array
print(marks)

Line-by-Line Explanation:

  • marks = [85, 90, 78]
    Creates an array storing student marks.
  • print(marks[1])
    Accesses second element (index starts from 0).
  • marks.append(95)
    Adds new mark at the end.
  • marks.remove(78)
    Removes the value 78 from the array.
  • print(marks)
    Displays updated array.

Example 2: Real-World Application — Student Fee System

Let’s simulate a simple system where Ali tracks student fees.

# List of student fees in PKR
fees = [5000, 7000, 6500]

# Add new student fee
fees.append(8000)

# Calculate total fees
total = sum(fees)

print("Total Fees Collected:", total)

Line-by-Line Explanation:

  • fees = [5000, 7000, 6500]
    Stores fees of students.
  • fees.append(8000)
    Adds a new student fee.
  • total = sum(fees)
    Calculates total collected fees.
  • print(...)
    Displays total amount in PKR.

Example 3: Simple Linked List in Python

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

# Create nodes
node1 = Node(10)
node2 = Node(20)

# Link nodes
node1.next = node2

# Print values
print(node1.data)
print(node1.next.data)

Line-by-Line Explanation:

  • class Node:
    Defines a node structure.
  • self.data = data
    Stores value.
  • self.next = None
    Points to next node (initially none).
  • node1.next = node2
    Links first node to second.
  • print(...)
    Displays values.


Common Mistakes & How to Avoid Them

Mistake 1: Index Out of Range in Arrays

Problem:

marks = [85, 90, 78]
print(marks[5])  # Error

Why it happens:

Index 5 does not exist.

Fix:

if len(marks) > 5:
    print(marks[5])

Mistake 2: Losing Reference in Linked List

Problem:

head = Node(10)
head = Node(20)  # Lost first node

Why it happens:

Original node is overwritten.

Fix:

new_node = Node(20)
new_node.next = head
head = new_node


Practice Exercises

Exercise 1: Find Maximum Value in Array

Problem:

Given an array of marks, find the highest score.

Solution:

marks = [70, 85, 90, 60]
max_value = max(marks)
print(max_value)

Explanation:

  • max() finds the largest value in the array.

Exercise 2: Insert Node at Beginning

Problem:

Insert a node at the start of a linked list.

Solution:

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

head = Node(10)

new_node = Node(5)
new_node.next = head
head = new_node

print(head.data)

Explanation:

  • New node points to old head
  • Head updated to new node

Frequently Asked Questions

What is an array in programming?

An array is a collection of elements stored in contiguous memory locations. It allows fast access using indexes, making it efficient for reading data.

What is a linked list?

A linked list is a collection of nodes where each node contains data and a pointer to the next node. It is useful for dynamic memory allocation.

Which is better: array vs linked list?

Arrays are better for fast access, while linked lists are better for frequent insertions and deletions. The choice depends on your use case.

How do I choose between array and linked list?

If you need quick access by index, use arrays. If you need flexibility in size and frequent updates, use linked lists.

Are arrays used in real-world applications?

Yes, arrays are used in many applications like storing student records, managing inventory systems, and handling data in apps.


Summary & Key Takeaways

  • Arrays store elements in contiguous memory and allow fast access.
  • Linked lists use nodes connected via pointers and allow flexible size.
  • Arrays are easier for beginners but less flexible.
  • Linked lists are powerful for dynamic data handling.
  • Understanding both is essential for mastering DSA.
  • Choosing the right structure improves program efficiency.

Now that you understand arrays in programming and linked lists, continue your learning with:

  • Learn the basics in our DSA Tutorial for Beginners
  • Understand Stacks & Queues for better data handling
  • Explore Sorting Algorithms to organize data efficiently
  • Dive into Recursion for advanced problem-solving

Keep practicing, and remember — every expert programmer in Pakistan once started as a beginner just like you! 🚀

Practice the code examples from this tutorial
Open Compiler
Share this tutorial:

Test Your Python Knowledge!

Finished reading? Take a quick quiz to see how much you've learned from this tutorial.

Start Python Quiz

About Zaheer Ahmad