The following program use an interger stack. Which value the function int pop() can return when the stack is empty, if I want to avoid using isEmpty() before calling the pop() ?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STK_SIZE 100
#define INSTR_SIZE 5
int push(int * stack,int * stk_top,int operand);
int pop(int * stack,int * stk_top);
int main()
{
int n_instr=0;
int operand=0;
int stack[STK_SIZE];
int stk_top = -1;
char line[INSTR_SIZE+5];
FILE * fp = fopen("instructions.txt", "r");
fgets(line, sizeof(line), stdin);
n_instr = atoi(line);
for(int i=0;i<n_instr;i++)
{
fgets(line, sizeof(line), stdin);
char * opcode = strtok(line, " \n");
if(!strcmp(opcode,"PUSH")){
operand = atoi(strtok(NULL, " "));
push(stack,&stk_top,operand);
}
else if(!strcmp(opcode,"POP")){
printf("%d\n",pop(stack,&stk_top));
}
else if(!strcmp(opcode,"ADD")){
int n1 = pop(stack,&stk_top);
int n2 = pop(stack,&stk_top);
push(stack,&stk_top,n1 + n2);
}
else if(!strcmp(opcode,"SUB")){
int n1 = pop(stack,&stk_top);
int n2 = pop(stack,&stk_top);
push(stack,&stk_top,n2 - n1);
}
}
fclose(fp);
while(stk_top>=0)printf("%d\n",pop(stack,&stk_top));
return 0;
}
int push(int * stack,int * stk_top,int operand){
if(*stk_top<STK_SIZE-1){
(*stk_top)++;
stack[*stk_top]=operand;
return 0;
}
return 1;
}
int pop(int * stack,int *stk_top){
if(*stk_top>-1){
int n = stack[*stk_top];
(*stk_top)--;
return n;
}
return 0;
}
I use the above code for simulating the machine language instructions PUSH and POP ,ADD and SUB.For each POP instruction, output the value that was popped from the stack. After processing all instructions, print the final content of the stack, from the top to the bottom, one value per line.
Tried returning Integer values but it confuses with stack content, is there any work around ?
fgets()return a value (an input string) while it can also return whether or not the operation succeeded?"popfunction might return a pointer to the stack! If nothing is in the stack it returns null! Otherwise you shall use something as isEmpty() or a control variable but in this case you should control the stack situation before to callpop! On the other hand, also returning a null pointer if the stack is empty you have to control the result after thepopcall.stackarray, you would simply have amemoryarray for all memory which an SP can address, in which the stack-pointer were an index using modulo-n where n is the memory size.popoperation on the stack adapter doesn't return anything. It's on you to grabtop, thenpop, and do neither unlessemptyis false. top'ing or pop'ing an empty stack is considered a runtime error condition. Don't treat it as a workflow option; treat it for what it is: an error.