Question: Programming C fix my code Here is the question: Redo the implementation of your stack. Thanks to it being an encapsulated abstract data type, where

Programming C fix my code

Here is the question:

Redo the implementation of your stack. Thanks to it being an encapsulated abstract data type, where the implementation is separate, you can change without breaking existing code that uses your stack implementation! What you need to do is create a new file double_stack_array.c which, like double_stack_list.c, includes double_stack .h , but implements those three functions like push, pop, using fields instead of a linked list. The rest of the code can be exactly the same! In this case, your struct double_stack_struct will need to contain a double field with "just enough" elements instead of a pointer to a list. In addition, the struct needs to contain a counter that shows which index in the field is the top of the stack at the moment. If you want, instead of using a static field in your struct (eg exdouble data[10] ), you can use a field that is dynamically allocated (using malloc ) instead. If you do it that way, you can also use the realloc function to make the field bigger if needed.

I have to implement a stack using array, by making C and H fil first and link them. if the input is 1 2 3 4 the output should be:

4.0000

3.0000

2.0000

1.0000

Here is my code and it does nothing, I don't get any output ,can you find the error and fix it.

#ifndef DOUBLE_STACK_H #define DOUBLE_STACK_H typedef struct double_stack_struct double_stack; double_stack* create_stack(); int empty(double_stack* stack); int push(double_stack* stack,double d); int pop(double_stack* stack,double*d); double get_value_at_head(double_stack* stack); #endif

------------------

#include  #include  #include "double_stack.h" typedef struct double_stack_struct{ double *Array; int size; int head; }double_stack; double_stack* create_stack(){ double_stack *stack = (double_stack*)malloc(sizeof(double_stack)); stack->head = -1; return stack; } int push(double_stack * stack, double d ){ if(stack->head == stack->size - 1){ printf("Array is full"); } stack->head++; stack->Array[stack->head] = d; return 0; } int empty(double_stack* stack) { return (stack->head == -1) ? 1 : 0; } double get_value_at_head(double_stack* stack) { if(!empty(stack)) { return stack->Array[stack->head]; } return -1; } int pop(double_stack* stack,double *d) { if(stack->head == -1){ printf("stack is empty "); return -1; } *d = stack->Array[stack->head--]; return 0; } 

--------------------------------------------

Here it shuold not be any changes because I'm using it for another task aslo:

#include  #include  #include "double_stack.h" int main(int argc, char*argv[]){ double_stack*stack; stack = create_stack(); for(int i = 1; i < argc; i++){ double data = atof(argv[i]); push(stack,data); } double d; while(pop(stack,&d)){ printf("%.6f ",d); } return 0; }

the only changes I want here that If I don't get any argument the it should return some error.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!