All of the Stack parameters for the functions in lab4c.c are pointers to the Stack structure.
given code
#include <stdio.h>
#include <stdlib.h>
/* typical C boolean set-up */
#define TRUE 1
#define FALSE 0
typedef struct StackStruct
{
int* darr; /* pointer to dynamic array */
int size; /* amount of space allocated */
int inUse; /* top of stack indicator
- counts how many values are on the stack */
} Stack;
void init (Stack* s)
{
s->size = 2;
s->darr = (int*) malloc ( sizeof (int) * s->size );
s->inUse = 0;
}
void push (Stack* s, int val)
{
/* QUESTION 7 */
/* check if enough space currently on stack and grow if needed */
/* add val onto stack */
s->darr[s->inUse] = val;
s->inUse = s->inUse + 1;
}
int isEmpty (Stack* s)
{
if ( s->inUse == 0)
return TRUE;
else
return FALSE;
}
int top (Stack* s)
{
return ( s->darr[s->inUse-1] );
}
/* QUESTION 9.1 */
void pop (Stack* s)
{
if (isEmpty(s) == FALSE)
s->inUse = s->inUse - 1;
}
void reset (Stack* s)
{
/* Question 10: how to make the stack empty? */
}
int main (int argc, char** argv)
{
Stack st1;
init (&st1);
push (&st1, 20);
push (&st1, 30);
push (&st1, 40);
push (&st1, 50);
while ( isEmpty(&st1) == FALSE)
{
int value = top (&st1);
printf ("The top value on the stack is %d\n", value);
/* QUESTION 9.2 */
pop (&st1);
}
printf ("The stack is now empty\n");
}
![Q8
All of the Stack parameters for the functions in lab4c.c are pointers to the Stack structure.
Q8.1
Could any of the functions have been written so that the parameter containing the stack could
have used an instance of the Stack structure instead of a pointer (don't change the return type
to answer this question)?
O Yes, at least one of the stack functions could have the stack parameter be an instance to the
Stack structure.
O No, all of the stack functions need the stack parameter to be a pointer to the Stack structure.
Q8.2
If so, which functions could have been written in this manner?
Q8.3
Explain why your answers to Questions 8.1 and 8.2 are correct.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F672bf286-8abe-4b07-9ca1-0d5b2612956c%2F89c04236-2386-44b2-82ea-e2a9dbf63680%2F236uxjm_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
#include <stdio.h>
#include <stdlib.h>
/* typical C boolean set-up */
#define TRUE 1
#define FALSE 0
typedef struct StackStruct
{
int* darr; /* pointer to dynamic array */
int size; /* amount of space allocated */
int inUse; /* top of stack indicator
- counts how many values are on the stack */
} Stack;
void init (Stack* s)
{
s->size = 2;
s->darr = (int*) malloc ( sizeof (int) * s->size );
s->inUse = 0;
}
void push (Stack* s, int val)
{
/* QUESTION 7 */
/* check if enough space currently on stack and grow if needed */
if(s->inUse+1 >= s->size){ //If whole array is inn use i.e inUse +1 = size
s->size=s->size*2; //Increase the size to 2 times the current
//Using realloc to resize the size of array, it automatically retains previous elemnts
s->darr = (int *)realloc(s->darr,(s->size)*sizeof(int));
}
/* add val onto stack */
s->darr[s->inUse] = val;
s->inUse = s->inUse + 1;
}
int isEmpty (Stack* s)
{
if ( s->inUse == 0)
return TRUE;
else
return FALSE;
}
int top (Stack* s)
{
return ( s->darr[s->inUse-1] );
}
/* QUESTION 9 */
void pop (Stack* s)
{
s->inUse = s->inUse - 1;
}
void reset (Stack* s)
{
s->size=2;
free(s->darr);
s->darr = (int*) malloc ( sizeof (int) * s->size );
s->inUse = 0;
}
int main (int argc, char** argv)
{
Stack st1;
init (&st1);
push (&st1, 20);
push (&st1, 30);
push (&st1, 40);
push (&st1, 50);
while ( isEmpty(&st1) == FALSE)
{
int value = top (&st1);
printf ("The top value on the stack is %d\n", value);
/* QUESTION 10 */
pop (&st1);
}
printf ("The stack is now empty\n");
}
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![C++ Programming: From Problem Analysis to Program…](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
![Systems Architecture](https://www.bartleby.com/isbn_cover_images/9781305080195/9781305080195_smallCoverImage.gif)
![New Perspectives on HTML5, CSS3, and JavaScript](https://www.bartleby.com/isbn_cover_images/9781305503922/9781305503922_smallCoverImage.gif)
![C++ Programming: From Problem Analysis to Program…](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
![Systems Architecture](https://www.bartleby.com/isbn_cover_images/9781305080195/9781305080195_smallCoverImage.gif)
![New Perspectives on HTML5, CSS3, and JavaScript](https://www.bartleby.com/isbn_cover_images/9781305503922/9781305503922_smallCoverImage.gif)
![C++ for Engineers and Scientists](https://www.bartleby.com/isbn_cover_images/9781133187844/9781133187844_smallCoverImage.gif)
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)