Exercises
- What will be the top value for the following stack operations?
PUSH(1) PUSH(2) PUSH(6) PUSH(3) POP() POP()
- 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.
- 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) }
- Write a recursive algorithm to evaluate the Fibonacci sequence. (In a Fibonacci sequence, each item is the sum of the previous two.)
- Write a function to invert the values in a stack.
- 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.