πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English

Data Structures

Fundamental data structures in C: linked list, stack, and queue.

πŸ“– What to learn on this page
βœ… Must-know essentials
  • Linked list: each node points to the next
  • Stack: last-in first-out (LIFO)
  • Queue: first-in first-out (FIFO)
⭐ Read if you have time
  • Trees and hash tables
  • Complexity: O(n), O(1)
  • Comparison with STL / standard libraries

What is a Data Structure? β€” Designing How Data is Arranged

A data structure defines how data is laid out in memory and how you work with it. Arrays are a data structure themselves, but plenty of situations make arrays alone awkward to use.

Limitations of arrays

Slow middle/front insertion
Inserting near the front forces every later element to shift. With a million items, that's a million shifts.
Fixed size
The size is locked in at declaration time, which makes data that grows and shrinks awkward to handle (malloc helps, but it's extra work).

Common data structures

StructureCharacteristicExample use
Linked listNodes linked by pointers; fast insertion/deletionOrdered data with frequent insert/remove
StackLast-in, first-out (LIFO)Function calls, Undo history
QueueFirst-in, first-out (FIFO)Print queue, task scheduling
All of these are built from structs, pointers, and malloc. This lesson brings together everything we've learned so far!

Linked List β€” A Chain Connected by Pointers

A linked list is a sequence where each element (a node) holds some data plus a pointer to the next node.

Node definition

typedef struct Node {
  int data;              // the stored data
  struct Node *next;     // pointer to the next node
} Node;

Picture of memory

10
next→
β†’
20
next→
β†’
30
next→
β†’
NULL

Key operations

// (1) Create a node
Node* createNode(int val) {
  Node *n = malloc(sizeof(Node));
  n->data = val;
  n->next = NULL;
  return n;
}

// (2) Insert at the front (O(1) β€” fast!)
void pushFront(Node **head, int val) {
  Node *n = createNode(val);
  n->next = *head;     // new node's next = old head
  *head = n;           // head now points to new node
}

// (3) Print all elements
void printList(Node *head) {
  for (Node *p = head; p != NULL; p = p->next)
    printf("%d β†’ ", p->data);
  printf("NULL\n");
}

// (4) Free the memory (forgetting leaks!)
void freeList(Node *head) {
  while (head) {
    Node *tmp = head->next;
    free(head);
    head = tmp;
  }
}
Front insertion
O(1) β€” very fast
Search
O(n) β€” walk from the head
Vs. arrays
No indexed access; random access isn't its strength.

Stack β€” Last In, First Out (LIFO)

A stack pops the most recently pushed item first. Think of stacking plates. The call stack that tracks function calls works the same way.
Stack visualization
10
20
30
← TOP (items come off here)
// push: add to the top
void push(Node **top, int val) {
  Node *n = createNode(val);
  n->next = *top;
  *top = n;
}

// pop: remove from the top
int pop(Node **top) {
  if (*top == NULL) return -1;
  Node *tmp = *top;
  int val = tmp->data;
  *top = tmp->next;
  free(tmp);
  return val;
}
Everyday examples: the browser Back button, Ctrl+Z (Undo), and the call stack that tracks function calls β€” all of these use a stack under the hood.

Queue β€” First In, First Out (FIFO)

A queue removes items in the same order they went in β€” just like a checkout line.
Queue visualization
Out ←
10
20
30
β†’ In
// enqueue: add to the tail
void enqueue(Node **head, Node **tail, int val) {
  Node *n = createNode(val);
  if (*tail) (*tail)->next = n;
  else *head = n;
  *tail = n;
}

// dequeue: remove from the head
int dequeue(Node **head, Node **tail) {
  if (!*head) return -1;
  Node *tmp = *head;
  int val = tmp->data;
  *head = tmp->next;
  if (!*head) *tail = NULL;
  free(tmp);
  return val;
}
Everyday examples: printer queues, OS task schedulers, and chat message ordering.

Array vs Linked List vs Stack vs Queue

OperationArrayLinked listStackQueue
Insert at frontO(n)O(1)β€”β€”
Append at backO(1)O(1)*O(1)O(1)
Random accessO(1)O(n)β€”β€”
Insert in middleO(n)O(1)†——
* with a tail pointer. † when the insertion point is known.

Linked List Demo

Use the buttons to add and remove elements from a linked list, and watch the structure update in real time.
List state:
(empty list)
Press a button to manipulate the list.

Related lessons

Advanced
malloc / free
Dynamic allocation and how to avoid memory leaks.
Advanced
Pointer Basics
Understand pointers with memory visualization.
Algorithms
Sorting Algorithms
Bubble sort, selection sort, and insertion sort, animated.
← Previous lesson
Lesson 30: Dynamic Memory (malloc/free)
Next lesson →
Lesson 32: Sorting Algorithms

Review Quiz

Check your understanding of this lesson.

Q1. What is an advantage of a linked list?

Fast random access
Efficient insertion and deletion
Uses less memory

Insertion and deletion in a linked list just rewire pointers. Arrays have to shift elements around, which is slow.

Q2. In what order does a stack return items?

FIFO (first in, first out)
LIFO (last in, first out)
Random

A stack is LIFO (Last In, First Out) β€” the most recently pushed item is the first one you pop off.

Q3. In what order does a queue return items?

FIFO (first in, first out)
LIFO (last in, first out)
Priority order

A queue is FIFO (First In, First Out) β€” just like a line at a checkout.

Share this article
Share on X Share on Facebook