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

(20) Question 2: Using a stack abstract data type Assume that you\'ve been given

ID: 3710236 • Letter: #

Question

(20) Question 2: Using a stack abstract data type Assume that you've been given an absiract data type for a stuck. It has the following operations ld push.on.stackrant tAckti t the sewta the top Write a C++ function twice_the_it ema·.) that, given me alters it so that every ele- ment in the stack now appears twice. For example. if we had 3 719 5 placed on the stack st.k on tht order), it would contain 3 3 7?-19-19 6 6 arter calling twice-the-item (stk Iny ortimy: It is an ADT. don't make any assumptions about the implementation of int-tack_c assumptions about the implementation of intstackt void twice the items (int stackt .istk)

Explanation / Answer

//function to make items twice in stack using given ADT
//EXAMPLE-- contents of istk stack: 3 7 -19 6


void twice_the_items(int_stack_t *istk)
{
//create an empty new stack to store double elements
int_stack_t *temp_stk = create_stack();
  
//pop all elements from given stack & push twice onto new stack using this loop
while(!is_empty_stack(istk))
{
//pop from given stack
int top_element = pop_off_stack(istk);
  
//push twice onto new stack
push_on_stack(temp_stk, top_element);
push_on_stack(temp_stk, top_element);
}
//contents of istk stack: EMPTY
//contents of new stack: 6 6 -19 -19 7 7 3 3
  
  
  
//reverse contents of new stack to get required output
while(!is_empty_stack(temp_stk))
{
int top_element=pop_off_stack(temp_stk);
  
push_on_stack(istk, top_element);
}
//contents of istk stack: 3 3 7 7 -19 -19 6 6
//contents of new stack: EMPTY
  
//destroy new stack
destroy_stack(temp_stk);
  
return;
}