Showing posts with label Stack Implementation. Show all posts
Showing posts with label Stack Implementation. Show all posts

Array Stack using interface in Java

/*  Stack using interface  */

    import java.io.*;
    interface stack
    {
        void push(int val);
        void pop();
        void display();
        void check();
    }
    class stacktest implements stack
    {
        int top,a[],n;
        stacktest(int cap)
        {
            n=cap;
            top=-1;
            a=new int[n];
        }
        public void push(int val)
        {
            if(top==n-1)
                System.out.println("\nStack overflow .... \n");
            else
                a[++top]=val;
        }
        public void pop()
        {
            int r;
            if(top==-1)
                System.out.println("\nStack underflow...\n");
            else
            {           
                r=a[top--];
                System.out.println("\nDeleted Element : " + r + "\n");
            }
        }
        public void display()
        {
            if(top==-1)
                System.out.println("\nStack Underflow .... \n");
            else
            {
                System.out.print("Stack : ");
                for(int i=top;i>=0;i--)
                    System.out.print(a[i] + "   ");
                System.out.println();
            }
        }
        public void check()
        {
            if(top==-1)
                System.out.println("\nStack empty... no element in stack....");
            else if(top==n-1)
                System.out.println("\nStack full.... no empty space...");
            else
            {
                int s=top+1;
                System.out.println("\nStack contains " + s + " elements");
            }
        }
    }
    class interstack
    {
        public static void main(String args[])throws IOException
        {
            System.out.println("\nProgram to implement stack using interface ");
            int ch,n,val;
            System.out.println("Enter array capacity : ");
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            n=Integer.parseInt(br.readLine());
            stacktest obj=new stacktest(n);
            do
            {
            System.out.println("[1] Push [2] Pop [3] Display [4] Check stack status [5] Exit \n");
            System.out.println("Enter choice : ");
            ch=Integer.parseInt(br.readLine());
            switch(ch)
            {
                case 1:
                    System.out.println("Enter value to push : ");
                    val=Integer.parseInt(br.readLine());
                    obj.push(val);
                    obj.display();
                    break;
           
                case 2:
                    obj.pop();
                    obj.display();
                    break;
                       
                case 3:
                    obj.display();
                    break;
   
                case 4:
                    obj.check();
                    break;

                case 5:    break;

                default :
                        System.out.println("Enter correct choice..");
                        break;
            }
            }while(ch!=5);
        }
    }

Peep Operations on Stack using arrays in C

# include
# include
# include

# define size  100

int top = -1;
int flag = 0;

int stack[100];
int data;

void push(int *, int);
int peep(int *);
void display(int *);

/* Definition of the push function */

void  push(int s[], int d)
{
    if(top ==(size-1))
        flag = 0;
    else
    {
        flag = 1;
        ++top;
        s[top] = d;

    }
}

/* Definition of the peep function */

int peep(int s[])
{
    int i;
    int peeped_element;
    printf("\n Input the information number to which you want access:");
    scanf("%d", &i);

    if(top - i + 1 < 0)
    {
        peeped_element = 0;
        flag = 0;
    }
    else
    {
        flag = 1;
        peeped_element = s[top-i +1];
    }
    return (peeped_element);
}

/* Definition of the display function */
void display(int s[])
{
    int i;
    if(top == -1)
    {
        printf("Stack is empty");
    }
    else
    {
        for(i = top; i>=0; --i)
            printf(" %d  ", s[i]);
    }
}

/* Function main */

void main()
{
    int  data;
    char choice;
    int q = 0;
    int top = -1;

    do
    {
        printf(" \nPush->i Peep->p Quit->q:");
        printf("\nInput the choice : ");
        do
        {
            choice = getchar();
            choice =tolower(choice);
        }while(strchr("ipq",choice)==NULL);
        printf("Your choice is: ", choice);

        switch(choice)
        {
        case 'i' :
            printf("\n Input the element to push:");
            scanf("%d", &data);
            push(stack, data);
            if(flag)
            {
                printf("\n After inserting ");
                display(stack);
                if(top == (size-1))
                    printf("\n Stack is full");
            }
            else
                printf("\n Stack overflow after pushing");
            break;

        case 'p' : 
            data = peep(stack);
            if(flag)
            {
                printf("\n Data is peeped: %d", data);
                printf("\n Stack is as follows:\n");

                display(stack);
            }
            else
                printf("\n Stack underflow");
            break;
        case 'q': 
            q = 1;
        }
    } while(!q);
}

Stack using array in Java

/* Program to implement stack  */ 

    import java.io.*;
    public class stack
    {
        int top=-1;
         void push(int a[],int n) throws IOException
        {
            int val;
            System.out.println("Enter value to push : ");
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            val=Integer.parseInt(br.readLine());
            if(top < n-1)
                a[++top]=val;
            else
                System.out.println("\nStack overflow.... \n");
            
        }
        void pop(int a[])
        {
            int val;
            if(top==-1)
                System.out.print("\nStack Underflow...\n");
            else
            {
                val=a[top];
                System.out.print("\nDeleted element : " + val);
                top--;
            }
            
        }
        void display(int a[])
        {
            int i;
            if(top==-1)
                System.out.print("\nStack Underflow ...\n");
            else
            {
                System.out.print("\nStack : ");
                for(i=top;i>=0;i--)
                    System.out.print(a[i] + "  ");
            }
        }
        public void main(String args[]) throws IOException
        {
            System.out.println("Program to implement stack : \n");
            int n,ch;
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter array capacity : ");
            n=Integer.parseInt(br.readLine());
            int [] a;
            a=new int[n];
            do
            {
                System.out.println("[1] Push   [2] Pop   [3] Display    [4] Exit\n");
                System.out.print("\nEnter your choice : ");
                BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
                ch=Integer.parseInt(br1.readLine());
                switch(ch)
                {
                    case 1:
                        push(a,n);
                        display(a);
                        break;
    
                    case 2:
                        pop(a);
                        display(a);
                        break;
    
                    case 3:
                        display(a);
                        break;

                    case 4:
                        break;
        
                    default : 
                        System.out.println("Enter correct choice...");
                        break;
                }
                
            }while(ch!=4);
        }
    }

Stack using array and pointer in C

/* Program to implement stack using array */
    #include<stdio.h>
    void push(int a[],int n,int *top,int val)
    {
        if(*top>=n-1)
        {
            printf("Overflow !!! \n\n");
            return;
        }
        a[++(*top)]=val;
    }
    int pop(int a[],int *top)
    {
        if(*top==-1)
        {
            printf("Underflow !!! \n\n");
            return (-999);
        }
        return(a[(*top)--]);
    }
    void display(int a[],int *top)
    {
        int i;
        if(*top==-1)    printf("\nStack Empty !! \n");
        else
        {
            printf("\nStack : ");
            for(i=*top;i>=0;i--)
                printf("%4d",a[i]);
        }
    }
    main()
    {
        int n,r,val,top=-1;
        printf("\nProgram to implement stack : \n");
        printf("\nEnter array capacity : ");
        scanf("%d",&n);
        int a[n];
        int ch;
        do
        {
            printf("\n[1] Push :");
            printf("\n[2] Pop : ");
            printf("\n[3] Display : ");
            printf("\n[4] Quit : ");
            printf("\nEnter your choice : ");
            scanf("%d",&ch);
            switch(ch)
            {
                case 1:
                    printf("\nEnter value to push : ");
                    scanf("%d",&val);
                    push(a,n,&top,val);
                    display(a,&top);
                    break;

                case 2:
                    r=pop(a,&top);
                    if(r==-999)    
                        printf("\nStack empty : ");
                    else
                    {
                        printf("\nDeleted element : %d",r);
                        display(a,&top);
                    }
                    break;
                case 3:
                    display(a,&top);
                    break;
                case 4:    break;
                default: printf("\nEnter correct choice ... ");
                     break;
            }
        }while(ch!=4);
        printf("\n\n");
    }

Stack using linked list

    /* Program to implement stack using linked list */

    #include

    #include

    #define new1 (nd*)malloc(sizeof(nd))

    typedef struct stack

    {

        int info;

        struct stack *next;

    }nd;

    void push(nd **ptr)

    {

        nd *c=*ptr;

        if(*ptr==NULL)

        {

            *ptr=new1;

            printf("\nEnter element to push : ");

            scanf("%d",&(*ptr)->info);

            (*ptr)->next=NULL;

        }

        else

        {

            nd *t;

            t=new1;

            printf("\nEnter element to push : ");

            scanf("%d",&t->info);

            while(c->next!=NULL)

                c=c->next;

            t->next=NULL;

            c->next=t;

        }

    }

    void pop(nd **ptr)

    {

        nd *c=*ptr,*t=*ptr,*p;

        if(*ptr==NULL)

            printf("\nStack is empty...");

        else if(t->next==NULL)         // If queue contains single node

        {

            *ptr=NULL;

            free(t);

        }

        else

        {

            while(c->next!=NULL)

            {

                p=c;

                c=c->next;

            }

            p->next=NULL;

            free(c);

        }

    }

    void display(nd *ptr)

    {

        if(ptr==NULL)

            printf("\nStack is empty...\n");

        else

        {

            printf("\nStack  :   ");

            while(ptr!=NULL)

            {

                printf(" %d --> ",ptr->info);

                ptr=ptr->next;

            }

            printf("\b\b\b\b   ");

            printf("\n");

        }

    }

    main()

    {

        nd *h=NULL;

        printf("\nProgram to implement stack using linked list : \n");

        int ch;

        do

        {

            printf("\n[1] Push :");

            printf("\n[2] Pop : ");

            printf("\n[3] Display : ");

            printf("\n[4] Quit : ");

            printf("\nEnter your choice : ");

            scanf("%d",&ch);

            switch(ch)

            {

                case 1:

                    push(&h);

                    display(h);

                    break;

                case 2:

                    pop(&h);

                    display(h);

                    break;

                case 3:

                    display(h);

                    break;

                case 4:    break;

                default: printf("\nEnter correct choice ... ");

                     break;

            }

        }while(ch!=4);

        printf("\n\n");

    }

Top