-
dynamic stack
Hello Please i need help with this code. I am not sure of what i did wrong but it does not generate the required result, and I am trying to do it without using a struct. Can someone help me figure this out? Thanks
Here are the instructions:
The difference with a dynamic stack is that adds never fail. If the stack is full when you attempt toadd a new item, you create a new array with twice the capacity of the current array, copy all the existingitems to the new array and then the new item, free the memory of the old array, set the stack pointer tothe new array and adjust the top indicator as always. Similarly, if after a remove from the stack, the stackis less than half full, then you shrink the array in half.
write a C program which satisfies the following:
• Your program must dynamically allocate the memory for a dynamic stack - using the appropriatefunction listed below.
• Your program must dynamically free this memory before termination.
• Your C program must declare and define functions to:– Create an empty stack (of float values) with a capacity of 1– Test to see if the stack is empty– Test to see if a stack is less than half full– Test to see if the stack is full– Push (add) an item (a float value) onto the stack– Pop (remove) an item (a float value) from the stack - if not already empty
• Your C program must define a main function to fully test all of the above functionality.
Notes:
1. A stack is really three separate data items:(a) a float pointer to the array(b) an int top index indicator(c) an int capacity (physical size of the array) - note this must be a variable this time - since itwill change over time
2. The effective size of the stack (number of items in the stack) can be deduced from the top indexindicator.
And here is my code so far:
Code:
#include <stdio.h>
#include <stdlib.h>
float *stack;
int top = -1;
int full(int top, int cap)
{
return top == cap - 1;
}
int less_half_full(float *stack, int top, int cap)
{
if (*stack == cap / 2)
return top;
}
int empty(int top)
{
return top == -1;
}
void push(float *stack, int top, int cap, int value)
{
if (top >= cap - 1) {
printf("\nStack is full\n");
return;
}
top = top + 1;
stack[top] = value;
printf("\nPushed %f into the stack!!\n", stack[top]);
}
int pop(float *stack, int top)
{
if (top == -1) {
printf("\nStack is not full!!\n");
return 0;
}
printf("\nPopped %f from the stack!!\n", stack[top]);
stack[top] = 0;
top--;
return 0;
}
void display(float *stack, int top)
{
printf("\nThe values in the stack are:\n");
int i;
for (i = 0; i <= top; i++) {
printf("%f\n", stack);
}
}
int main()
{
int top = -1, cap = 4, value, choice, f = 0, f1 = 0, f2 = 0;
/*printf("\nEnter the size of the stack:"); fflush(stdout);
scanf("%d", &cap); */
stack = malloc(sizeof(int) * cap);
while (1) {
printf
("\n1. Is stack empty?\n2. Is stack full?\n3. Is stack half full?\n4. Push\n");
fflush(stdout);
printf("5. Pop\n6. Display\n7. Exit\n Enter your choice:");
fflush(stdout);
scanf("%d", &choice);
switch (choice) {
case 1:
f = empty(top);
if (f == 1)
printf("\nStack is Empty\n");
else
printf("\nStack is Not Empty\n");
break;
case 2:
f1 = full(top, cap);
if (f1 == 1)
printf("\nStack is Full\n");
else
printf("\nStack is Not Full\n");
break;
default:
printf("\nWrong Option!!\n");
break;
case 3:
f2 = less_half_full(stack, top, cap);
if (f2 == 1)
printf("\nStack is less than half full\n");
else
printf("\nStack is not less than half full\n");
break;
case 4:
printf("\nEnter value to push:");
fflush(stdout);
scanf("%d", &value);
push(stack, cap, top, value);
break;
case 5:
pop(stack, top);
break;
case 6:
display(stack, top);
break;
case 7:
exit(0);
break;
free(stack);
}
}
return 0;
}
-
> 1. A stack is really three separate data items
So why don't you have
Code:
struct stack {
float *data;
int capacity;
int top;
};
void init ( struct stack *s ) {
}
void cleanup ( struct stack *s ) {
}
....
Your main only has
Code:
int main ( ) {
struct stack mystack;
init(&mystack);
// a bunch of other method calls, all passing &mystack
}
-
What input is being used?
What is the expected output?
How does the real output differ?
Tim S.
-
Why is your stack of floats using this code?
Code:
stack = malloc(sizeof(int) * cap);
Why do you think "Stack is not full!!" is a error like condition?
Code:
if (top == -1) {
printf("\nStack is not full!!\n");
return 0;
}
Why do you not wish to use an C structure?
NOTE: Your code can not work unless use pass by address, global variables, or structures!
Tim S.
-
- The input is the first value pushed in because the stack is originally empty with a capacity of one.
- When the stack gets full the program has to create another stack whose capacity is twice that of the previous one, so in that sense i guess stack is never full
- the output is a list of choices asking the user to select if the stack is full, empty, less than half full, if the user wants to pop, shrink or display the values.
- If after popping the stack is less than half full the code should shrink the stack to half its capacity.
Malloc is used for memory allocation of the stack. And I think i will use pass by address. Thanks!
-
C only has pass by value semantics. If the value passed is an address it's an address. No need to complicate things.