From b7884cef999bcf86b22303eda9f53ba525f8500a Mon Sep 17 00:00:00 2001 From: Nithin Kumar <71171702+nithin-kasauzki@users.noreply.github.com> Date: Sat, 22 Oct 2022 12:42:09 +0530 Subject: [PATCH 1/2] Added file stack using linkedlist implementing stack using linked list --- C/stackusingLL.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 C/stackusingLL.c diff --git a/C/stackusingLL.c b/C/stackusingLL.c new file mode 100644 index 0000000..172c483 --- /dev/null +++ b/C/stackusingLL.c @@ -0,0 +1,68 @@ +#include +#include + +struct node +{ + int data; + struct node * next; +}; +struct node * top=NULL; + +void push(int x){ + struct node *newnode; + newnode=(struct node *)malloc(sizeof(struct node)); + newnode->next=NULL; + newnode->data=x; + + if(top==NULL){ + top=newnode; + } + else{ + newnode->next=top; + top=newnode; + } +} + +void pop(){ + struct node * current=top; + if(top!=NULL) + { + top=current->next; + free(current); + } + else printf("no elements"); +} + +void display(){ + struct node * current=top; + while(current!=NULL){ + printf("%d ",current->data); + current=current->next; + } +} + +void main(){ + int option=0; + int d; + while(1){ + printf("\n1.push\t2.pop\t3.show\t0.exit:"); + scanf("%d",&option); + switch (option) + { + case 1: + printf("data:");scanf("%d",&d); + push(d); + break; + case 2: + pop(); + break; + case 3: + display(); + break; + case 0: + exit(0); + default: + break; + } + } +} From 5ebc8fad220740b9306988b58713672426d69522 Mon Sep 17 00:00:00 2001 From: Nithin Kumar <71171702+nithin-kasauzki@users.noreply.github.com> Date: Sat, 22 Oct 2022 12:45:26 +0530 Subject: [PATCH 2/2] infix to postfix --- C/infix2post.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 C/infix2post.c diff --git a/C/infix2post.c b/C/infix2post.c new file mode 100644 index 0000000..a1c43b8 --- /dev/null +++ b/C/infix2post.c @@ -0,0 +1,64 @@ +#include + +#define size 100 +char arr[size]; +top= -1; + +void push(char x){ + top++; + arr[top]=x; +} +void pop(){ + if(arr[top]!='(') + printf("%c",arr[top]); + top--; +} + +int pr(char c){ + if(c=='+' || c=='-'){ + return 1; + } + else if(c=='*' || c=='/' || c=='%'){ + return 2; + } + else + return -1; +} +int isalnum(char c){ + if((c>='a' && c<='z')||(c>='A' && c<='B')|| (c>='1' && c<='9')) + return 1; + else + return 0; +} +int main(){ + + char a[size]; + printf("exp:"); + scanf("%s",a); + for(int i=0; a[i]!= '\0';i++){ + if(isalnum(a[i])){ + printf("%c",a[i]); + } + else{ + if(top==-1 || a[i]=='(') + push(a[i]); + else if(a[i]==')'){ + while(arr[top]!='('){ + pop(); + } + } + else { + while(pr(a[i])<=pr(arr[top]) && top>=0 && pr(a[i])!=-1){ + pop(); + } + push(a[i]); + } + } + } + while (top>=0) + { + pop(); + } + + return 0; +} \ No newline at end of file