Sunteți pe pagina 1din 9

CHECKING BALANCED PARANTHESES USING

ARRAY IMPLEMENTATION OF STACK


#include<conio.h>
#include<stdio.h>
#define TRUE 1
#define FALSE 0
void push(char);
void pop();
int empty();
struct stack
{
int top;
char paren[20];
};
struct stack s;
void main()
{
char instr[20];
int valid=TRUE,i=0;
clrscr();
s.top=-1;
printf("enter the arithmetic expression");
scanf("%s",instr);
while(instr[i]!='\0')
{
if(instr[i]=='(')
push(instr[i]);
if(instr[i]==')')
{
if(empty())
{
valid=FALSE;
break;
}
else
{
pop();
}
}
i++;
}
if(!empty())
valid=FALSE;
if(valid)
printf("\n The given expression is valid");
else
printf("\n The given expression is invalid");
getch();
}
void push(char n)
{ s.top++;
s.paren[s.top]=n;
}
void pop()
{
s.top--;
}
int empty()
{ if(s.top==-1)
return TRUE;
else
return FALSE;
}

OUTPUT:
ENTER THE EXPRESSION : (((())))
VALID

ENTER THE EXPRESSION : (((()))


INVALID
CHECKING BALANCED PARANTHESES USING
LINKED LIST IMPLEMENTATION OF STACK

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
struct Node;
typedef struct Node *PtrToNode;
typedef struct Node *Stack;
void push(Stack S,char);
void pop(Stack S);
int empty(Stack S);
struct Node
{
char Element;
PtrToNode Next;
};
Stack S;
void main()
{
char instr[20];
int valid=TRUE,i=0;
S=(Stack)malloc(sizeof(struct Node));
S->Next=NULL;
printf("enter the arithmetic expression");
scanf("%s",instr);
while(instr[i]!='\0')
{
if(instr[i]=='(')
push(S,instr[i]);
if(instr[i]==')')
{
if(empty(S))
{
valid=FALSE;
break;
}
else
{
pop(S);
}
}
i++;
}

if(!empty(S))
valid=FALSE;
if(valid)
printf("\n The given expression is valid");
else
printf("\n The given expression is invalid");
getch();
}

void push(Stack S,char n)


{
PtrToNode TmpCell;
TmpCell=(Stack)malloc(sizeof(struct Node));
TmpCell->Next=S->Next;
TmpCell->Element= n;
S->Next=TmpCell;
}

void pop(Stack S)
{
PtrToNode FirstCell;
FirstCell=S->Next;
S->Next=S->Next->Next;
free(FirstCell);
}

int empty(Stack S)
{
if(S->Next==NULL)
return TRUE;
else
return FALSE;
}

OUTPUT:

$./a.out

Enter an arithmetic expression


(A+(B+C)*D)
The given expression is valid.

$./a.out

Enter an arithmetic expression


(A+(B*(C-D))
The given expression is invalid.

EVALUATION OF POSTFIX EXPRESSION USING


ARRAY IMPLEMENTATION OF STACK

#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<stdlib.h>
struct stack
{
int top;
int operand[40];
};
void push(int);
void pop();
struct stack s;
void main()
{
char instr[40];
int i=0,result,op1,op2;
int value;
s.top=-1;
clrscr();
printf("Enter an expression in postfix form\n");
while((instr[i]=getchar())!='\n')
{
if(isdigit(instr[i]))
{
printf("Pushing...%d\n",instr[i]-'0');
push(instr[i]-'0');
}
else
{
op2=s.operand[s.top];
pop();
printf("Popping....%d\n",op2);
op1=s.operand[s.top];
pop();
printf("Popping ....%d\n",op1);
printf("Applying operator %c\n",instr[i]);
switch(instr[i])
{
case '+':
result=op1+op2;
break;
case '-':
result=op1-op2;
break;
case '*':
result=op1*op2;
break;
case '/':
result=op1/op2;
break;
}
printf("\n Pushing intermediate Result ....%d\n",result);
push(result);
}
i++;
}
printf("\n Value of postfix expression is %d\n",result);
getch();

void push(int n)
{
s.top++;
s.operand[s.top]=n;
}

void pop()
{
s.top--;
}

OUTPUT:

$
Enter an expression in postfix form42-23+*
Pushing...4
Pushing...2
Popping....2
Popping ....4
Applying operator -

Pushing intermediate result.......2


Pushing...2
Pushing...3
Popping....3
Popping ....2
Applying operator +

Pushing intermediate result.......5


Popping....5
Popping ....2
Applying operator *

Pushing intermediate result.......10

Value of postfix expression is 10

EVALUATION OF POSTFIX EXPRESSION USING


LINKED LIST IMPLEMENTATION OF STACK

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
struct Node;
typedef struct Node *PtrToNode;
typedef struct Node *Stack;
void push(Stack S,char);
void pop(Stack S);
struct Node
{
char Element;
PtrToNode Next;
};
Stack S;
void main()
{
char instr[40];
int i=0,result,op1,op2;
int value;
clrscr();
S=(Stack)malloc(sizeof(struct Node));
S->Next=NULL;
printf("Enter an expression in postfix form\n");
scanf("%s",instr);
while(instr[i]!='\0')
{
if(isdigit(instr[i]))
{
printf("Pushing...%d\n",instr[i]-'0');
push(S,instr[i]-'0');
}
else
{
op2=S->Next->Element;
pop(S);
printf("Popping....%d\n",op2);
op1=S->Next->Element;
pop(S);
printf("Popping ....%d\n",op1);
printf("Applying operator %c\n",instr[i]);
switch(instr[i])
{
case '+':
result=op1+op2;
break;
case '-':
result=op1-op2;
break;
case '*':
result=op1*op2;
break;
case '/':
result=op1/op2;
break;
}
printf("\n Pushing intermediate result.......%d\n",result);
push(S,result);
}
i++;
}
printf("\n Value of postfix expression is %d\n",result);
getch();
}

void push(Stack S,char n)


{
PtrToNode TmpCell;
TmpCell=(Stack)malloc(sizeof(struct Node));
TmpCell->Next=S->Next;
TmpCell->Element= n;
S->Next=TmpCell;
}

void pop(Stack S)
{
PtrToNode FirstCell;
FirstCell=S->Next;
S->Next=S->Next->Next;
free(FirstCell);
}

OUTPUT:

$ ./a.out
Enter an expression in postfix form23*23*+
Pushing...2
Pushing...3
Popping....3
Popping ....2
Applying operator *

Pushing intermediate result.......6


Pushing...2
Pushing...3
Popping....3
Popping ....2
Applying operator *

Pushing intermediate result.......6


Popping....6
Popping ....6
Applying operator +

Pushing intermediate result.......12

Value of postfix expression is 12

S-ar putea să vă placă și