Stack

A stack is an ordered collection of items to which new items may be inserted and from which items may be deleted at one end, called the top of the stack.

Unlike that of the array, the definition of the stack provides for the insertion and deletion of items, so that a stack is dynamic, constantly changing object. The definition of a stack is that, a single end of the stack is designated as top where new items may be put on the top or items can be removed from the top of the stack

LIFO - LAST IN FIRST OUT

•          Placing a data item on the top is called “pushing”, while removing an item from the top is called “popping” it.
•          Push and pop are the primary stack operations.
•          Some of the applications are: microprocessors, some older calculators etc.

/* Implementing Stack as an Array */
#include<stdio.h>
#define SIZE 5
int stack[SIZE];
int ele,tos;

void push(int);
int pop();
void show();
isEmpty();
isFull();

void main()
{
            int ch;
            clrscr();
            tos=0;
            do
            {
                        clrscr();
                        gotoxy(30,1); printf("1. Push");
                        gotoxy(30,2); printf("2. Pop");
                        gotoxy(30,3); printf("3. Show");
                        gotoxy(30,4); printf("4. Exit");
                        gotoxy(30,6); printf("Enter Your Choice :");
                        scanf("%d", &ch);
                        switch(ch)
                        {
                        case 1:
                                    if(isFull())
                                    printf("\n Stack is Full");
                                    else
                                    {
                                                printf("\n Enter Element to insert :");
                                                scanf("%d", &ele);
                                                push(ele);
                                    }
                                    getch();
                                    break;
                        case 2:
                                    if(isEmpty())
                                    printf("\n Stack is Empty");
                                    else
                                    printf("\n Removed Element is :%d", pop());
                                    getch();
                                    break;
                        case 3:
                                    if(isEmpty())
                                    printf("\n Stack is Empty");
                                    else
show();
                                    getch();
                                    break;
                        case 4:
                                    exit(0);
                        default :
                                    printf("\n Invalid Choice");
                                    getch();
                        }
            }while(1);
getch();
}
isEmpty()
{
            return(tos==0);
}
isFull()
{
            return(tos==SIZE);
}
void push(int ele)
{
            stack[tos]=ele;
            tos++;
}
int pop()
{
            tos--;
            return(stack[tos]);
}
void show()
{
            int x=tos;
            printf("\n The Stack Elements are........\n");
            while(x!=0)
            {
                        printf("%d\n", stack[--x]);
            }


}

No comments:

Post a Comment