0% found this document useful (0 votes)
10 views

Program 6

Uploaded by

ajaiswal2907
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Program 6

Uploaded by

ajaiswal2907
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PROGRAM 6

OBJECTIVE

Perform push, pop, and display function on the stack implemented by array.

THEORY

In this program we take an array of constant size that is 100. We use top pointer to implement
stack which represent the topmost element of the stack. If top=size-1, the message says Overflow
condition, that is no more elements can be added, else if top=0, the message says Underflow
condition, that is, no more elements can be deleted.

PSEUDO CODE

ALGORITHM MainFunction()

BEGIN:

READ(stack[size])

READ(choice)

IF choice==1 THEN

READ(value)

CALL Push(value)

ELSE IF choice==2 THEN

CALL Pop()

ELSE IF choice==3 THEN

CALL Display()

ELSE

WRITE(Invalid Choice)

END;

ALGORITHM Push(value)
BEGIN:

IF top<size-1 THEN

top=top+1

Stack[top]=value

WRITE(stack[top])

ELSE

WRITE(Stack Overflow)

END;

ALGORITHM Pop()

BEGIN:

IF top==-1 THEN

WRITE(Stack Underflow)

ELSE

top=top-1

END;

ALGORITHM Display()

BEGIN:

IF top>=0 THEN

WRITE(stack[])

ELSE

WRITE(Stack is empty)

END;
SOURCE CODE

#include<stdio.h>

#define size 100

int stack[size];

int top=-1;

void push(int value){

if(top<size-1){

stack[++top]=value;

printf("stack[%d]=%d",top,value);

else

printf("Stack Overflow");

void pop(){

if(top==-1)

printf("Stack Underflow");

else

top--;

void display(){

if(top>=0){

printf("Stack Elements are:\n");

for(int i=0;i<=top;i++){

printf("%d\n",stack[i]);

}
}

else{

printf("Stack is empty");

int main(){

int choice;

printf("Type 1 if you want to push, 2 if you want to pop, and 3 if you want to display the stack
elements: ");

scanf("%d",&choice);

switch(choice){

case 1:

int value=0;

printf("Enter the value to be pushed into the stack:");

scanf("%d",&value);

push(value);

break;

case 2:

pop();

break;

case 3:

display();

break;

default:

printf("Invalid Input");
}

return 0;

OUTPUT

Type 1 if you want to push, 2 if you want to pop, and 3 if you want to display the stack
elements:1

Enter the value to be pushed into the stack:5

Stack[1]=5

TIME COMPLEXITY: O(n)

SPACE COMPLEXITY: O(1)

You might also like