In: Computer Science
Question five
Write a c program to calculate the factorial of a number using recursion .
#include <stdio.h>
int factorial(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %d", n, factorial(n));
return 0;
}
int factorial(int n)
{
if (n >= 1)
return n*factorial(n-1);
else
return 1;
}
Write a stack algorithm to POP an item
function pop: stack if stack is empty return null endif data ← stack[top] top ← top - 1 return data end
Function://implementation
int pop(int data) { if(!isempty()) { data = stack[top]; top = top - 1; return data; } else { printf("Could not retrieve data, Stack is empty.\n"); } }
What does FRONT and REAR signify in a queue?
Front and rear variables point to the position from where insertions and deletions are performed in a queue. Initially, the value of front and queue is -1 which represents an empty queue. Array representation of a queue containing 5 elements along with the respective values of front and rea
A | B | C | D | E | |
0 | 1 | 2 | 3 | 4 | 5 |
FRONT | REAR |
FRONT POINTS TO A
REAR POINTS TO E
Write an algorithm for a dequeue operation
function dequeue if queue is empty return underflow end if data = queue[front] front ← front + 1 return true end function
Implementation
int dequeue() { if(isempty()) return 0; int data = queue[front]; front = front + 1; return data; }