I'm trying to implement a stack to solve parenthesis problem, but i'm getting confused with the pointers(i'm new to C) here is my code so far:
typedef struct node{
char data;
struct node *next;
}node;
typedef struct stack{
struct node **head;
}stack;
void push(stack *s, char c){
node *n = (node *)malloc(sizeof(node));
n->data = c;
if(*s->head == NULL){
n->next = *s->head;
*s->head = n;
}
else{
n->next = *s->head;
*s->head = n;
}
}
void pop(stack *s){
if(s->head->next == NULL){
*s->head = NULL;
}
else{
node *temp = *s->head;
*s->head = s->head->next;
free(temp);
}
}
bool isStringBalanced(char** sentence, int size){
stack *s = malloc(sizeof(stack));
*s->head = NULL;
for(int i = 0; i < size; i++){
int j = 0;
while(sentence[i][j] != '\0'){
switch(sentence[i][j]){
case '(' : push(s, '('); break;
case '[' : push(s, '['); break;
case '{' : push(s, '{'); break;
case ')' :
if(*s->head)
if(*s->head->data == '(')
pop(s);
else return false;
else return false;
break;
case ']' :
if(*s->head)
if(*s->head->data == '[')
pop(s);
else return false;
else return false;
break;
case '}' :
if(*s->head)
if(*s->head->data == '{')
pop(s);
else return false;
else return false;
break;
}
j++;
}
}
if(!s->head)
return true;
else
return false;
}
when i try to run this, i get error on all the double arrows like s->head->data and s->head->next Help me to understand how to use right double pointer thanks.
sis defined asstack *sandstackis a struct, you would use eithers->memberor(*s).member. Refer to the C language reference for details.heada pointer-to-a-pointer?stack, I don't think you needstruct node **head;; just a single indirection would dostruct node *head;. Furthermore, because of this, I think you are getting confused by the two different ways to referenceheadandnext.s->head->next, it would only work if head is node*, not node** like you have.