C Program to perform Stack Operations Using Pointer | C Language Stack Operation with Pointers

C Program to perform Stack Operations Using Pointer | C Language Stack Operation with Pointers | Pointers and stack operation in C



#include
#include
#include
#define MAX 50

int size;

/* Defining the stack structure */
struct stack
{
    int AR[MAX];
    int top;
};

/* Initializing the stack(i.e., top=-1)   */
void init_stk(struct stack *st)
{
    st->top=-1;
}

/* Entering the elements into stack */
void push (struct stack *st,int num)
{
    if(st->top == size-1)
    {
    printf("\nStack overflow(i.e., stack full).");
    return;
    }
    
    st->top++;
    st->AR[st->top] = num;
}

//Deleting an element from the stack.
int pop(struct stack *st)
{
    int num;
    if(st->top == -1)
    {
    printf("\nStack underflow(i.e., stack empty).");
    return NULL;
    }
    
    num=st->AR[st->top];
    st->top--;
    return num;
}
void display(struct stack *st)
{
    int i;
    for(i=st->top;i>=0;i--)
        printf("\n%d",st->AR[i]);
}
void main()
{
    int ele,opt,val;
    struct stack ptr;
    clrscr();
    init_stk(&ptr);
    printf("\nEnter Stack Size :");
    scanf("%d",&size);
    while(1)
    {
        printf("\n\n\tSTACK PRIMITIVE OPERATIONS");
        printf("\n1.PUSH");
        printf("\n2.POP");
        printf("\n3.DISPLAY");
        printf("\n4.QUIT");
        printf("\n");
        printf("\nEnter your option : ");
        scanf("%d",&opt);
        switch(opt)
        {
            case 1:
                printf("\nEnter the element into stack:");
                scanf("%d",&val);
                push(&ptr,val);
                break;
            case 2:
                ele=pop(&ptr);
                printf("\nThe element popped from stack is : %d",ele);
                break;
            case 3:
                printf("\nThe current stack elements are:");
                display(&ptr);
                break;
            case 4:
                getch();
                exit(0);
            default:
                printf("\nEnter correct option!Try again.");
        }
    }
}



Related Links :

1 comment:

  1. stack operations are used for storing elements in a stack. The input method for stack is last in first out. Thank you for sharing this program.
    regards:
    srinath reddy.
    admin of Programming Tutorials for Beginners

    ReplyDelete


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!