Stack using Arrays using C

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void push(int a[],int*tp)
{
    int data,choice=1;
    while(choice==1)
    {
    printf("enter the element to be inserted");
    scanf("%d",&data);
    *tp = *tp+1;
    a[*tp]=data;
    printf("want another data to be pushed (0,1): ");
    scanf("%d",&choice);
    }
}
void pop(int a[],int*tp)
{
    int data,choice=1;
    while(choice==1)
    {
    data=a[*tp];
    *tp=*tp-1;
    printf("deleted %d from stack\n",data);
    printf("want another data to be popped (0,1): ");
    scanf("%d",&choice);
    }
}
void display(int a[],int*tp)
{
    int i;
    for(i=*tp;i>=0;i--)
        printf("%d\n",a[i]);
}
int main()
{
    int arr[100],top=-1,i,choice=1,ch;
    while(choice==1)
    {
        printf("enter 1. to Push element in stack\n");
        printf("enter 2. to pop element in stack\n");
        printf("enter 3. to display elements of stack\n");
        scanf("%d",&ch);
        if(ch==1)
        {
            if(top==99)
            printf("Stack overflow");
            else
            push(arr,&top);
        }
        else if(ch==2)
        {
            if(top==-1)
                printf("stack underflow");
            else
                pop(arr,&top);
        }
        else if(ch==3)
        {
            display(arr,&top);
        }
        else
            printf("Invalid Choice");

        printf("Do you want to continue (0,1): ");
        scanf("%d",&choice);
    }
}

Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree