You are on page 1of 5

WEEK5

Task1:Stack
#include<stdio.h>
int tos=0;
int push(int y,int a[50])
{
    int max=50;
    if (tos<=max)
    {
        tos++;
        a[tos]=y;
    }
    else{
        printf("Stack overflow\n");
    }
    
}
void pop(int a[50])
{
    int x;
    if(tos>0)
    {
        x=a[tos];
        tos--;
        printf("poped:%d\n",x);
    }
    else
    {
       printf("Stack is empty\n");
    }
}
int display(int a[50])
{
    int i;
    if (tos!=0){
    for(i=1;i<=tos;i++)
    {
        printf("%d ",a[i]);
   }
    printf("\n");}
    else
    {
        printf("Stack is empty\n");
    }
}

int main()
{
    int a[50],option,k;
    printf("1.push\n2.pop\n3.display\n4.stop\n");
    
    while (1)
    {
        printf("enter the option:");
        scanf("%d",&option);
        switch(option){
        case 1:
        {
            printf("enter a number to push:");
            scanf("%d",&k);
            push(k,a);
            break;
        }
        case 2:
        {
            printf("ur in pop:\n");
            pop(a);
            break;
        }
        case 3:
        {
            display(a);
            break;
        }
        case 4:
            exit(0);
        }
        
    }
    return 0;
    
}

task2:to check palindrome or not using stack


#include<stdio.h>
int pop(char a[50],int g)
{
    char x;
    if(g>=0)
    {
        x=a[g];
        return x;
    }
    else
    {
        printf("Stack is empty\n");
        return 0;
    }
}
int main()
{
    int option,g,j=0,f=0;
    char a[50];
    printf("enter the string:");
    scanf("%s",&a);
    g=strlen(a);    
    while (g>0)
    {
        if (pop(a,g-1)!=a[j]){
            printf("Not a palindrome\n");
            f=1;
            break;
        }
        j++;
        g--;
        
    }
    if (f==0){
        printf("Palindrome\n");
    }
    return 0;
    
}

task3:to implement stack operations


1.push(),2.pop(),3.to print maximum element
#include<stdio.h>
int tos=0;
int push(int y,int a[50])
{
    int max=50;
    if (tos<=max)
    {
        tos++;
        a[tos]=y;
    }
    else{
        printf("Stack overflow\n");
    }
    
}
void pop(int a[50])
{
    int x;
    if(tos>0)
    {
        x=a[tos];
        tos--;
        printf("poped:%d\n",x);
    }
    else
    {
       printf("Stack is empty\n");
    }
}
int display(int a[50])
{
    int i,max;
    max=a[1];
    if (tos!=0){
    for(i=2;i<=tos;i++)
    {
        if (max<a[i])
        {
            max=a[i];
        }
    }
    printf("maximum element:%d\n",max);
}
    else
    {
        printf("Stack is empty\n");
    }
}

int main()
{
    int a[50],option,k,h,j;
    printf("enter the no.of inputs:");
    scanf("%d",&h);
    printf("1.push\n2.pop\n3.display\n");
    for (j=1;j<=h;j++)
    {
        printf("enter the option:");
        scanf("%d",&option);
        switch(option){
        case 1:
        {
            printf("enter a number to push:");
            scanf("%d",&k);
            push(k,a);
            break;
        }
        case 2:
        {
            printf("ur in pop:\n");
            pop(a);
            break;
        }
        case 3:
        {
            display(a);
            break;
        }
    }
}
return 0;
}

You might also like