Write menu driven program for Stack using array for Push, Pop,display operations

On-campus and online computer science courses to Learn the basic concepts of Computer Science.This tutorial will cover c ,c++, java, data structure and algorithm,computer graphics,microprocessor,analysis of algorithms,Digital Logic Design and Analysis,computer architecture,computer networks,operating system.
code in C:
#include<stdio.h>
#include<conio.h>
#define MAX 5

typedef struct stack
{
 int data[MAX];
 int top;
}stack;

void initialize(stack *);
int isEmpty(stack *);
int isFull(stack *);
void push(stack *,int);
void pop(stack *);
void display(stack *);

int main()
{
 stack s;
 int choice;
 int n,x;
 initialize(&s);
 do
 {
  printf("Select your option:-\n");
  printf("1. Push\n");
  printf("2. Pop\n");
  printf("3. Display\n");
  printf("4. Exit\n");
  scn:scanf("%d",&choice);
  switch(choice)
  {
   case 1: printf("Enter Element To Push: ");
    scanf("%d",&n);
    push(&s,n);
    getch();
    break;
   case 2: pop(&s);
    getch();
    break;
   case 3: display(&s);
    getch();
    break;
   case 4: break;
   default:printf("Invalid Option. Enter Again\n");
    goto scn;
  }

 }while(choice!=4);
 return 0;
}

void initialize(stack *s)
{
 s->top = -1;
}

int isEmpty(stack *s)
{
 if (s->top==-1)
  return 1;
 else
  return 0;
}

int isFull(stack *s)
{
 if(s->top==4)
  return 1;
 else
  return 0;
}

void push(stack *s,int n)
{
 if(isFull(s))
  printf("Stack Full\n");
 else
 {
  s->top = s->top + 1;
  s->data[s->top]=n;
  printf("%d Added To Stack\n",n);
 }
}

void pop(stack *s)
{
 if(isEmpty(s))
  printf("Stack Empty");
 else
 {
  int x = s->data[s->top];
  s->top = s->top - 1;
  printf("%d Popped From Stack\n",x);
 }
}

void display(stack *s)
{
 int i;
 if(isEmpty(s))
  printf("Stack Empty");
 else
 {
  printf("Stack:-\n");
  for(i=0;i<=s->top;i++)
   printf("%d ",s->data[i]);
 }
}

output:-
Select your option:-
1. Push
2. Pop
3. Display
4. Exit
1
Enter Element To Push: 12
12 Added To Stack
Select your option:-
1. Push
2. Pop
3. Display
4. Exit
1
Enter Element To Push: 13
13 Added To Stack
Select your option:-
1. Push
2. Pop
3. Display
4. Exit
3
Stack:-
12 13 Select your option:-
1. Push
2. Pop
3. Display
4. Exit
2
13 Popped From Stack
Select your option:-
1. Push
2. Pop
3. Display
4. Exit
3
Stack:-
12 Select your option:-
1. Push
2. Pop
3. Display
4. Exit
4

--------------------------------

0 comments: