4
PROGRAM-2 AIM:- WRITE A PROGRAM TO POP, PUSH & INSERT ELEMENTS OF A STACK #include<conio.h> #include<stdio.h> #define size 100 int a[size]; int n=-1; void push(); void display(); void pop(); void main() { int num; do { printf("\nenter 1 to push,2 to pop,3 to display the elements"); scanf("%d",&num); switch(num) { case 1: push(); break; case 2: pop(); break;

Data Structure Prac. No. 2 Write a Program to Pop, Push & Insert Elements of a Stack

Embed Size (px)

DESCRIPTION

Data Structure Prac. No. 2 Write a Program to Pop, Push & Insert Elements of a Stack

Citation preview

Page 1: Data Structure Prac. No. 2 Write a Program to Pop, Push & Insert Elements of a Stack

PROGRAM-2

AIM:-WRITE A PROGRAM TO POP, PUSH & INSERT ELEMENTS OF A STACK

#include<conio.h>#include<stdio.h>#define size 100int a[size];int n=-1;void push();void display();void pop();void main() { int num; do { printf("\nenter 1 to push,2 to pop,3 to display the elements"); scanf("%d",&num); switch(num) { case 1:

push(); break;

case 2: pop(); break;

case 3: display();

} printf("\nenter 1 for appling loop again or 2 for not"); scanf("%d",&num); }while(num==1); }

Page 2: Data Structure Prac. No. 2 Write a Program to Pop, Push & Insert Elements of a Stack

void push() { if(n==size) { printf("\narray overflow"); return; } n=n+1; printf("\nenter the number"); scanf("%d",&a[n]); return; }

void pop() { if(n==-1) { printf("\narray is empty"); return; } else { printf("\nelement deleted=%d",a[n]); n=n-1; } return; }

void display() { int i; for(i=n;i>=0;i--) printf("\n%dth element=%d",i+1,a[i]); return;}

Page 3: Data Structure Prac. No. 2 Write a Program to Pop, Push & Insert Elements of a Stack

OUTPUT:-

Page 4: Data Structure Prac. No. 2 Write a Program to Pop, Push & Insert Elements of a Stack