Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

N pancakes, all of distinct sizes, are piled in a stack. Your task is to sort th

ID: 3828275 • Letter: N

Question

N pancakes, all of distinct sizes, are piled in a stack. Your task is to sort them by size, with the largest pancake at the bottom and the smallest one on top. The only operation you are allowed to perform is to stick a spatula anywhere in the stack and flip the pancakes above the spatula. That is, in one operation you can flip the top k pancakes, where k is any number of your choice between 1 and N. An example is shown below. Describe a recursive strategy to sort any stack using a sequence of allowed operations. Give a detailed proof to show that your strategy correctly sorts every stack. Do not omit any details. Write a recurrence relation S(N) for the maximum number of operations that suffice to sort a stack of N pancakes. Show that S(N) lessthanorequalto 2N - 3. for all N greaterthanorequalto 1.

Explanation / Answer

I can help you to give the answer of 1 (Stack program by recurive)

for rest of i suggest you go to search engine , it will give you help


/ * C Program to Reverse a Stack using Recursion */
#include <stdio.h>
#include <stdlib.h>

struct node
{
int a;
struct node *next;
};

void generate(struct node **);
void display(struct node *);
void stack_reverse(struct node **, struct node **);
void delete(struct node **);

int main()
{
struct node *head = NULL;

generate(&head);
printf(" The sequence of contents in stack ");
display(head);
printf(" Inversing the contents of the stack ");
if (head != NULL)
{
stack_reverse(&head, &(head->next));
}
printf(" The contents in stack after reversal ");
display(head);
delete(&head);

return 0;
}

void stack_reverse(struct node **head, struct node **head_next)
{
struct node *temp;

if (*head_next != NULL)
{
temp = (*head_next)->next;
(*head_next)->next = (*head);
*head = *head_next;
*head_next = temp;
stack_reverse(head, head_next);
}
}

void display(struct node *head)
{
if (head != NULL)
{
printf("%d ", head->a);
display(head->next);
}
}

void generate(struct node **head)
{
int num, i;
struct node *temp;

printf("Enter length of list: ");
scanf("%d", &num);
for (i = num; i > 0; i--)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->a = i;
if (*head == NULL)
{
*head = temp;
(*head)->next = NULL;
}
else
{
temp->next = *head;
*head = temp;
}
}
}

void delete(struct node **head)
{
struct node *temp;
while (*head != NULL)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
}