You are on page 1of 2

#include <stdio.

h>
#include <stdlib.h>
#include <limits.h>
typedef struct stack_node{
int data;
struct stack_node* next;
}node;
void push(node **head_ref,int data);
int pop(node **head_ref);
int peek(node *head);
int IsEmpty(node *head);
void reverse_string(char *s);
void conv_to_postfix(char *s);
int ischar(char c);
int close(char c);
int open(char c);
int main(void)
{
int val;
node *head=NULL;
char s[]="VISHY";
char exp[]="a+b-c+d";
val=IsEmpty(head);
if(val!=0) printf("Stack is empty\n");
else printf("Stack is full\n");
push(&head,10);
push(&head,20);
push(&head,30);
printf("%d popped from stack\n",pop(&head));
printf("Top element of the stack: %d\n",peek(head));
reverse_string(s);
printf("Reversed String: %s\n",s);
conv_to_postfix(exp);
printf("Postfix exp: %s\n",exp);
return 0;
}
void push(node **head_ref,int data)
{
node *new_node=(node *)malloc(sizeof(node));
new_node->data=data;
new_node->next=*head_ref;
*head_ref=new_node;
printf("%d pushed on to stack\n",data);
}
int pop(node **head_ref)
{
if(*head_ref==NULL) return INT_MIN;
int data=(*head_ref)->data;
node *temp=*head_ref;
*head_ref=(*head_ref)->next;
free(temp);
return data;
}
int peek(node *head)

{
if(head==NULL) return INT_MIN;
return head->data;
}
int IsEmpty(node *head)
{
if(head==NULL) return 1;
return 0;
}
void reverse_string(char *s)
{
node *head=NULL;
int i;
for(i=0;s[i];++i) push(&head,s[i]);
i=0;
while(!IsEmpty(head))
s[i++]=pop(&head);
}
void conv_to_postfix(char *s)
{
int i,j;
char *q=(char *)malloc(sizeof(s));
node *head=NULL;
for(i=0,j=0;s[i];++i)
{
if(ischar(s[i]))
{
q[j]=s[i];
++j;
}
else if(close(s[i]))
while(!open(s[i])) q[j++]=pop(&head);
else push(&head,s[i]);
}
while(!IsEmpty(head))
q[j++]=pop(&head);
q[j]='\0';
for(i=0;q[i];++i) s[i]=q[i];
}
int ischar(char c)
{
if(c>='a' && c<='z') return 1;
return 0;
}
int close(char c)
{
if(c==')' || c==']' || c=='}') return 1;
return 0;
}
int open(char c)
{
if(c=='(' || c=='[' || c=='}') return 1;
return 0;
}

You might also like