Skip to content

Commit a0b9ef3

Browse files
committed
Stack README updated and code reformatted
1 parent 571056d commit a0b9ef3

File tree

5 files changed

+51
-25
lines changed

5 files changed

+51
-25
lines changed

Data Structure/Stack/README.md

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,46 @@ A **stack** is an Abstract Data Type (ADT), commonly used in most programming la
44

55
A real-world stack allows operations at one end only. For example, we can place or remove a card or plate from the top of the stack only. Likewise, Stack ADT allows all data operations at one end only. At any given time, we can only access the top element of a stack.
66

7-
This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element which is placed (inserted or added) last, is accessed first. In stack terminology, insertion operation is called PUSH operation and removal operation is called POP operation.
7+
This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element which is placed (inserted or added) last, is accessed first. In stack terminology, insertion operation is called **PUSH** operation and removal operation is called **POP** operation.
88

99

1010
Conceptually, a stack is simple: a data structure that allows adding and removing elements in a particular order. Every time an element is added, it goes on the top of the stack; the only element that can be removed is the element that was at the top of the stack. Consequently, a stack is said to have "first in last out" behavior (or "last in, first out"). The first item added to a stack will be the last item removed from a stack.
1111

12-
![Stack](stack.svg)
12+
![Stack](./images/stack.svg)
1313

14-
**Basic Operations:**
14+
### Basic Operations:
1515

1616
Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from these basic stuffs, a stack is used for the following two primary operations −
1717

18-
- **push()** − Pushing (storing) an element on the stack.
19-
- **pop()** − Removing (accessing) an element from the stack.
18+
#### Push
19+
20+
The process of putting a new data element onto stack is known as a **Push** Operation.
21+
22+
There is a pointer generally known as **Top** which tracks the last inserted position or the top of the Stack. In every insertion and deletion in the Stack will update this Top as well.
23+
24+
To insert a new element to the Stack, first check if the stack is full (for fixed size Stack). If the stack is not full then insert the element at the top of the Stack and update the Top.
25+
26+
![Stack Push](./images/push-operation.gif)
27+
28+
29+
#### Pop
30+
31+
Accessing the content while removing it from the stack, is known as a **Pop** Operation.
32+
33+
In Pop operation, if the Stack is not empty then the last inserted element is removed and the Top points to the next element.
34+
35+
![Stack Pop](./images/pop-operation.gif)
36+
2037

2138
To use a stack efficiently, we need to check the status of stack as well. For the same purpose, the following functionality are available too −
2239

23-
- **peek()** − get the top data element of the stack, without removing it.
24-
- **isEmpty()** − check if stack is empty.
40+
- **Peek** − get the top data element of the Stack, without removing it.
41+
- **isEmpty** − check if Stack is empty.
42+
43+
44+
```
45+
In Javascript, Stack can be easily implemented by an Array. Javascript Arrays have the built-in methods called 'push' and 'pop', which do exactly the same operation as Stack.
46+
```
2547

2648

2749
#### Complexity Analysis

Data Structure/Stack/Stack.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1+
/*
2+
Stack Implementation in Javascript
3+
*/
4+
15
//Stack Class
2-
function Stack(){
3-
var items = [];
6+
function Stack() {
7+
var items = [];
48

5-
this.push = function(element){
6-
items.push(element);
7-
}
9+
this.push = function(element) {
10+
items.push(element);
11+
}
812

9-
this.pop = function(){
10-
return items.pop();
11-
}
13+
this.pop = function() {
14+
return items.pop();
15+
}
1216

13-
this.peek = function(){
14-
return items[items.length - 1];
15-
}
17+
this.peek = function() {
18+
return items[items.length - 1];
19+
}
1620

17-
this.isEmpty = function(){
18-
return items.length === 0;
19-
}
21+
this.isEmpty = function() {
22+
return items.length === 0;
23+
}
2024

21-
this.printAll = function(){
22-
console.log(items.toString());
23-
}
25+
this.printAll = function() {
26+
console.log(items.toString());
27+
}
2428
}
2529

26-
//Testing the Stack
30+
/***************** Testing the Stack ***************/
2731
var stack = new Stack();
2832
stack.push(10);
2933
stack.push(15);
29.2 KB
Loading
36.9 KB
Loading

0 commit comments

Comments
 (0)