Array-Based Data Structures
Array-based data structures build upon the foundational properties of arrays,
leveraging their efficiency in indexing and storage. They are widely used in
computer science for organizing and processing data.
1. Stacks
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle.
Array Representation:
o Use an array to store elements.
o Maintain a pointer (top) to track the index of the last inserted
element.
Operations:
o Push: Add an element to the top.
o Pop: Remove the top element.
o Peek: View the top element.
Python Implementation:
class Stack:
def __init__(self):
self.stack = []
def push(self, value):
self.stack.append(value)
def pop(self):
return self.stack.pop() if self.stack else None
def peek(self):
return self.stack[-1] if self.stack else None
, JavaScript Implementation:
class Stack {
constructor() {
this.stack = [];
}
push(value) {
this.stack.push(value);
}
pop() {
return this.stack.pop();
}
peek() {
return this.stack[this.stack.length - 1];
}
}
2. Queues
A queue is a linear data structure that follows the First In, First Out (FIFO)
principle.
Array Representation:
o Use an array to store elements.
o Elements are added at the rear and removed from the front.
Operations:
o Enqueue: Add an element to the rear.
o Dequeue: Remove an element from the front.
Python Implementation:
class Queue:
def __init__(self):
self.queue = []
def enqueue(self, value):
Array-based data structures build upon the foundational properties of arrays,
leveraging their efficiency in indexing and storage. They are widely used in
computer science for organizing and processing data.
1. Stacks
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle.
Array Representation:
o Use an array to store elements.
o Maintain a pointer (top) to track the index of the last inserted
element.
Operations:
o Push: Add an element to the top.
o Pop: Remove the top element.
o Peek: View the top element.
Python Implementation:
class Stack:
def __init__(self):
self.stack = []
def push(self, value):
self.stack.append(value)
def pop(self):
return self.stack.pop() if self.stack else None
def peek(self):
return self.stack[-1] if self.stack else None
, JavaScript Implementation:
class Stack {
constructor() {
this.stack = [];
}
push(value) {
this.stack.push(value);
}
pop() {
return this.stack.pop();
}
peek() {
return this.stack[this.stack.length - 1];
}
}
2. Queues
A queue is a linear data structure that follows the First In, First Out (FIFO)
principle.
Array Representation:
o Use an array to store elements.
o Elements are added at the rear and removed from the front.
Operations:
o Enqueue: Add an element to the rear.
o Dequeue: Remove an element from the front.
Python Implementation:
class Queue:
def __init__(self):
self.queue = []
def enqueue(self, value):