3. (20pt) Recall the stack ADT. Followings are the important things from its int
ID: 3735273 • Letter: 3
Question
3. (20pt) Recall the stack ADT. Followings are the important things from its interface stack.h #ifndefstackh - - define _stack h #include "genlb.h" typedef void *stackElementT typedef struct stackCDT stackADT; void Push (stackADT stack, stackElementT element) stackElementT Pop (stackADT stack) endif Please note that the stackElementT is void *. But you need to push/pop double values in your application. In this case, you need to first dynamically allocate memory space for your data, save your data at that space, and push its address. When you need the data, you can pop its address, dereference it and get the value, then free the memory space. Instead of doing these things whenever needed, you can simply implement the following two functions that can push and pop a double value as described above, then you can call them when needed. (Don't forget to check if memory is allocated or not in push, and to free it in pop) void myPushDouble (stackADT stack, double value) // 14pt double *ptr; double myPopDouble (stackADT stack) // 6pt double tmp, *ptr;Explanation / Answer
void myPushDouble(stackADT stack, double value) {
// allocating memory for double
double *ptr = (double *)malloc(sizeof(double));
*ptr = value; // storing the value
stack.Push(stack, ptr);
}
double myPopDouble(stackADT stack) {
// getting void pointer
stackElementT = Pop(stack);
// typecasting
double *ptr = (double *)stackElementT;
return *ptr;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.