C# Linear Data Structures

Arrays

Arrays are fixed-size collections of elements of the same type. They allow direct access to elements via index.

Example: Arrays

1
int[] numbers = new int[] { 1, 2, 3, 4, 5 };

Lists

Lists are dynamic arrays that can grow in size. They provide methods to add, remove, and access elements.

Example: Lists

1
2
3
List<string> names = new List<string>();
names.Add("Alice");
names.Add("Bob");

Queues

Queues follow the First-In-First-Out (FIFO) principle. Elements are added at the end and removed from the front.

Example: Queues

1
2
3
4
Queue<int> queue = new Queue<int>();
queue.Enqueue(10);
queue.Enqueue(20);
int value = queue.Dequeue(); // Removes and returns the first element (10)

Stacks

Stacks follow the Last-In-First-Out (LIFO) principle. Elements are added and removed from the top.

Example: Stacks

1
2
3
4
Stack<string> stack = new Stack<string>();
stack.Push("A");
stack.Push("B");
string item = stack.Pop(); // Removes and returns the top element ("B")

Linked Lists

Linked lists consist of nodes where each node contains a value and a reference to the next node. They allow efficient insertions and deletions.

Example: Linked Lists

1
2
3
LinkedList<int> linkedList = new LinkedList<int>();
linkedList.AddLast(1);
linkedList.AddLast(2);

Explanation of Data Structures

  1. Arrays

    • Fixed-size
    • Direct access via index
    • Suitable for simple collections with known size
    1
    2
    
    int[] numbers = new int[] { 1, 2, 3, 4, 5 };
    Console.WriteLine(numbers[0]); // Outputs: 1
    
  2. Lists

    • Dynamic size
    • Methods to add, remove, and access elements
    • Suitable for collections where size may change
    1
    2
    3
    4
    
    List<string> names = new List<string>();
    names.Add("Alice");
    names.Add("Bob");
    Console.WriteLine(names[0]); // Outputs: Alice
    
  3. Queues

    • FIFO principle
    • Enqueue to add elements
    • Dequeue to remove and return elements from the front
    1
    2
    3
    4
    
    Queue<int> queue = new Queue<int>();
    queue.Enqueue(10);
    queue.Enqueue(20);
    int value = queue.Dequeue(); // Outputs: 10
    
  4. Stacks

    • LIFO principle
    • Push to add elements
    • Pop to remove and return elements from the top
    1
    2
    3
    4
    
    Stack<string> stack = new Stack<string>();
    stack.Push("A");
    stack.Push("B");
    string item = stack.Pop(); // Outputs: B
    
  5. Linked Lists

    • Consist of nodes with values and references to next nodes
    • Efficient insertions and deletions
    • Suitable for collections where frequent insertions and deletions are required
    1
    2
    3
    4
    
    LinkedList<int> linkedList = new LinkedList<int>();
    linkedList.AddLast(1);
    linkedList.AddLast(2);
    Console.WriteLine(linkedList.First.Value); // Outputs: 1
    

For More visit Officaial Documentation

Previous Chapter Loop