4) Modify the “stack.c” program we looked at in class (available on D2L) such th
ID: 3811779 • Letter: 4
Question
4) Modify the “stack.c” program we looked at in class (available on D2L) such that the variable “top” is a pointer to the “contents” array rather than a regular variable. The pointer should point to the current array element that represents the top of the stack, and should change as values are added and removed. Include a screenshot of the program running (“push” a few values to the stack, “pop” a value or two out of the stack, then display the contents).
Code
#include <stdio.h>
#define SIZE 5
int contents[SIZE]; //data array
int top = 0; //variable to indicate top of stack
void push(int val)
{
if (top >= SIZE) //if currently at top of stack
printf("Error - stack full ");
else
contents[top++] = val; //put value into array, then increment top indicator
}
int pop(void)
{
if (top <=0) //if currently at bottom of stack
printf("Error - bottom of stack ");
else
return contents[--top]; //decrement top indicator, then return value at that location
}
int main(void)
{
int choice = 0;
int value, i;
while (choice != 4) //repeat until choice is 4 (end)
{
printf("1)push 2)pop 3)display 4)end: ");
scanf("%d", &choice); //input choice
printf(" ");
switch (choice)
{
case 1:
printf("Push value: ");
scanf("%d", &value);
push(value); //call push function, send value
break;
case 2:
value = pop(); //call pop function, retrieve value
printf("Pop value = %d ", value);
break;
case 3:
printf("Contents: ");
for (i=top-1; i>=0; i--) //display each value in stack
printf("%d ", contents[i]);
break;
case 4:
break;
default:
printf("Invalid input: ");
}
printf(" ");
}
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int contents[SIZE]; //data array
int *top = contents;
int size = 0;
void push(int val)
{
if (size >= SIZE) //if currently at top of stack
printf("Error - stack full ");
else
{
*top = val; // put value into array, then increment top indicator
top++;
size++;
}
}
int pop(void)
{
if (size <=0) //if currently at bottom of stack
{
printf("Error - bottom of stack ");
return -1;
}
else
{
int popped = *top;
top = top -1; //decrement top indicator, then return value at that location
size--;
return popped;
}
}
int main(void)
{
int choice = 0;
int value, i;
while (choice != 4) //repeat until choice is 4 (end)
{
printf("1)push 2)pop 3)display 4)end: ");
scanf("%d", &choice); //input choice
printf(" ");
switch (choice)
{
case 1:
printf("Push value: ");
scanf("%d", &value);
push(value); //call push function, send value
break;
case 2:
value = pop(); //call pop function, retrieve value
if (value != -1)
printf("Pop value = %d ", value);
break;
case 3:
printf("Contents: ");
for (i=size-1; i>=0; i--) //display each value in stack
printf("%d ", contents[i]);
break;
case 4:
break;
default:
printf("Invalid input: ");
}
printf(" ");
}
return 0;
}
Sample run
1)push 2)pop 3)display 4)end: 1
Push value: 5
1)push 2)pop 3)display 4)end: 1
Push value: 6
1)push 2)pop 3)display 4)end: 1
Push value: 8
1)push 2)pop 3)display 4)end: 1
Push value: 9
1)push 2)pop 3)display 4)end: 1
Push value: 7
1)push 2)pop 3)display 4)end: 1
Push value: 8
Error - stack full
1)push 2)pop 3)display 4)end: 3
Contents:
7
9
8
6
5
1)push 2)pop 3)display 4)end: 2
Pop value = 0
1)push 2)pop 3)display 4)end: 2
Pop value = 7
1)push 2)pop 3)display 4)end: 2
Pop value = 9
1)push 2)pop 3)display 4)end: 2
Pop value = 8
1)push 2)pop 3)display 4)end: 2
Pop value = 6
1)push 2)pop 3)display 4)end: 2
Error - bottom of stack
1)push 2)pop 3)display 4)end: 2
Error - bottom of stack
1)push 2)pop 3)display 4)end: 2
Error - bottom of stack
1)push 2)pop 3)display 4)end: 4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.