#1 :Stack using Array.

Stack.

1.What is stack?
ANS.: Ordered collection of elements using LIFO(Last in First Out) mechanism.

2.Operations performed on stack?
ANS.: Push (insertion), pop (deletion), peek (Topmost element) & display.

3.Applications of stack?
ANS.: Infix, Postfix and Prefix notations, Recursive functions, Function cell, Expression evalutation etc

Graphical Representation.

Program for Stack using Array.

Source code.

#include<conio.h>
#include<stdio.h>
#define SIZE 4

int stack[SIZE];
int top=-1;

int empty();
int full();
void display();
void pop();
void push(int);

int main()
{
	
        printf("push elements\n");
	push(10);//10
	push(20);//10 20
	push(30);//10 20 30
        push(40);// 10 20 30 40
	push(15);//error full
        printf("pop elements.\n ");
	pop();//10 20 30
	pop();//10 20
	pop();// 10
	pop();//error empty
        getch();
}

int empty()
{
	if(top==-1)
		return 1;
	else
		return 0;
}

int full()
{
	if(top==SIZE-1)
		return 1;
	else
		return 0;
}

void display()
{
	int i;
	printf("Stack:");
	for(i=0; i<=top; i++)
        printf("%d ",stack[i]);
	printf("\n");
}

void push(int val)
{
	if(full())
	{
		printf("Error : 
                Stack full\n");
	}
	else
	{
		stack[++top]=val;
		display();
	}
}

void pop()
{
	int i,loc;
	if(empty())
	{
	      printf("Error : 
             Stack empty\n");
	}
	else
	{    top--;
             display();
	}
}

Output.

push stack.
Stack: 10
Stack: 10 20
Stack: 10 20 3O
Stack: 10 20 3O 40
Error : Stack full
pop stack.
Stack: 10 20 3O
Stack: 10 20
Stack: 10
Stack: 

Ashishkumar Vishwakarma

I am Ashish- a Developer and Artist live in Mumbai.

One thought on “#1 :Stack using Array.

Leave a Reply