Stacks Flashcards

1
Q

General

A

Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Operations

A

Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.
Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.
Peek or Top: Returns top element of stack.
isEmpty: Returns true if stack is empty, else fails.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Time Complexity

A

push(), pop(), esEmpty() and peek() all take O(1) time. We do not run any loop in any of these operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Applications

A

Balancing of symbols
Infix to Postfix /Prefix conversion
Redo-undo features at many places like editors, photoshop.
Forward and backward feature in web browsers
Used in many algorithms like Tower of Hanoi, tree traversals, stock span problem, histogram problem.
Other applications can be Backtracking, Knight tour problem, rat in a maze, N queen problem and sudoku solver

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Implementation

A

There are two ways to implement a stack:

Using array
Using linked list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Array

A

Pros: Easy to implement. Memory is saved as pointers are not involved.
Cons: It is not dynamic. It doesn’t grow and shrink depending on needs at runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Linked List

A

Pros: The linked list implementation of stack can grow and shrink according to the needs at runtime.
Cons: Requires extra memory due to involvement of pointers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Pros

A
  1. Helps manage the data in particular way (LIFO) which is not possible with Linked list and array.
  2. When function is called the local varriables are stored in stack and destroyed once returned. Stack is used when varriable is not used outside the function.
    So, it gives control over how memory is allocated and deallocated
  3. Stack frees you from the burden of remembering to cleanup(read delete) the object
  4. Not easily corrupted (No one can easily inset data in middle)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Cons

A
  1. Stack memory is limited.
  2. Creating too many objects on the stack will increase the chances of stack overflow
  3. Random access not possible
How well did you know this?
1
Not at all
2
3
4
5
Perfectly