Data Structures and Algorithms in C#
Data Structures, Algorithms, and Linked Lists
Organizing Data to Solve Problems
A data structure is a way of organizing and storing data so that a computer can access and modify it effectively. The best structure depends on what a program needs to do. A structure that works well for reading items in order may not be the best choice for representing relationships or retrieving items according to a special rule.
For example, a linked list suits sequential data: each item keeps a reference to the next item. A tree or graph is more suitable for representing relationships such as an organization chart, where one person or position can connect to other positions. A queue suits situations where items should be retrieved in the same order they were added. This behavior is called first in, first out, or FIFO.
The general idea is that data structures are chosen to match the problem. A structure can make particular operations—such as reading, adding, or removing data—more straightforward or efficient.
Abstract thinking and ADTs
There are many data structure implementations, including multiple kinds of arrays and linked lists. Learning each implementation as an isolated case can make it difficult to choose a suitable structure for a new problem. Abstraction helps by focusing on the general behavior shared by related structures rather than on every implementation detail.
An abstract data type, or ADT, describes a category of data structure through the operations it supports, without requiring one particular internal implementation. For example, a linked-list ADT might be described in terms of operations such as adding, removing, sorting, and updating items. A simple, doubly linked, or circular linked list can differ internally while still belonging to the linked-list family.
This is similar to recognizing different kinds of trees as trees: their details may vary, but the shared concept makes them easier to understand and compare. Thinking in ADTs helps connect a problem to an appropriate family of structures before deciding how to implement it.
An ADT describes what a data structure does; an implementation describes how it does it.
Think About It: If a program must display a sequence of items in the order they were added, which structure from this lesson seems like a natural fit, and why?
Algorithms: Clear Procedures for Solving Problems
An algorithm is a step-by-step procedure for solving a problem. The steps must be clear enough that carrying them out produces the intended result. For example, adding two numbers can be described simply: take the first number, take the second number, and add them.
Not every piece of program code is automatically an algorithm. A program may contain insert, update, or delete operations, but naming an operation does not by itself explain the procedure it follows. To assess a procedure as an algorithm, the lesson emphasizes five characteristics:
- Well-defined input: The information the procedure receives is clear.
- Well-defined output: The result it produces is clear.
- Precise steps: Each instruction is unambiguous and to the point.
- Finiteness: The procedure starts and eventually ends.
- Step-to-step continuity: The result of a step can be used as input to a later step, so the procedure progresses coherently.
For adding two numbers, the inputs are the two numbers, the output is their sum, and the steps specify what to do. The procedure ends after the addition. These characteristics help distinguish a defined problem-solving method from code whose behavior or purpose is unclear.
Big O Notation and Growth in Work
An algorithm has inputs, processing, and outputs. Big O notation describes how the amount of work required by the processing grows as the input size increases. It is a way to reason about performance as data becomes larger, rather than a promise of a specific number of seconds.
The input size can represent records, bytes, bits, or another measure of data. Similarly, processing time might be measured in seconds, minutes, or another unit. Big O focuses on the relationship between input size and work, not on which units are used.
For example, suppose an algorithm takes about 27 seconds for 5 records and about 105 seconds for 10 records. Those figures are consistent with a pattern that grows roughly like the square of the input size. The growth can be represented as O(n2), where n is the input size.
Common growth patterns
- Constant time, O(1): The work stays about the same as the input grows. For example, an operation that retrieves the first item without examining the rest of a collection has constant growth.
- Linear time, O(n): The work grows in proportion to the input size. A search that checks items one by one may need to inspect more items as the collection grows.
- Logarithmic time, O(logn): The work grows slowly compared with the input size. In the lesson’s performance comparison, increases in data eventually add only a relatively small amount of processing time.
- Quadratic time, O(n2): The work can grow with the square of the input size. A nested loop that processes the full collection for each item can produce this kind of growth.
- Exponential time, O(2n): The work grows very rapidly as input size increases, making large inputs especially costly.
A detail worth noticing is that a double loop does not automatically mean the work is exactly “twice the input.” If the inner loop examines the full collection for every item in the outer loop, the number of operations can grow like n2. The precise structure of the loops determines the growth pattern.
Big O also helps compare expressions that differ only by a constant factor. For example, O(2n) is classified as linear growth, O(n), because both grow in proportion to n. The expression O(2n) is different: it represents exponential growth.
A simplified performance picture places constant and logarithmic growth in favorable zones, linear growth in a zone that deserves attention as data increases, and quadratic or exponential growth in costly zones. This is a guide to how work scales, not a guarantee about a program’s actual speed. Real performance can also be affected by factors beyond the growth category.
Think About It: A method checks every item in a list once. If the list doubles in size, how does its amount of work change compared with a method that checks the full list once for every item?
Linked Lists: A Sequential Structure Made of Nodes
A linked list is a family of structures that represents data in a linear, sequential way. Its basic building block is a node (also called an element). A node contains data and a reference, or pointer, that connects it to another node.
In a simple linked list, the first node points to the second, the second points to the third, and the chain continues. The nodes do not need to be treated as one uninterrupted block: their references establish the sequence. The list can be followed by starting at its first node and moving along the links.
Three common linked-list forms
- Singly linked list: Each node points to the next node. The list is traversed forward through these links.
- Doubly linked list: Each node points to both the next node and the previous node. This makes it possible to traverse in either direction.
- Circular linked list: The final node points back to the first node instead of ending the chain. A circular list can be singly linked or doubly linked.
The form affects how the chain can be traversed. A singly linked list provides a forward path; a doubly linked list adds a backward path; and a circular list connects the end back to the beginning.
Head and tail
A linked-list class needs a way to find the chain. The head is the first node. Starting at the head, the program can follow each node’s reference to reach later nodes.
The tail is the final node. It is useful when adding an item at the end: if the list tracks its tail, the new node can be connected there without first searching through every node to find the last one. In a non-circular list, the last node has no next node.
When a list is empty, it has no first or last node. When the first item is added, that node is both the head and the tail.
Planning a C# Linked List
A simple implementation can be planned around two classes:
- A node class holds the data and a reference to the next node.
- A linked-list class manages the nodes and the connections between them.
The list class can provide operations such as AddLast(), AddFirst(), and ReadAll(). The first adds after the tail, the second adds before the head, and the third traverses the list to read its items.
The node’s next reference can initially be null, meaning it does not yet point to another node. The list itself needs a reference to its head so that it can reach the chain. Tracking a tail as well makes adding at the end direct.
A generic C# implementation
A first design might store node data as object, which allows different kinds of values but does not preserve a specific data type. A generic design uses a type parameter such as T, so a list can be created for a chosen type—such as strings or integers—while using the same implementation.
using System;
public class SimpleLinkedList<T>
{
private class Node
{
public T Data { get; }
public Node Next { get; set; }
public Node(T data)
{
Data = data;
Next = null;
}
}
private Node head;
private Node tail;
public void AddLast(T data)
{
Node newNode = new Node(data);
if (head == null)
{
head = newNode;
tail = newNode;
return;
}
tail.Next = newNode;
tail = newNode;
}
public void AddFirst(T data)
{
Node newNode = new Node(data);
if (head == null)
{
head = newNode;
tail = newNode;
return;
}
newNode.Next = head;
head = newNode;
}
public void ReadAll()
{
Node current = head;
while (current != null)
{
Console.WriteLine(current.Data);
current = current.Next;
}
}
}
The key idea in AddLast() is that the list first handles the empty case. If there is no head, the new node becomes both head and tail. Otherwise, the current tail’s Next reference is set to the new node, and the tail is updated to that node.
AddFirst() also handles an empty list by making the new node both head and tail. For a non-empty list, the new node points to the old head, and then becomes the new head.
ReadAll() starts at the head and repeatedly follows Next. It stops when the current reference is null, which marks the end of a non-circular list. This traversal shows how the head provides access to the rest of the chain.
Because the list stores the tail, both adding at the beginning and adding at the end take constant time, O(1), in this implementation. Reading every item takes linear time, O(n), because each node must be visited. This connects the structure’s design to the performance ideas described by Big O notation.
Learning through implementation
A useful way to study data structures and algorithms is to alternate between them: learn a structure, then examine algorithms that use or operate on it, and continue with another structure and its related procedures. This keeps the study balanced between organizing data and reasoning about the steps performed on it.
Trying to implement an operation independently is also valuable. An imperfect first attempt can reveal questions that reading code alone may not raise: how an empty list behaves, which reference must change when a node is added, and how traversal knows when to stop. Working through those cases helps turn the abstract idea of a linked list into a clear mental model.
Think About It: When a node is added to the front of a non-empty list, why must its Next reference be assigned before the head is changed?
Comprehensive Review
Data structures organize data to suit the needs of a problem, while ADTs describe families of structures through their behavior rather than their internal details. Algorithms provide precise, finite procedures for solving problems, and Big O notation describes how their processing work grows as input grows. A linked list organizes items as nodes connected by references: its head gives access to the chain, its tail marks the final node, and singly linked, doubly linked, and circular forms differ in how their links are arranged. In a C# implementation, a node holds data and a next reference, while a list class manages connections and operations; using a generic type lets the same design work with a chosen data type.

Ask anything
We've read this whole study set, so questions about it get the best answers.