Linear Data Structure Examples
Linear Data Structure Examples
Array Implementation
An array is a collection of elements, all of the same type, stored in contiguous memory locations.
Explanation:
Array Declaration: int[] arr; declares an array to store integer elements.
Insert Method: Adds an element at a specified index. It checks if the index is within bounds before inserting
the value.
Get Method: Retrieves an element at a specified index. It checks if the index is valid and returns the
element; otherwise, it returns a sentinel value.
DisplayArray Method: Loops through the array and prints all elements.
2. Stack Implementation
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle, meaning the last element added is
the first to be removed.
Explanation:
Push Method: Adds an element to the top of the stack. It checks if the stack is full (overflow) before adding
the element.
Pop Method: Removes and returns the top element. It checks if the stack is empty (underflow) before
removing the element.
DisplayStack Method: Prints all the elements currently in the stack from bottom to top.
3. Queue Implementation
A queue is a linear data structure that follows the First In, First Out (FIFO) principle, meaning the first element added
is the first to be removed.
Explanation:
Enqueue Method: Adds an element to the rear of the queue. It checks if the queue is full (overflow) before
adding the element.
Dequeue Method: Removes and returns the front element. It checks if the queue is empty (underflow)
before removing the element.
DisplayQueue Method: Prints all the elements currently in the queue from front to rear.
4. Linked List Implementation
A linked list is a linear data structure where each element (node) contains a reference to the next node, forming a
chain.
public LinkedList()
{
head = null;
}
if (current == null)
{
Console.WriteLine("Node with data {0} not found.", data);
return;
}
if (current == null)
{
Console.WriteLine("List is empty.");
return;
}
Console.WriteLine("null");
}
}
Explanation:
Node Class: Represents each element in the linked list. It contains the data and a reference to the next node.
LinkedList Class: Manages the linked list operations like adding, removing, and printing nodes.
AddNode Method: Adds a new node to the end of the list.
RemoveNode Method: Removes the first node that contains the specified data.
PrintList Method: Traverses the list and prints all the nodes from head to the last node.
Summary:
Array: A simple, fixed-size data structure that stores elements in contiguous memory locations. Allows direct
access via indices.
Stack: A LIFO data structure implemented using an array. Elements are added and removed from the same
end (top).
Queue: A FIFO data structure implemented using an array. Elements are added to the rear and removed
from the front.
Linked List: A dynamic data structure where each element points to the next. Allows easy insertion and
deletion of nodes without the need for shifting elements, unlike arrays.