Mastering Stacks and Queues: Data Structure Fundamentals
1. What is a Stack?
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. Think of it like a stack of plates in a cafeteria: the last plate you place on top is the first one you take off.
2. Representation of a Stack
There are two primary ways to implement a stack in memory:
A. Array Representation (Static Allocation)
- How it works: A fixed-size array is allocated, and a variable named
topkeeps track of the index of the topmost element. - Initial State:
top = -1(indicates
Java Design Patterns: Practical Code Examples
Singleton Pattern
class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
public class Main {
public static void main(String[] args) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
System.out.println(s1 == s2);
}
}Factory Pattern
interface Animal Read More
Essential Array Operations and Binary Tree Traversal
Array Traversal
Traversal means accessing or visiting each element of an array one by one. It is used to display, process, or perform operations on all elements of the array. Traversal is a fundamental operation in data structures.
Algorithm for Array Traversal
- Start
- Enter the size of array N
- Enter the array elements
- Set I = 0
- Repeat while I < N:
- Print ARR[I]
- Set I = I + 1
- Stop
Example: Suppose the array is A = [10, 20, 30, 40]. After traversal, the output will be: 10 20 30 40.
Pros and Cons
- Advantages: Useful
C# Object-Oriented Programming: CSV Parsing and Inheritance
CSV Data Processing in C#
public class Program
{
public static void Main(string[] args)
{
List<Employee> employees = new List<Employee>();
string filePath = "employees.csv";
try
{
using(StreamReader reader = new StreamReader(filePath))
{
reader.ReadLine(); // Skip header
string line;
while((line = reader.ReadLine()) != null)
{
string[] data Read More
Algorithmic Solutions: String Manipulation and Parsing
String Duplicate Removal
def removeDuplicates(self, s: str, k: int) -> str:
stack = []
for char in s:
if not stack:
stack.append((char, 1))
else:
last_char, last_cnt = stack[-1]
if char != last_char:
stack.append((char, 1))
else:
if last_cnt + 1 < k:
stack[-1] = (last_char, last_cnt + 1)
else:
stack.pop()
return ''.join(c * cnt for Read More
String Operations and Queue Data Structures Explained
String Storage and Memory Representation
A string is a collection of characters stored together in memory. Strings are used to store names, words, sentences, and other text data in computer systems. In programming languages like C, a string is stored as an array of characters and ends with a special null character '\0'.
Example of String: char name[] = "ROHIT";
In memory, the string is stored as:
| R | O | H | I | T | \0 |
Here, \0 represents the end of the string.
