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

1. Suppose that an integer Stack class has member functions void pop(), void pee

ID: 3786816 • Letter: 1

Question

1. Suppose that an integer Stack class has member functions void pop(), void peek(int &top) and bool isEmpty(). Implement function int uncover(Stack &s, int n); that pops the stack s until n is at the top, or empties the stack,
if n isn’t found. The return value it the number of items popped from the stack to reach n or to empty the stack in the case where n is not found.

2. An alternate implementation for a circular array-based queue includes int values[MAXSIZE] along with  int front and int size. Implement the private helper function int Queue::getBack() that returns the index of the back of the queue (where the next item will be inserted). In the case where the queue is full, getBack should return -1. Then implement the boolQueue::insert(int v) function.

3. Implement a recursive function int * find(int * first, int * last, int target) that looks for an occurrence of target   in the sorted subarray bounded by elements at addresses first and last. If there is an occurrence of target in the subarray, the function will return a pointer to that element. If there is no such occurrence, then the function will return nullptr. Since the elements between *first and *last are sorted in ascending order, you should use binary search. Your binary search algorithm will work as follows: In the case where first == last just check element *first to see if it's equal to target. If it is, then return first, otherwise return nullptr. In the case where first < last , compute the average of first and last to get a pointer mid to the element in the middle of the current subarray. If target is less than or equal to that element then recursively call the function on the subarray bounded by the elements at addresses first and mid. Otherwise recursively call the function on the subarray bounded by the elements at addresses mid+1 and last

Explanation / Answer

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void push();
int pop();
void display();

struct stack
{
int no;
struct stack *next;
}
*top=NULL;
typedef struct stack st;
void main()
{
int choice,item;
char ch;
clrscr();
   do
   {
   printf("Enter your option ");
   printf("1 . push ");
   printf("2 . pop ");
   printf("3 . display ");
   scanf("%d",&choice);

   switch(choice)
       {
   case 1: push();
       break;
   case 2: item=pop();
           printf("Deleted element is = %d ",item);
           break;
   case 3: display();
           break;
   default:printf("You entered wrong choice");
       }
   printf(" Do you wish to continue");
   fflush(stdin);
   scanf("%c",&ch);
   }
   while(ch=='y' || ch=='Y');
}


void push()
{
st *node;
node=(st*)malloc(sizeof(st));
printf("Enter number");
scanf("%d",&node->no);
printf("NODE=%d",node);
node->next=top;
top=node;
printf("top=%d",top);
}

int pop()
{
st *temp;
temp=top;
printf("temp=%d",temp);
if(top==NULL)
{
printf("Stack is empty");
getch();
exit(0);

}
else
{
top=top->next;
printf("%d",top);
free(temp);
}
return(temp->no);
}

void display()
{
st *temp;
temp=top;
while(temp->next!=NULL)
{
printf(" no= %d",temp->no);
temp=temp->next;
}
printf(" no=%d",temp->no);
}