Hey i really need help for this stack project im working on. im just trying to make a simple stack that adds values,in total there will be five values and then im trying to get the stack to pop the values at the top and take in new values at the bottom. Can someone please tell me what im doing wrong heres my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
#define newbos 0
/*create a stack size of 50*/
void push(int i);
/*create a fucntion called push which returns value i*/
int pop (void);
/*create a function called pop which has no return value*/
int *tos, *p1, stack[SIZE],*p2,loop;
/*make tos an address that points to top of stack,p1
as push 1 value, and stack with an array size*/
int main(int argc, char *argv[])
{
int value;
/*create a integer number called value its undeclared ehre*/
tos = stack; /* tos points to the top of stack */
p1 = stack; /* initialize p1 */
/*make top of stack point to int stack*/
do {
printf("Enter value: ");
scanf("%d", &value);
if(value != 0) push(value);
else printf("value on top is %d\n", pop());
} while(value != -1);
/*do enter value and take value until, if value is not = to 0
push (value) or else print value on the top of the stack and pop it*/
return 0;
}
void push(int i)
{
p1++;
if(p1 == (tos+SIZE)) {
printf("Maximum stack size,trying to pop and add one new value.\n");
getch();
if(p1 ==(tos+SIZE)){
p1 == 0;
loop++;
overlap();
printf("value of stack is %i",p1);
}
exit(1);
}
*p1 = i;
}
int overlap()
{
if(loop == 5)
{
*tos == newbos;
}
}
int isempty()
{
if(tos <= 0)
{
printf("stack is empty");
}
}
int pop(void)
{
if(p1 == tos) {
printf("Stack Underflow.\n");
getch();
}
p1--;
return *(p1+1);
}