0% found this document useful (0 votes)
17 views

Linear Data Structure Examples

Linear data structure

Uploaded by

honorofkingsfb1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Linear Data Structure Examples

Linear data structure

Uploaded by

honorofkingsfb1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Array Implementation

An array is a collection of elements, all of the same type, stored in contiguous memory locations.

public class ArrayExample


{
private int[] arr; // Array to store elements
private int size; // Size of the array

// Constructor to initialize the array with a given size


public ArrayExample(int size)
{
this.size = size;
arr = new int[size];
}

// Method to insert a value at a specific index


public void Insert(int index, int value)
{
if (index >= 0 && index < size)
{
arr[index] = value; // Insert the value if the index is valid
Console.WriteLine("Inserted {0} at index {1}", value, index);
}
else
{
Console.WriteLine("Index out of bounds."); // Handle invalid index
}
}

// Method to retrieve a value at a specific index


public int Get(int index)
{
if (index >= 0 && index < size)
{
return arr[index]; // Return the value if the index is valid
}
else
{
Console.WriteLine("Index out of bounds.");
return -1; // Return a sentinel value for an invalid index
}
}

// Method to display all elements in the array


public void DisplayArray()
{
Console.WriteLine("Array elements:");
for (int i = 0; i < size; i++)
{
Console.Write(arr[i] + " ");
}
Console.WriteLine();
}
}

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.

public class StackExample


{
private int[] stackArray; // Array to store stack elements
private int top; // Index of the top element
private int maxSize; // Maximum size of the stack

// Constructor to initialize the stack with a given size


public StackExample(int size)
{
maxSize = size;
stackArray = new int[maxSize];
top = -1; // Stack is initially empty
}

// Method to push an element onto the stack


public void Push(int value)
{
if (top == maxSize - 1)
{
Console.WriteLine("Stack Overflow! Cannot push {0}.", value);
}
else
{
stackArray[++top] = value; // Increment top and add value
Console.WriteLine("Pushed {0} to stack.", value);
}
}

// Method to pop the top element from the stack


public int Pop()
{
if (top == -1)
{
Console.WriteLine("Stack Underflow! No element to pop.");
return -1; // Return a sentinel value for an empty stack
}
else
{
return stackArray[top--]; // Return the top element and decrement top
}
}

// Method to display all elements in the stack


public void DisplayStack()
{
if (top == -1)
{
Console.WriteLine("Stack is empty.");
return;
}

Console.WriteLine("Stack elements are:");


for (int i = 0; i <= top; i++)
{
Console.Write(stackArray[i] + " ");
}
Console.WriteLine();
}
}

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.

public class QueueExample


{
private int[] queueArray; // Array to store queue elements
private int front; // Index of the front element
private int rear; // Index of the rear element
private int maxSize; // Maximum size of the queue

// Constructor to initialize the queue with a given size


public QueueExample(int size)
{
maxSize = size;
queueArray = new int[maxSize];
front = 0;
rear = -1; // Queue is initially empty
}

// Method to enqueue an element to the rear of the queue


public void Enqueue(int value)
{
if (rear == maxSize - 1)
{
Console.WriteLine("Queue Overflow! Cannot enqueue {0}.", value);
}
else
{
queueArray[++rear] = value; // Increment rear and add value
Console.WriteLine("Enqueued {0} to queue.", value);
}
}

// Method to dequeue the front element from the queue


public int Dequeue()
{
if (front > rear)
{
Console.WriteLine("Queue Underflow! No element to dequeue.");
return -1; // Return a sentinel value for an empty queue
}
else
{
return queueArray[front++]; // Return the front element and increment front
}
}

// Method to display all elements in the queue


public void DisplayQueue()
{
if (front > rear)
{
Console.WriteLine("Queue is empty.");
return;
}

Console.WriteLine("Queue elements are:");


for (int i = front; i <= rear; i++)
{
Console.Write(queueArray[i] + " ");
}
Console.WriteLine();
}
}

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 class Node


{
public int Data; // The value stored in the node
public Node Next; // The reference to the next node

public Node(int data)


{
Data = data;
Next = null; // Initialize the next reference as null
}
}

public class LinkedList


{
private Node head; // Reference to the first node in the list

public LinkedList()
{
head = null;
}

// Method to add a node at the end of the list


public void AddNode(int data)
{
Node newNode = new Node(data); // Create a new node

if (head == null) // If the list is empty


{
head = newNode;
}
else
{
Node current = head;
while (current.Next != null)
{
current = current.Next; // Traverse to the end of the list
}
current.Next = newNode; // Attach the new node
}
}

// Method to remove a node with specific data


public void RemoveNode(int data)
{
if (head == null)
{
Console.WriteLine("List is empty.");
return;
}
if (head.Data == data)
{
head = head.Next; // Remove the head node
return;
}

Node current = head;


Node previous = null;

while (current != null && current.Data != data)


{
previous = current;
current = current.Next; // Traverse the list
}

if (current == null)
{
Console.WriteLine("Node with data {0} not found.", data);
return;
}

previous.Next = current.Next; // Remove the node


}

// Method to traverse and print the list


public void PrintList()
{
Node current = head;

if (current == null)
{
Console.WriteLine("List is empty.");
return;
}

while (current != null)


{
Console.Write(current.Data + " -> ");
current = current.Next;
}

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.

You might also like