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
|
|
Lists
Lists are dynamic arrays that can grow in size. They provide methods to add, remove, and access elements.
Example: Lists
|
|
Queues
Queues follow the First-In-First-Out (FIFO) principle. Elements are added at the end and removed from the front.
Example: Queues
|
|
Stacks
Stacks follow the Last-In-First-Out (LIFO) principle. Elements are added and removed from the top.
Example: Stacks
|
|
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
|
|
Explanation of Data Structures
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
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
Queues
- FIFO principle
Enqueue
to add elementsDequeue
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
Stacks
- LIFO principle
Push
to add elementsPop
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
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