Learning Stack Data Structure

Learning Stack Data Structure

Post the Screenshot here if recording is not available in your dashboard.

sir i cannot find it

What is map, filter, reduce every and some?

1 Like

map()—> Creates new array with the result of calling a function for each array element.
filter()—> Creates new array with every elements in an array that pass a test.
reduce()-> Reduce the values of an array to a single value.

1 Like

Explain the Stack and it’s use cases ?

map()-map functions is used to apply transformation of each element of an array
filter()-filter method creates a new array with only elements that passes a specific condition.
reduce()-reduce function returns a numerical values of an array.
some()-some is used wheather one number is even number the full statement is true,or every number odd
the statement is false.
every()-every function is number which is the number is positive number the statement is true,or one number is negative number the statement is false

1 Like

Stack is pushed on to the call /top of the element and it is a statck of LIFO which is last-in-first-out of a stack.
they are 2 use cases
*Function Call Stack
*Undo/Redo Operations
operations on the stack
**push
**pop
**top
**size of stack
**clear of stack
**empty check

1 Like

Map, reduce, and filter are all array methods in JavaScript. Each one will iterate over an array and perform a transformation or computation. Each will return a new array based on the result of the function.

1 Like

A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle. Stack has one end. It contains only one pointer top pointer pointing to the topmost element of the stack. Whenever an element is added in the stack, it is added on the top of the stack, and the element can be deleted only from the stack. In other words, a stack can be defined as a container in which insertion and deletion can be done from the one end known as the top of the stack.

use cases of stack:

  • Function calls and recursion: When a function is called, the current state of the program is pushed onto the stack. When the function returns, the state is popped from the stack to resume the previous function’s execution.
  • Undo/Redo operations: The undo-redo feature in various applications uses stacks to keep track of the previous actions. Each time an action is performed, it is pushed onto the stack. To undo the action, the top element of the stack is popped, and the reverse operation is performed.
  • Expression evaluation: Stack data structure is used to evaluate expressions in infix, postfix, and prefix notations. Operators and operands are pushed onto the stack, and operations are performed based on the stack’s top elements.
  • Browser history: Web browsers use stacks to keep track of the web pages you visit. Each time you visit a new page, the URL is pushed onto the stack, and when you hit the back button, the previous URL is popped from the stack.
  • Balanced Parentheses: Stack data structure is used to check if parentheses are balanced or not. An opening parenthesis is pushed onto the stack, and a closing parenthesis is popped from the stack. If the stack is empty at the end of the expression, the parentheses are balanced.
  • Backtracking Algorithms: The backtracking algorithm uses stacks to keep track of the states of the problem-solving process. The current state is pushed onto the stack, and when the algorithm backtracks, the previous state is popped from the stack.
1 Like
  1. map():- creates a new array from calling a function for every array element.it will not execute for empty elements and also does not change the original array
  2. Filter():- method creates a new array filled with elements that pass a test provided by a function
  3. Reduce():- method returns a single value, which the function’s having accumulated result.
  4. Some():- method returns true (and stops) if the function returns true for one of the array elements and returns false if the function returns false for all of the array elements.
  5. Every():- method returns true if the function returns true for all elements. and returns false if the function returns false for one element.
1 Like

Elements can be added and removed from the stack only at the top. push adds an item to the top of the stack, pop removes the item from the top, LIFO (last In First Out) method we used for the stack.
use Case :-
a) Function call Stack
b) Undo/Redo
c) Evaluation of Arithmetic Expressions
d) Backtracking
e) Delimiter Checking
f) Reverse a Data

1 Like




1 Like

Nice Notes Everyone.

// Map:
//map()is used to apply transformation to each element of an array
//and Returns a new array
// let arr1=[ 10, 20, 30, 50, 60 ]
// let newarray=arr1.map(function(element){
//     // console.log(element)
//     element =element*100;
//     return element
// });
// console.log(newarray)//[ 1000, 2000, 3000, 5000, 6000 ]
//console.log(arr1)//[ 10, 20, 30, 50, 60 ] original array doesn't change

//filter():
// creates new array with only element that passes specific condition:
let arr2=[ 10, 22, 35, 57, 65,34 ];
let newarray2= arr2.filter(function(element){
        return element%2===0;
        // return element%2 != 0;[ 35, 57, 65 ]
    })
    console.log(newarray2)//[ 10, 22,34 ]

// reduce():
//Reduce the values of an array and return numeric  value.
// let arr2=[ 100,2,3,4,5 ];//86
// let reduceValue=arr2.reduce(function(acumulator,element){// accepts two parameter
//     return acumulator-(element);
// },)//inital value
// console.log(reduceValue)//86

// some();
//if any of the condition is true,it returns true
//no element is satisfied ,it return false
// let arr2=[ 14, 22, 36, 56, 66,33 ]

// let hasEvennumber=arr2.some(function(element){
//     let bool =element % 2 ===0;
//     return bool;
// })
// console.log(hasEvennumber)//True
// every():
///if every  condition should be satisfied  ,it returns true
//no element is not satisfied ,it return false
let arr2=[ 10, 22, 34, 67,34 ];
// let hasPostive=arr2.every(function(element){
//     let bool =element % 2 ===0;
//     return bool;
// })
// console.log(hasPostive)//false
1 Like
Map() :-
        -> map function is used to apply transformation to each element of an array.
        -> It will just iterate on array & returns you a new array.
        -> This will not change the original array.

Reduce() :-
        -> Returns a numerical value.
        -> It has 2 paramenters: Accumulator, element
        -> // Reduce Syntax:
            array.reduce(function (accumulator, element) {// Your Code}, initialValueOfAccumulator);
        ->  'initialValueOfAccumulator = 0 / 10 / any numeric values' 
                - It will also add if we give any numeric values other than 0
                - 0 means initial value before summing up by adds to first value
       -> 🚨📢Capture Intern Question
              // With 'initialValueOfAccumulator' - Wrong Answer with 0 in -ve
              let value = arr4.reduce(function (accumulator, element) {
                  return accumulator - element;
              }, 0);
              console.log(value); // -1700
              /*
              0 - 1000 ==> -1000
              -1000 - 400 ==> -1400
              -1400 - 200 ==> -1600
              -1600 - 100 ==> -1700
              */
          
              // Without 'initialValueOfAccumulator' - Correct Answer by removing -> '0' 
              let value1 = arr4.reduce(function (accumulator, element) {
                  return accumulator - element;
              });
              console.log(value1); // 300
              /*
              1000 - 400 ==> 600
              600 - 200 ==> 400
              400 - 100 ==> 300
              */
Filter() :-
        -> It filters to get only like even numbers, odd numbers etc based on your condition.
        -> It returns you a new array.
        -> This will not change the original array.
        -> filter condition will create a new array with only elements that passes specific condition

Every() :-
        -> If all elements satisfies this condition, then only it will return True. 
        -> If anyone not satisfies then False.

Some() :-
       -> Reason: Atleast one value satisfies condition, it returns True value. 
       -> If nothing satisfies only returns False.
1 Like
Stack :-
      > Follows Last In First Out (LIFO rule) | It interacts only with the end node
      > It allocate and deallocate memory for individual nodes as required, making them very flexible. 
      > Stacks provide 2 primary functions: 
          - ‘pushing’ or introducing a new element, and 
          - ‘popping’ or removing the last element.
      > Other 3 operations are:- Top, SizeOf & Empty 
      > Practical example is balls put inside a glass. We can pull out last ball only.
      > In Browser History, last & recently viewed web pages are stacked at top.
      > In chat application also recent messages are stacked at last.

Stack's  Use Cases :-
      > Function call stack in JS engine
      > Executes functions one by one and pops out last function element
      > A stack can be used to store operations that were triggered to implement the undo functionality.
      > “Back” buttons for navigation (“Next” navigation also uses a stack).
      > Chronological news feeds behave in a LIFO manner to ensure the latest item is at the top of the feed, hence a stack can be used for this.

 Use Case 1: Reversing Order
 Use Case 2: Testing Symmetry
 Use Case 3: Undoing Commands

Stack Use Cases

1 Like

A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle.
stack can be defined as a container in which insertion and deletion can be done from the one end known as the top of the stack.
Use Cases:
Function call stack:
Undo/redo operation
Operation:
pop()
push()
clear
Emptyof()
isFull()
size()
top()/peek()

1 Like