I have a C program that implements a stack.
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *link;
};
struct stack{
struct node *head;
struct node *data_node;
};
int push(struct stack *a_stack, int i){
a_stack->data_node = malloc(sizeof(struct node));
if(a_stack->data_node == NULL){
puts("Error: Cannot allocate sufficient memory.");
exit(1);
}
a_stack->data_node->data = i;
a_stack->data_node->link = a_stack->head;
a_stack->head= a_stack->data_node;
return 0;
}
int pop(struct stack *a_stack){
if(a_stack->head==NULL){
return '\n';
}
int temp = a_stack->head->data;
a_stack->data_node = a_stack->head;
a_stack->head = a_stack->head->link;
free(a_stack->data_node);
return temp;
}
int minimum(struct stack *a_stack){
if(a_stack->head==NULL){
return '\n';
}
int min = a_stack->head->data;
struct node *a_node = a_stack->head;
while(a_node!=NULL){
if(min>a_node->data){
min = a_node->data;
a_node = a_node->link;
}
}
return min;
}
int init_stack(struct stack *a_stack){
a_stack->head = NULL;
a_stack->data_node = NULL;
}
int handle_input(struct stack *test){
char* input_string = (char*)malloc(20);
scanf("%s", input_string);
// gets(input_string);
char* pop_cmd = "-";
char* min_cmd = "min";
int num;
if (strcmp(pop_cmd, input_string) == 0){
printf("%d\n", pop(test));
}
else{
if (input_string[0] == 'm'){
printf("%d\n", minimum(test));
}
else{
num = atoi(input_string);
push(test, num);
}
}
return 0;
}
int main(void){
int no_of_input, counter;
struct stack test;
init_stack(&test);
scanf("%d", &no_of_input);
for(counter=no_of_input; counter>0; counter=counter-1){
handle_input(&test);
};
return 0;
}
The problem is if I want to enter 'min' which is the command for calculating the minimum element of the array, the program waits forever on input. After searching around for quite a while I still have no idea why this is happening.
The scanf don't wait but you have infinite loop problem. In function minimum()
, you only conditionally update a_node
to next node in linked list:
int min = a_stack->head->data; //note
struct node *a_node = a_stack->head; //note
while(a_node!=NULL){
if(min > a_node->data){<-- "Always evaluates FALSE because: min is a_node->data"
min = a_node->data;
a_node = a_node->link; <--"Should NOT be here"
}
a_node = a_node->link; <--"but it should be here"
}
Also, if
condition (min > a_node->data)
is always evaluates false
because of the reason:
min
is a_stack->head->data
and a_node
is a_stack->head
so min == a_node->date
and min > a_node->data
always evaluates false
because you updated a_node
in if
body.
Additionally I figured out that you have memory leak in the function handle_input()
. You should free()
dynamically allocated memory explicitly. Read my suggestion below:
int handle_input(struct stack *test){
char* input_string = malloc(20); <-- "No need to type case"
// code here
free(input_string); <-- "Add this"
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With