dsa

1
#include<stdio.h>
#include<ctype.h>

char s[100];
int top=-1;

int priority(char ch)
{
    if(ch=='+' || ch=='-') return 1;
    if(ch=='*' || ch=='/') return 2;
    return 0;
}

int main()
{
    char exp[100];
    scanf("%s",exp);

    for(int i=0; exp[i]!='\0'; i++)
    {
        if(isalnum(exp[i]))
            printf("%c ",exp[i]);

        else if(exp[i]=='(')
            s[++top]='(';

        else if(exp[i]==')')
        {
            while(s[top]!='(')
                printf("%c ",s[top--]);
            top--;
        }

        else
        {
            while(top!=-1 && priority(s[top])>=priority(exp[i]))
                printf("%c ",s[top--]);
            s[++top]=exp[i];
        }
    }

    while(top!=-1)
        printf("%c ",s[top--]);

    return 0;
}


4th

#include<stdio.h>
#include<stdlib.h>

struct node{
    int d;
    struct node *n;
}*h=NULL,*t,*p;

void display(){
    for(t=h;t;t=t->n) printf("%d ",t->d);
    printf("\n");
}

void insBeg(int x){
    t=malloc(sizeof(struct node));
    t->d=x; t->n=h; h=t;
}

void insEnd(int x){
    t=malloc(sizeof(struct node));
    t->d=x; t->n=NULL;
    if(!h) h=t;
    else{
        p=h;
        while(p->n) p=p->n;
        p->n=t;
    }
}

void delBeg(){
    if(h){
        t=h;
        h=h->n;
        free(t);
    }
}

void delEnd(){
    if(!h) return;
    if(!h->n){ free(h); h=NULL; return; }

    p=h;
    while(p->n->n) p=p->n;
    free(p->n);
    p->n=NULL;
}

void delVal(int x){
    p=h; t=NULL;
    while(p && p->d!=x){
        t=p; p=p->n;
    }
    if(!p) return;
    if(!t) h=p->n;
    else t->n=p->n;
    free(p);
}

int main(){
    int c,x;
    while(1){
        printf("1.Display\n2.Insert Begin\n3.Insert End\n4.Delete Begin\n5.Delete End\n6.Delete Element\n7.Exit\n");
        scanf("%d",&c);

        switch(c){
            case 1: display(); break;
            case 2: printf("Enter data: "); scanf("%d",&x); insBeg(x); break;
            case 3: printf("Enter data: "); scanf("%d",&x); insEnd(x); break;
            case 4: delBeg(); break;
            case 5: delEnd(); break;
            case 6: printf("Enter value to delete: "); scanf("%d",&x); delVal(x); break;
            case 7: return 0;
            default: printf("Invalid Choice\n");
        }
    }
}

Popular posts from this blog

WT lab

DBMS LAB

A2 python