Exercises

  1. What will be the top value for the following stack operations?
          PUSH(1) 
          PUSH(2) 
          PUSH(6) 
          PUSH(3) 
          POP() 
          POP()
    
  2. Assume a stack with the elements {1, 2, 3, 5, 6, 7} in order, with 7 on top. Write the operations required to insert 4 after 3 in the current stack.
  3. Explain the difference in the outputs of the following recursion functions:

    Output 1

    Output 2

    problemfun1<-function(n){
      if(n<1) return(1)
      problemfun1(n-1)
      print(n)
    }

    problemfun1<-function(n){
      if(n<1) return(1)
      print(n)
      problemfun1(n-1)
    }

  4. Write a recursive algorithm to evaluate the Fibonacci sequence. (In a Fibonacci sequence, each item is the sum of the previous two.)
  5. Write a function to invert the values in a stack.
  6. Write a function to implement two stacks using only one ...

Get R Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.