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

Any help on any of these questions will be appreciated. The code can be a psuedo

ID: 3666625 • Letter: A

Question

Any help on any of these questions will be appreciated. The code can be a psuedo code.

1. Use the methods push, pop, peek and empty to construct code which does each of the following:

a. Set the variable i equal to the second element from the top of the stack. Leave the stack unchanged.

b. Given an integer n, set the variable i equal to the nth element from the top of the stack. Leave the stack without its top n elements.

c. Set the variable i equal to the bottom element of the stack. Leave the stack unchanged.

d.  Set the variable i equal to the third element from the bottom of the stack.

Explanation / Answer


Lets assume stack is 'S', and top of stack is 'top'

a.

int ele;
int i = 1;
int temp = top; // saving original value og top in temp, later to restore
while(i <=2){ // peek two times
   if(empty(S) == true) // if stack has lesser element
       printf("Stack does not have enough element");
   ele = peek(S);
   top = top-1;
   }
//restoring top
top = temp;
return ele;

b.
// here stack will be changed
int ele;
int i = 1;
while(i <=N){ // pop N times
   if(empty(S) == true) // if stack has lesser element
       printf("Stack does not have enough element");
   ele = pop(S);// popping
   top = top-1; //reducing top
   }
return ele;

c.

int ele;
int i = 1;
int temp = top; // saving original value og top in temp, later to restore
while(top != -1){ // peek all elements till last
   if(empty(S) == true) // if stack has lesser element
       printf("Stack does not have enough element");
   ele = peek(S);
   top = top-1;
   }
//restoring top
top = temp;
return ele;

d.


int ele;
int i = 1;
int temp = top; // saving original value og top in temp, later to restore
while(top <=(top - 3)){ // peek till (top - 3)
   if(empty(S) == true) // if stack has lesser element
       printf("Stack does not have enough element");
   ele = peek(S);
   top = top-1;
   }
//restoring top
top = temp;
return ele;
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote