Monday, October 4, 2010

Advance Stack Using C language

Stack using C language with advance options.

#include <stdio.h>
#include<stdlib.h>
#define MAX 9
void push(int stack[],int *top,int item)
{
 if(*top==MAX)
 {
  printf("\nOver Flow.....");
  return;
 }
 else
 {
  *top=*top+1;
  stack[*top]=item;
 }
}
int pop(int stack[],int *top)
{
 int a;
 if(*top==-1)
 {
  printf("\nUnder flow......");
  return -1;
 }
 else
 {
  a=stack[*top];
  *top=*top-1;
  return a;
 }
}
void display(int stack[],int top)
{
 int i;
 for(i=0;i<=top;i++)
 {
  printf("\nPosition: %d\tVaue: %d",i,stack[i]);
 }
}
void main()
{
 int stack[10];
 int top=-1;
 int c,item;
 while(1)
 {
  printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
  printf("\nEnter your choice: ");
  scanf("%d",&c);
  switch(c)
  {
   case 1:
    printf("Enter item to insert: ");
    scanf("%d",&item);
    push(stack,&top,item);
    break;
   case 2:
    item=pop(stack,&top);
    if(item!=-1)
     printf("\n%d is deleted",item);
    break;
   case 3:
    display(stack,top);
    break;
   case 4:
    exit(0);
   default:
    printf("\nWrong choice");
  }
 }
}

0 comments:

Post a Comment

You are most welcome for making comments here. I will consider your view and will reply you soon.