Showing posts with label source code of STACKs (Using Arrays). Show all posts
Showing posts with label source code of STACKs (Using Arrays). Show all posts

Wednesday, January 31, 2024

STACKs (Using Arrays) - DSTC using C Language (Source Code Implemented)

STACKs (Using Arrays)
Implemented using C Language

C Language Code:


ARRSTACK.H

//          DSTC    ARRSTACK.H
// implementation of stacks using 1 dimentional array

typedef struct arraystack
{
            int *bfr ;
            int size;
            int top ;
}stack ;

stack *makestack(int n)
{

            stack *s ;
            s = (stack *)malloc(sizeof(stack));
            s->top = -1 ;
            s->size = n ;
            s->bfr = (int *)malloc(sizeof(int)*n);
            return (s);
}

 

void pushstack(stack *s,int val)
{
            if (s->top == (s->size-1))
            {            printf("\n Stack is FULL ");
                        exit(1);
            }
            if (s->top == -1)
                        s->top = 0 ;
            else
                        s->top += 1 ;

            s->bfr[s->top] = val ;
}

int popstack(stack *s)
{
            int v ;
            if(s->top == -1)
            {
                        printf("\n Stack is EMPTY");
                        exit(1);
            }
            v = s->bfr[s->top];
            s->top -= 1 ;
            return (v);
}


ARRSTACK.CPP

#include
#include
#include
#include "arrstack.h"

main()
{
            stack *s1 ;
            int n,v,i;
            clrscr();
            printf("\n enter the no. of elements in the stack:");
            scanf("%d",&v);
            s1 = makestack(v);
            for(i=0;i< V;I++)
            {
                        pushstack(s1,i+10);
            }

            for(i=0;i< V ;I++)
            {
                        printf("\n Values no %d = %d",i,popstack(s1));
            }
}