Skip to content

Commit cd84cc2

Browse files
authored
Merge pull request #1034 from Hemav009/hema
Added stack using array in DSA
2 parents a4cbc3e + 68a39de commit cd84cc2

File tree

4 files changed

+324
-82
lines changed

4 files changed

+324
-82
lines changed

docs/dsa/stack/_category_.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Stack",
3+
"position": 9,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Stack is a linear data structure that follows LIFO principle"
7+
}
8+
}

docs/dsa/stack/stack-using-array.md

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
---
2+
id: stack-in-dsa
3+
title: Stack Using Array
4+
sidebar_label: Stack Using Array
5+
sidebar_position: 1
6+
description: "Stack is a linear data structure that follows LIFO principle"
7+
tags: [dsa, data-structures, stack, LIFO]
8+
---
9+
10+
### Introduction to Stack
11+
12+
A stack is a linear data structure that follows the Last In First Out (LIFO) principle. This means that the last element added to the stack will be the first one to be removed. Stacks are used in various applications such as expression evaluation, function call management in recursion, and more.
13+
14+
### Stack Operations
15+
16+
1. **Push**: Add an element to the top of the stack.
17+
2. **Pop**: Remove the top element from the stack.
18+
3. **Peek (or Top)**: Retrieve the top element of the stack without removing it.
19+
4. **isEmpty**: Check if the stack is empty.
20+
5. **Size**: Get the number of elements in the stack.
21+
22+
### Pseudocode
23+
24+
#### Basic Operations
25+
26+
1. **Push**:
27+
28+
```text
29+
function push(stack, element):
30+
stack.append(element)
31+
```
32+
33+
2. **Pop**:
34+
35+
```text
36+
function pop(stack):
37+
if isEmpty(stack):
38+
return "Stack Underflow"
39+
return stack.pop()
40+
```
41+
42+
3. **Peek**:
43+
44+
```text
45+
function peek(stack):
46+
if isEmpty(stack):
47+
return "Stack is empty"
48+
return stack[-1]
49+
```
50+
51+
4. **isEmpty**:
52+
53+
```text
54+
function isEmpty(stack):
55+
return len(stack) == 0
56+
```
57+
58+
5. **Size**:
59+
```text
60+
function size(stack):
61+
return len(stack)
62+
```
63+
64+
### Implementation in Python, C++, and Java
65+
66+
#### Python Implementation
67+
68+
```python
69+
class Stack:
70+
def __init__(self):
71+
self.elements = []
72+
73+
def push(self, element):
74+
self.elements.append(element)
75+
76+
def pop(self):
77+
if self.is_empty():
78+
return "Stack Underflow"
79+
return self.elements.pop()
80+
81+
def peek(self):
82+
if self.is_empty():
83+
return "Stack is empty"
84+
return self.elements[-1]
85+
86+
def is_empty(self):
87+
return len(self.elements) == 0
88+
89+
def size(self):
90+
return len(self.elements)
91+
92+
# Example usage
93+
stack = Stack()
94+
stack.push(10)
95+
stack.push(20)
96+
print(stack.pop()) # Output: 20
97+
print(stack.peek()) # Output: 10
98+
print(stack.is_empty()) # Output: False
99+
print(stack.size()) # Output: 1
100+
```
101+
102+
#### C++ Implementation
103+
104+
```cpp
105+
#include <iostream>
106+
#include <vector>
107+
108+
class Stack {
109+
private:
110+
std::vector<int>.elements;
111+
112+
public:
113+
void push(int element) {
114+
.elements.push_back(element);
115+
}
116+
117+
int pop() {
118+
if (isEmpty()) {
119+
std::cerr << "Stack Underflow" << std::endl;
120+
return -1;
121+
}
122+
int top =.elements.back();
123+
.elements.pop_back();
124+
return top;
125+
}
126+
127+
int peek() {
128+
if (isEmpty()) {
129+
std::cerr << "Stack is empty" << std::endl;
130+
return -1;
131+
}
132+
return.elements.back();
133+
}
134+
135+
bool isEmpty() {
136+
return.elements.empty();
137+
}
138+
139+
int size() {
140+
return.elements.size();
141+
}
142+
};
143+
144+
// Example usage
145+
int main() {
146+
Stack stack;
147+
stack.push(10);
148+
stack.push(20);
149+
std::cout << stack.pop() << std::endl; // Output: 20
150+
std::cout << stack.peek() << std::endl; // Output: 10
151+
std::cout << std::boolalpha << stack.isEmpty() << std::endl; // Output: false
152+
std::cout << stack.size() << std::endl; // Output: 1
153+
return 0;
154+
}
155+
```
156+
157+
#### Java Implementation
158+
159+
```java
160+
import java.util.ArrayList;
161+
162+
public class Stack {
163+
private ArrayList<Integer>.elements;
164+
165+
public Stack() {
166+
.elements = new ArrayList<>();
167+
}
168+
169+
public void push(int element) {
170+
.elements.add(element);
171+
}
172+
173+
public int pop() {
174+
if (isEmpty()) {
175+
System.out.println("Stack Underflow");
176+
return -1;
177+
}
178+
return.elements.remove.elements.size() - 1);
179+
}
180+
181+
public int peek() {
182+
if (isEmpty()) {
183+
System.out.println("Stack is empty");
184+
return -1;
185+
}
186+
return.elements.get.elements.size() - 1);
187+
}
188+
189+
public boolean isEmpty() {
190+
return.elements.isEmpty();
191+
}
192+
193+
public int size() {
194+
return.elements.size();
195+
}
196+
197+
// Example usage
198+
public static void main(String[] args) {
199+
Stack stack = new Stack();
200+
stack.push(10);
201+
stack.push(20);
202+
System.out.println(stack.pop()); // Output: 20
203+
System.out.println(stack.peek()); // Output: 10
204+
System.out.println(stack.isEmpty()); // Output: false
205+
System.out.println(stack.size()); // Output: 1
206+
}
207+
}
208+
```
209+
210+
### Complexity
211+
212+
- **Time Complexity**:
213+
214+
- Push: $O(1)$
215+
- Pop: $O(1)$
216+
- Peek: $O(1)$
217+
- isEmpty: $O(1)$
218+
- Size: $O(1)$
219+
220+
- **Space Complexity**: $O(n)$, where $n$ is the number of elements in the stack.
221+
222+
### Example
223+
224+
Consider a stack with the following operations:
225+
226+
1. Push 10
227+
2. Push 20
228+
3. Pop
229+
4. Peek
230+
5. Check if empty
231+
6. Get size
232+
233+
**Operations**:
234+
235+
- Push 10: Stack becomes [10]
236+
- Push 20: Stack becomes [10, 20]
237+
- Pop: Removes 20, Stack becomes [10]
238+
- Peek: Returns 10, Stack remains [10]
239+
- isEmpty: Returns false
240+
- Size: Returns 1
241+
242+
### Conclusion
243+
244+
A stack is a fundamental data structure used in computer science for various applications where the LIFO (Last In First Out) principle is required. It is simple to implement and provides efficient operations for adding and removing elements. Understanding and using stacks effectively can help solve many algorithmic problems.

0 commit comments

Comments
 (0)