Consider the pop() function in lab4c.c. It is correctly written to take the stack parameter as a pointer. Also note that this pop( ) function only removes the top value from the stack by decrementing the inUse value. It does NOT remove the top value AND also return the top value as is often normally done in a pop( ) function. (Writing this function to remove and return the top value would add way too much complexity to this question. So this code was given using a simpler algorithm/variation of pop.)

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

given code lab4

 

#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");
}

Q9.1
Consider the pop() function in lab4c.c. It is correctly written to take the stack parameter as a
pointer. Also note that this pop() function only removes the top value from the stack by
decrementing the inUse value. It does NOT remove the top value AND also return the top
value as is often normally done in a pop() function. (Writing this function to remove and return
the top value would add way too much complexity to this question. So this code was given
using a simpler algorithm/variation of pop.)
Re-write the pop( ) function from lab4c.c to take a Stack instance as a parameter, modify this
stack instance, and then return an instance of type Stack.
The function declaration line is given below for this idea to hopefully clarify this difference in
approach.
Stack pop (Stack s) {
Q9.2
Rewrite the line of code from lab4c.c that would call this modified pop( ) function to reflect the
changes made in Q 9.1.
Q10
In the program lab4c.c, the function body of reset() does not contain any code.
Write the C code that would reset the stack so that it is emptied of all values after the reset( )
function is called.
Transcribed Image Text:Q9.1 Consider the pop() function in lab4c.c. It is correctly written to take the stack parameter as a pointer. Also note that this pop() function only removes the top value from the stack by decrementing the inUse value. It does NOT remove the top value AND also return the top value as is often normally done in a pop() function. (Writing this function to remove and return the top value would add way too much complexity to this question. So this code was given using a simpler algorithm/variation of pop.) Re-write the pop( ) function from lab4c.c to take a Stack instance as a parameter, modify this stack instance, and then return an instance of type Stack. The function declaration line is given below for this idea to hopefully clarify this difference in approach. Stack pop (Stack s) { Q9.2 Rewrite the line of code from lab4c.c that would call this modified pop( ) function to reflect the changes made in Q 9.1. Q10 In the program lab4c.c, the function body of reset() does not contain any code. Write the C code that would reset the stack so that it is emptied of all values after the reset( ) function is called.
Expert Solution
Step 1
#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");
}
steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education