Below are explanations of 10 data structures, each including:
Data Structure Explanation
C# Code Examples (Add, Read, Search, Delete)
Complexity Analysis (Add, Read, Search, Remove) with explanations
1. Array
Data Structure Explanation
An array is a collection of elements of the same data type stored in contiguous memory locations. Each element is identified by an index or subscript.
C# Code Examples
csharp
using System;
class Program
{
static void Main()
{
// Initialize Array
int[] myArray = new int[5];
// **Add** (Assignment)
myArray[0] = 10;
myArray[1] = 20;
myArray[2] = 30;
myArray[3] = 40;
myArray[4] = 50;
// **Read**
Console.WriteLine("Element at index 2: " + myArray[2]);
// **Search** (Linear Search)
int target = 40;
bool found = false;
for (int i = 0; i < myArray.Length; i++)
{
if (myArray[i] == target)
{
Console.WriteLine("Target " + target + " found at index " + i);
found = true;
break;
}
}
if (!found) Console.WriteLine("Target not found");
// **Delete** (Shift elements, assuming last element is now empty)
int deleteIndex = 2;
for (int i = deleteIndex; i < myArray.Length - 1; i++)
{
myArray[i] = myArray[i + 1];
}
myArray[myArray.Length - 1] = 0; // Mark as empty or handle based on need
}
}
Complexity Analysis
Add (Assignment): O(1) - Direct access via index.
Read: O(1) - Immediate access using the index.
Search (Linear): O(n) - Potentially checks every element.
Delete (as implemented): O(n) - Requires shifting all elements after the target index.
2. Linked List
Data Structure Explanation
A linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next.
C# Code Examples
csharp
using System;
public class Node
{
public int Value { get; set; }
public Node Next { get; set; }
public Node(int value)
{
Value = value;
Next = null;
}
}
class LinkedList
{
public Node Head { get; set; }
public void **Add**(int value)
{
Node newNode = new Node(value);
if (Head == null)
Head = newNode;
else
{
Node lastNode = Head;
while (lastNode.Next != null)
lastNode = lastNode.Next;
lastNode.Next = newNode;
}
}
public void **Read**()
{
Node temp = Head;
while (temp != null)
{
Console.Write(temp.Value + " ");
temp = temp.Next;
}
}
public bool **Search**(int target)
{
Node temp = Head;
while (temp != null)
{
if (temp.Value == target)
return true;
temp = temp.Next;
}
return false;
}
public void **Delete**(int target)
{
if (Head == null) return;
if (Head.Value == target)
{
Head = Head.Next;
return;
}
Node temp = Head;
while (temp.Next != null)
{
if (temp.Next.Value == target)
{
temp.Next = temp.Next.Next;
return;
}
temp = temp.Next;
}
}
}
class Program
{
static void Main()
{
LinkedList list = new LinkedList();
list.Add(10);
list.Add(20);
list.Add(30);
Console.WriteLine("Reading: ");
list.Read();
Console.WriteLine("\nSearching for 20: " + list.Search(20));
list.Delete(20);
Console.WriteLine("After Deletion: ");
list.Read();
}
}
Delete: O(n) - May need to traverse the entire list to find the node.
3. Stack
Data Structure Explanation
A stack is a linear data structure that follows the LIFO (Last In First Out) principle, meaning the last element added will be the first one to be removed.
C# Code Examples
csharp
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Stack<int> myStack = new Stack<int>();
// **Push (Add)**
myStack.Push(10);
myStack.Push(20);
myStack.Push(30);
// **Pop (Remove)**
Console.WriteLine("Popped: " + myStack.Pop());
// **Peek (Read top element)**
Console.WriteLine("Top element after pop: " + myStack.Peek());
// **Search** (Manual implementation, Stack doesn't natively support)
bool found = false;
Stack<int> tempStack = new Stack<int>();
int target = 20;
while (myStack.Count > 0)
{
int value = myStack.Pop();
if (value == target)
found = true;
tempStack.Push(value);
}
while (tempStack.Count > 0)
myStack.Push(tempStack.Pop());
Console.WriteLine("Target 20 found: " + found);
}
}
Complexity Analysis
Push (Add): O(1) - Always adds to the top.
Pop (Remove): O(1) - Always removes from the top.
Peek (Read top): O(1) - Direct access to the top element.
Search (Manual): O(n) - Involves potentially moving all elements.
4. Queue
Data Structure Explanation
A queue is a First-In-First-Out (FIFO) data structure, meaning the first element added will be the first one to be removed.
C# Code Examples
csharp
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Queue<int> myQueue = new Queue<int>();
// **Enqueue (Add)**
myQueue.Enqueue(10);
myQueue.Enqueue(20);
myQueue.Enqueue(30);
// **Dequeue (Remove)**
Console.WriteLine("Dequeued: " + myQueue.Dequeue());
// **Peek (Read front element)**
Console.WriteLine("Front element after dequeue: " + myQueue.Peek());
// **Search** (Manual implementation, Queue doesn't natively support)
bool found = false;
Queue<int> tempQueue = new Queue<int>();
int target = 20;
while (myQueue.Count > 0)
{
int value = myQueue.Dequeue();
if (value == target)
found = true;
tempQueue.Enqueue(value);
}
while (tempQueue.Count > 0)
{
myQueue.Enqueue(tempQueue.Dequeue());
}
Console.WriteLine("Target 20 found: " + found);
}
}
Complexity Analysis
Enqueue (Add): O(1) - Always adds to the end.
Dequeue (Remove): O(1) - Always removes from the front.
Peek (Read front): O(1) - Direct access to the front element.
Search (Manual): O(n) - Involves potentially moving all elements.
5. Dictionary (Hash Table)
Data Structure Explanation
A dictionary (or hash table) is a data structure that stores mappings of unique keys to values. Lookups, insertions, and deletions are very efficient.
C# Code Examples
csharp
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Dictionary<string, int> myDict = new Dictionary<string, int>();
// **Add**
myDict.Add("One", 1);
myDict.Add("Two", 2);
// **Read**
Console.WriteLine("Value for 'One': " + myDict["One"]);
// **Search** (Implicit in Read, explicit example)
bool containsKey = myDict.ContainsKey("Two");
Console.WriteLine("Contains 'Two': " + containsKey);
// **Delete**
myDict.Remove("One");
Console.WriteLine("After Removal, Contains 'One': " + myDict.ContainsKey("One"));
}
}
Complexity Analysis
Add: O(1) average, O(n) worst case (hash collisions).
Read: O(1) average, O(n) worst case (hash collisions).