Sonzaschool
Rudi

Sekondari ya Juu · Kidato cha Sita

Sayansi ya Kompyuta

Explore and utilise basic data structure (linked lists, stacks, queues and trees.)

takriban dakika 8 kusoma

Mada za sehemu hiiDemonstrate mastery of basic principles of Algorithms and Data structuresMada 7

Exploring and Utilising Basic Data Structures

A data structure is a method of storing and organising data in a computer so that it can be used efficiently. While static data structures like arrays have a fixed size, many real-world applications require data structures that can grow and shrink during program execution. This leads us to dynamic data structures: linked lists, stacks, queues, and trees. These structures provide flexibility in managing data whose size is not known in advance.

A singly linked list: nodes with data and a pointer to the next node, ending in NULL

Meaning and Structure

A linked list is a linear data structure in which elements (called nodes) are stored in non-contiguous memory locations. Each node contains two parts: the data (the actual information) and the pointer (the link to the next node). The pointer in each node allows the program to move from one node to the next, forming a chain. The last node contains a null pointer, indicating the end of the list.

The key terms used in linked lists are:

  • Link: Each element of a linked list that stores data
  • Next: A pointer that contains the address of the following link
  • First: A pointer to the first node (also called Head)
  • Tail: The last node in the list
Data   | Pointer |     Data   | Pointer |     Data   | Pointer
  10   | ----→   |     20    | ----→   |     30    |  NULL

Types of Linked Lists

There are three main types of linked lists:

  1. Singly Linked List: Traversed in one direction from head to tail
  2. Doubly Linked List: Traversed in both directions (forward and backward)
  3. Circular Linked List: The last node points back to the first node

Operations on Linked Lists

Creation: Creates a linked list with one node. Once created, insertion operations can add more elements.

Insertion: A new node can be inserted:

  • At the beginning of the linked list
  • At the end of the linked list
  • At any specified position in between

When inserting at the beginning, the new node's pointer links to the current head, and the new node becomes the new head. No elements need to be shifted—only pointers are updated.

Deletion: A node can be deleted from:

  • The beginning of the linked list
  • The end of the linked list
  • A specified location

Traversal: Going through all nodes from one end to another. In a singly linked list, you can only traverse forward. In a doubly linked list, forward and backward traversing is possible.

Concatenation: Appending the second list to the end of the first list.

Advantages and Disadvantages

Advantages:

  • Easy insertion and deletion (takes constant time, no need to shift elements)
  • No memory wastage—size increases and decreases during runtime
  • Can expand constantly without copying and reallocating
  • Easy to implement stacks and queues

Disadvantages:

  • Uses more memory than arrays (each node stores data plus a pointer)
  • Traversal is not easy—you cannot randomly access elements like in arrays
  • Reverse traversing is difficult

Real-world Application

A real example of a linked list is the "forward" and "backward" buttons in a web browser. A doubly linked list keeps track of visited pages, allowing you to move back to previous pages and forward again.


Swali

What is the correct order of node visitation for preorder traversal in a binary tree?

Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.

Ingia ili kufanya mazoezi

Mwalimu

Umekwama? Niulize chochote kuhusu mada hii.

Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.

Ingia ili kuuliza