hey guys i'm trying to write a code using stacks in C
the code should recognize the parenthesis if it closed or opened or not
I've done the greater part of the code, but I'm so confused .. I have to
submit it after 4 hours .. please I need the best programmer to help. its very emergent !!!
#include <stdio.h>
struct stack{
int top;
char items[50];
};
void initStack (struct stack *);
int is_full(struct stack *);
int is_empty(struct stack*);
void push (struct stack *, char);
char pop (struct stack *);
char topstack (struct stack *);
int prcd (char, char);
int main (void)
{
struct stack s;
s=initStack (&s); // initialize the stack
char valid;
valid =true;
while( not read entire string)
{
read the next symbol (symb) of the string
if(symb = = '(' || symb = = '[' || symb = = '{' )
push(&s, symb)
if(symb = = ')' || symb = = ']' || symb = = '}' )
if(empty_stack)
valid = false
else{
i = pop(&s)
if( i is not the matching opener of symb)
valid = false
}
}
if(!empty_stack)
valid = false
if(valid)
printf("String is valid");
else
printf"String is not valid (invalid)");
return 0;
}
char topstack (struct stack *ps)
{
return ps->items [ps->top];
}
int prcd (char a, char b)
{
if ( (a=='+' && b=='*')||(a=='+' && b=='/')||(a=='-' && b=='*')||(a=='-' && b=='/'))
return 0;
else
return 1;
}
//-----------------------(Function 3)--------------------------------------…
//START
int is_empty(struct stack *ps)
{
if(ps->top == -1)
return 1;
else
return 0;
}
//END
//-----------------------(Function 4)--------------------------------------…
//START
int is_full(struct stack *ps)
{
if(ps->top == 49)
return 1;
else
return 0;
}
//END
//----------------------(Function 5)--------------------------------------…
void push(struct stack *ps, char x)
{
if(is_full(ps)) //if(is_full(&(*ps))… //if (ps->top==STACKSIZE -1)
{
printf("Underflow: Stack is empty");
return ;
}
ps->top++;
ps->items[ps->top]=x;
}
//----------------------(Function 6)--------------------------------------…
char pop(struct stack *ps)
{
if(is_empty(ps)==1) //if(ps->top==-1)
{
printf("Underflow: Stack is empty");
return 1;
}
return ps->items[(ps->top)--];
}
//------------------------------------…
void initStack(struct stack *ps)
{
ps->top = - 1;
}