From 899b8c47155e164b2412352d4b801badad2c1101 Mon Sep 17 00:00:00 2001 From: RatulGhosh Date: Mon, 4 Apr 2016 00:37:21 +0530 Subject: [PATCH 1/2] infix-postfix --- infixToPostfix.c | 386 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 368 insertions(+), 18 deletions(-) diff --git a/infixToPostfix.c b/infixToPostfix.c index e603209..97cae49 100644 --- a/infixToPostfix.c +++ b/infixToPostfix.c @@ -13,7 +13,6 @@ #include #define N 100 - typedef struct stack { int top; @@ -22,13 +21,12 @@ typedef struct stack bool empty(stack *ps) { - if(ps->top==0) + if(ps->top==-1) return true; else return false; } - void push(stack *ps, int x) { if(ps->top==N-1) @@ -66,7 +64,6 @@ void print(stack *ps,int d) printf("\n"); } - bool isoperand(char c) { switch(c) @@ -82,8 +79,6 @@ bool isoperand(char c) } } - - int pr ( char a) { int c; @@ -93,19 +88,200 @@ int pr ( char a) else if(a=='*'||a=='/') c=2; return c; - } bool prcd(char a, char b) { + // returns true if precedence of char a is more than that of b + if (pr(a) >= pr(b)) { + return true; + } + return false; +} - /* returns true if precedence of char a is more than that of b*/ +int main() +{ + int z; + scanf("%d",&z); + while(z--) + { + char a; + stack s; + char str[100][100]={'\0'}; + char si[100][100]={'\0'}; + int f=0; + int o=0; + s.top=-1; + int j=0,k=0,i; + while(1) + { + a=getchar(); + if(a==' ') + { + j=0; + k++; + } + else if(a=='?') + { + break; + } + else + { + str[k][j++]=a; + } + } + for(i=0;i +#include +#include +#include +#define N 100 +typedef struct stack +{ + int top; + char A[N]; +}stack; +bool empty(stack *ps) +{ + if(ps->top==-1) + return true; + else + return false; } +void push(stack *ps, int x) +{ + if(ps->top==N-1) + { + printf("Overflow\n"); + return; + } + (ps->top)++; + ps->A[(ps->top)]=x; +} +char pop(stack *ps) +{ + if(empty(ps)) + { + printf("Underflow\n"); + return ; + } + char a = ps->A[ps->top]; + ps->top--; + return a; +} +void print(stack *ps,int d) +{ + int t= ps->top; + while(t>=0) + { + if(d) + printf("%d ",ps->A[t]); + else + printf("%c ",ps->A[t]); + t--; + } + printf("\n"); +} + +bool isoperand(char c) +{ + switch(c) + { + case '+' : return true; + case '-' : return true; + case '*' : return true; + case '/' : return true; + case '^' : return true; + case '(' : return true; + case ')' : return true; + default : return false; + } +} + +int pr ( char a) +{ + int c; + + if(a=='+'||a=='-') + c=1; + else if(a=='*'||a=='/') + c=2; + return c; +} + +bool prcd(char a, char b) +{ + // returns true if precedence of char a is more than that of b + if (pr(a) >= pr(b)) { + return true; + } + return false; +} int main() { @@ -115,8 +291,8 @@ int main() { char a; stack s; - char str[100][100]={'\0'}; - char si[100][100]={'\0'}; + char str[100][100]={'\0'}; + char si[100][100]={'\0'}; int f=0; int o=0; s.top=-1; @@ -138,14 +314,12 @@ int main() str[k][j++]=a; } } - - - for(i=0;i +#include +#include +#include +#define N 100 +typedef struct stack +{ + int top; + char A[N]; +}stack; +bool empty(stack *ps) +{ + if(ps->top==-1) + return true; + else + return false; +} - +void push(stack *ps, int x) +{ + if(ps->top==N-1) + { + printf("Overflow\n"); + return; + } + (ps->top)++; + ps->A[(ps->top)]=x; } +char pop(stack *ps) +{ + if(empty(ps)) + { + printf("Underflow\n"); + return ; + } + char a = ps->A[ps->top]; + ps->top--; + return a; +} +void print(stack *ps,int d) +{ + int t= ps->top; + while(t>=0) + { + if(d) + printf("%d ",ps->A[t]); + else + printf("%c ",ps->A[t]); + t--; + } + printf("\n"); +} +bool isoperand(char c) +{ + switch(c) + { + case '+' : return true; + case '-' : return true; + case '*' : return true; + case '/' : return true; + case '^' : return true; + case '(' : return true; + case ')' : return true; + default : return false; + } +} +int pr ( char a) +{ + int c; + if(a=='+'||a=='-') + c=1; + else if(a=='*'||a=='/') + c=2; + return c; +} +bool prcd(char a, char b) +{ + // returns true if precedence of char a is more than that of b + if (pr(a) >= pr(b)) { + return true; + } + return false; +} +int main() +{ + int z; + scanf("%d",&z); + while(z--) + { + char a; + stack s; + char str[100][100]={'\0'}; + char si[100][100]={'\0'}; + int f=0; + int o=0; + s.top=-1; + int j=0,k=0,i; + while(1) + { + a=getchar(); + if(a==' ') + { + j=0; + k++; + } + else if(a=='?') + { + break; + } + else + { + str[k][j++]=a; + } + } + for(i=0;i Date: Mon, 4 Apr 2016 00:52:45 +0530 Subject: [PATCH 2/2] header file --- linkedlist.h | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/linkedlist.h b/linkedlist.h index 2107b7e..9c67ea8 100644 --- a/linkedlist.h +++ b/linkedlist.h @@ -3,6 +3,7 @@ #define llist linkedlist #include +#include /* overview : struct node ; @@ -33,15 +34,15 @@ struct llist struct linkedlist* createList() { - struct llist *x = malloc(sizeof(struct llist); + struct llist *x = malloc(sizeof(struct llist)); x->head = NULL; return x; } -struct node newnode (int value) +struct node* newnode (int value) { struct node *x = malloc(sizeof(struct node)); - x.item = value; + x->item = value; return x; } @@ -53,28 +54,28 @@ void insertatBeg(struct llist *l, int value) } void insertAtend(struct llist *l, int value) -{; +{ struct node *n = newnode(value); n->next = NULL; if(l->head != NULL) { l->head = n; - return 0; + return ; } struct node *tmp = l->head; - while(temp-next != NULL) + while(tmp->next != NULL) { - temp = temp->next; + tmp = tmp->next; } - temp->next = n; + tmp->next = n; } void insertAfter(struct llist *l, int value, int key) { struct node *temp = l->head; - if (l->head = NULL) + if (l->head == NULL) { printf("List is empty. \n"); - return 1; + return ; } while (temp != NULL) { @@ -95,19 +96,19 @@ void insertAfter(struct llist *l, int value, int key) int delAtBeg(struct llist *l) { - if(l->head = NULL) + if(l->head == NULL) { printf("List is already empty. \n"); return -1; } int x = l->head->item; - struct node *temp = l.head; + struct node *temp = l->head; l->head = temp->next; free(temp); return x; } -int delAtEnd(struct llist *l +int delAtEnd(struct llist *l) { int x; if (l->head == NULL) @@ -121,7 +122,7 @@ int delAtEnd(struct llist *l return delAtBeg(l); } struct node *temp1 = temp->next; - while (temp1->item != NULL) + while (temp1->next != NULL) { temp = temp->next; temp1 = temp1->next; @@ -153,7 +154,7 @@ int delAfter (struct llist *l, int key) return -1; } } - struct node *temp1; = temp->next; + struct node *temp1 = temp->next; while (temp1->next != NULL && temp->item!=key) { temp = temp->next; @@ -166,15 +167,15 @@ int delAfter (struct llist *l, int key) } x = temp1->item; temp->next = temp1->next; - return x + return x; } int count (struct llist *l) { int ctr = 0; struct node *temp = l->head; - while (temp == NULL) // { - temp = tmp->next; + while (temp == NULL) { + temp = temp->next; ctr += 1; } return ctr++; @@ -202,7 +203,6 @@ void reverse(struct llist *l) cur->next = prev; prev = cur; cur = next; - //} - l>head = prev; -} - + } + l->head = prev; +} \ No newline at end of file