Data Structures II
Data Structures II
2
Data structures continue to play a crucial role in computer science, enabling efficient
data management and manipulation. Building upon the foundation laid in Part 1, this
module delves further into advanced data structures such as stacks, queues and
linked lists.
● Discover the functionality and structure of lists and arrays, and how to
manipulate them in different programming environments.
● Gain insights into linked lists, understanding how to build and operate
them, including insertion, deletion, and traversal.
● Learn about the structure and functionality of stacks, queues, and binary
trees, and how they can be utilised in programming.
● Discover the principles of graphs, sets, and hash tables, and understand
how to manipulate them in various programming environments.
● Design and manipulate user-defined data structures, integrating them into
real-world applications.
● Create an array and perform basic operations like insertion and deletion
● Implement stacks and queues using C++.
● Build a singly linked list and perform operations like insertion, deletion, and
traversal.
The material in this module relates to the following subject learning outcomes:
SLO 2: Gain the ability to create and modify basic data structures such as
arrays and linked lists, along with performing operations like insertion,
deletion, and traversal.
Student Expectations
During this module, you are expected to spend time working through the prescribed
material prior to attending class. This will enable a more valuable discussion to take
place in online tutorials or conferences, where you will be able to discuss concepts,
present ideas and explore information collaboratively in greater depth.
4 hours for facilitated study. This includes guided facilitated study, watching or
attending course lectures (live or pre-recorded), responding to facilitator feedback.
8 hours for personal study. This includes reading through course material,
undertaking learning activities and exploring additional related resources.
1. Lists can be either static or dynamic. Static lists have a fixed size, while
dynamic lists (dynamic arrays) can grow or shrink at runtime.
2. Operations like insertion and deletion are fundamental to understanding how
lists and arrays work.
3. Lists and arrays are used in various applications such as managing
databases, building temporary data storage, and data retrieval.
Learner Guide
The Learner Guide provides the core knowledge for this topic. Work through the
materials and be prepared to discuss these in your forums, tutorials or classes.
Expand All
Panels
Collapse All
Panels
Understanding Lists
● Dynamic Size
○ A list is akin to a magic bag that adjusts its size to
accommodate or remove items as necessary, showcasing a
dynamic nature.
○ Example: Imagine creating a to-do list where you can
continually add new tasks and remove completed ones without
any restriction.
● Ordered Collection
○ Lists are known to maintain a systematic order, ensuring every
item is accessible based on its position or rank in the list.
○ Example: Consider a playlist where songs are arranged in a
specific order, allowing you to find and play any song based on
its position in the list.
Understanding Arrays
Arrays stand as another pivotal data structure, designed to hold more than one value
at a time. They can be visualised as a collection of variables, each identified with a
unique index number, facilitating rapid access. The primary attributes of arrays
include:
● Fixed Size
Contrary to lists, arrays in C++ have a fixed size, which is determined at
the time of creation and cannot be changed thereafter. It's a limitation that
requires the size to be known at compile time. However, you can use
vectors in C++ if you need a dynamic size similar to lists.
● Index-Based Access
In C++ arrays, each element is identified by a unique index number,
allowing swift and direct access to any element based on its position in the
array.
Further Reading
Reference: Abhishek Sharma 2022, 'Data Structures in C++ | Array | Linked List | Stack', PrepBytes
Blog, accessed 30 January 2024,
<https://www.prepbytes.com/blog/arrays/data-structures-in-c-array-linked-list-stack/>
source: BoardInfinity
When navigating lists and arrays, the selection of the right data structure based on
your specific requirements is imperative. But how do you decide on the right one?
Let's look at strategies that can guide you to use these structures most effectively:
source: freepik
Links to an external site.
Background
Problem Statement
In the initial phase, the management identified a few pressing issues with the current
inventory system:
Implementation
● Individual Product Details with Lists: On the other hand, lists were
employed to manage individual product details within each category. Lists,
with their dynamic size, allowed for seamless addition or removal of
products, offering flexibility in managing the ever-changing inventory
dynamics.
● Training and Deployment: Before full-fledged deployment, staff were
trained to adapt to the new system, followed by a phased rollout to ensure
smooth transition without hindering the daily operations.
Outcome
Learner Guide
The Learner Guide provides the core knowledge for this topic. Work through the
materials and be prepared to discuss these in your forums, tutorials or classes.
Expand All
Panels
Collapse All
Panels
Stacks
● Push: This operation adds an item to the stack, expanding its size. It
places a new item on top of the stack, ready to be the first to be removed
when the time comes.
● Pop: This function removes the top item from the stack, reducing its size.
It facilitates the retrieval and removal of the most recently added item.
● Peek: A utility function that permits viewing of the top item without its
removal, providing a way to know the item at the top without altering the
stack structure.
Learn more about Stack operations
●
Practical Applications
Queues
A queue is analogous to a line of people at a bank, adhering to a First-In-First-Out
(FIFO) principle. This system ensures that the first item added is the first to be
removed, maintaining an order based on the addition time of the elements.
● Enqueue: This method adds an item to the rear of the queue, ensuring the
maintenance of the FIFO structure.
● Dequeue: This method removes an item from the front of the queue,
abiding by the FIFO rule.
● Front: This function allows the viewing of the front item without its
removal, providing insight into the next item to be dequeued without
altering the queue structure.
Learn more about Queue operations
Practical Applications
Background
In an attempt to modernise its public transportation system and enhance the daily
commuting experience of its citizens, the MetroCity local government initiated a
project to integrate technology-driven solutions.
They identified that the incorporation of specific data structures — stacks, queues,
and trees — could play a pivotal role in addressing current inefficiencies and offering
an improved, streamlined service.
Problem Statement
MetroCity was grappling with a congested and disorganised public transport network.
Commuters were often met with irregular bus services, unclear transit routes, and
overcrowded vehicles. Moreover, the existing system lacked a proper method to
gather and utilise data for planning and optimisation. MetroCity needed a
transformative solution to organise and streamline its transport services.
Implementation
To improve the system, MetroCity introduced a digital platform that utilised the
principles of stacks, queues, and trees data structures in the following manner:
Outcome
1. Linked lists can be singly linked or doubly linked. Singly linked lists contain
pointers to the next node, while doubly linked lists contain pointers to both the
next and previous nodes.
2. Linked lists are dynamic data structures, which means they can easily grow or
shrink during execution.
3. Operations like insertion, deletion, and traversal require an understanding of
pointers and the interconnected nodes to be efficient.
Learner Guide
The Learner Guide provides the core knowledge for this topic. Work through the
materials and be prepared to discuss these in your forums, tutorials or classes.
Expand All
Panels
Collapse All
Panels
source: freepik
In the realm of data structures, a linked list holds a significant place. It is essentially a
linear collection of data elements or nodes, wherein each element points to the next
one, forming a sequence or link.
There are different variations of linked lists, each catering to different needs:
● Singly Linked List: Contains nodes which have a data part and a link to
the next node in the sequence.
● Doubly Linked List: Each node contains, besides the next-node link, a
second link field pointing to the ‘previous’ node in the sequence, which
facilitates traversal in both directions.
● Circular Linked List: The last node in the list points back to the first node,
forming a closed loop.
Further Reading
Understanding the Basics of Linked List
Explore the purpose and structure of linked list with visual representations of the
different types.
Reference: Geeks for Geeks n.d., Understanding the Basics of Linked List, Geeks for Geeks,
accessed 30 January 2024, <https://www.geeksforgeeks.org/what-is-linked-list/>
Memory Allocation
Unlike arrays, linked lists do not allocate a contiguous memory block; instead,
memory is assigned as and when a new node is added. This dynamic memory
allocation can prevent wastage of memory space and offer more flexibility in data
handling.
Insertion and Deletion
In linked lists, the insertion and deletion of nodes are more straightforward
processes. You are not shifting the elements but merely changing the links, which
can be done in constant time, thereby saving computational resources.
Real-World Applications
Linked lists find their application in various real-world scenarios. Here are a few
examples:
source: freepik
Links to an external site.
Background
In the ever-evolving digital landscape, social media platforms stand as bustling hubs
of interaction and content exchange. Imagine a particular platform, let’s call it
‘SocialSphere’, that has witnessed an exponential growth in its user base in recent
years. Initially, the user feed mechanism was based on an array data structure.
However, as the platform expanded, it became increasingly apparent that the
array-based structure was struggling to keep up with the dynamic nature of user
feeds, leading to slower updates and a less responsive user interface.
Preliminary Challenges
Implementation
To overcome these challenges, the development team at SocialSphere embarked on
rethinking their data management strategy. They realised that a doubly linked list
would serve as an ideal solution, as it could handle dynamic data more efficiently.
Here’s how they approached the revamping process:
Before the fully-fledged deployment, rigorous testing was carried out to ensure the
efficiency and reliability of the new system. The development team created
simulations to test the performance of the new structure under different loads and
conditions.
Outcome
Expand All
Panels
Collapse All
Panels
Reading: Linked List Data Structure In C++
A linked list is a linear dynamic data structure to store data items. This article gives
further explanation of this data structure and demonstrates various operations that
can be performed on linked list with code examples.
Reference: (2023) 'Linked List Data Structure In C++', Software Testing Help, accessed 30 January
2024, <https://www.softwaretestinghelp.com/linked-list/>
Reference: Sharma, A. (2022) 'Data Structures in C++ | Array | Linked List | Stack', PrepBytes Blog,
accessed 16 Sept. 2024,
<https://www.prepbytes.com/blog/arrays/data-structures-in-c-array-linked-list-stack/>