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

Create a program that creates a stack. You must push numbers 1-10 onto the stack

ID: 3537686 • Letter: C

Question


Create a program that creates a stack. You must push numbers 1-10 onto the stack, pop the numbers off the stack, and display the result.

You must also include a screen shot of the program running

Create a program that creates a stack. You must push numbers 1-10 onto the stack, pop the numbers off the stack, and display the result.

You must also include a screen shot of the program running

Explanation / Answer

include struct two_stacks { int first_stack_top; int second_stack_top; int size; int * array; }; void push(struct two_stacks * ts, int insert_value, int stack_num) { if(stack_num==1) { if(ts->first_stack_top+1==ts->second_stack_top) { printf("ERROR: two stack array full! can't have any more elements! "); return; } else { ts->first_stack_top++; ts->array[ts->first_stack_top]=insert_value; } } else if(stack_num==2) { if(ts->second_stack_top-1==ts->first_stack_top) { printf("ERROR: two stack array full! can't have any more elements! "); return; } else { ts->second_stack_top--; ts->array[ts->second_stack_top]=insert_value; } } } int pop(struct two_stacks * ts, int stack_num) { int popped_element; if(stack_num==1) { if(ts->first_stack_top==-1) { printf("ERROR: first two stack array empty! can't pop an element! "); return -999; } else { popped_element=ts->array[ts->first_stack_top]; ts->array[ts->first_stack_top]=-999; ts->first_stack_top--; return popped_element; } } else if(stack_num==2) { if(ts->second_stack_top==ts->size) { printf("ERROR: second two stack array empty! can't pop an element! "); return -999; } else { popped_element=ts->array[ts->second_stack_top]; ts->array[ts->second_stack_top]=-999; ts->second_stack_top++; return popped_element; } } } int main(){ struct two_stacks ts; ts.size=10; ts.array=(int *)calloc(sizeof(int), ts.size); ts.first_stack_top=-1; ts.second_stack_top=ts.size; push(&ts, 8, 1); pop(&ts, 2); pop(&ts, 1); push(&ts, 8, 2); pop(&ts, 2); return 0; }
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