Thursday, 6 February 2014

Program of Stack in C Language with full functionality PHSH,POP,PEEP,SEARCH,DISPLAY,UPDATE


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int tos=-1;
int data[5];
void MENU();
void PUSH();
void POP();
void PRINT();
void PEEP();
void UPDATE();
void SEARCH();
void MSG(char msg[]);
void main()
{
       MENU();
       getch();
}
void MENU()
{
int ch;
clrscr();
printf("\n\t----Menu----");
printf("\n\n\t 1. PUSH \n\t 2. POP \n\t 3. PEEP \n\t 4. UPDATE \n\t 5. SEARCH \n\t 6. DISPLAY \n\t 7. EXIT ");
printf("\n\n\t Enter Your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
PUSH();
break;
case 2:
POP();
break;
case 3:
PEEP();
break;
case 4:
UPDATE();
break;
case 5:
SEARCH();
break;
case 6:
PRINT();
break;
case 7:
exit(0);
break;
default:
MSG("Invalid Choice");
}
}
void PUSH()
{
if(tos<4)
{
int item;
clrscr();
printf("\n\t Enter Item : ");
scanf("%d",&item);
tos++;
data[tos]=item;
MENU();
}else
{
MSG("Stack is Full");
}
}
void POP()
{
if(tos>-1)
{
clrscr();
printf("\n\t Deleted Item is : %d",data[tos]);
tos--;
getch();
MENU();
}else
{
MSG("\n\t Stack is Empty");
}
}
void PEEP()
{
if(tos>-1)
{
int pos;
clrscr();
printf("\n\t Enter Which Element You Want to See : ");
scanf("%d",&pos);
pos=tos-pos+1;
printf("\n\n\t Element is : ");
scanf("%d",data[pos]);
getch();
MENU();
}else
{
MSG("\n\t Stack is Empty");
}
}
void UPDATE()
{
if(tos>-1)
{
int pos,ele;
clrscr();
printf("\n\t Enter Position to Update Element : ");
scanf("%d",&pos);
printf("\n\t Enter Value : ");
scanf("%d",&ele);
pos=tos-pos+1;
data[pos]=ele;
MSG("\n\t Record Updated");
getch();
MENU();
}else
{
MSG("Stack is Empty");
}

}
void SEARCH()
{
if(tos>-1)
{
int i,ele,cnt=0;
clrscr();
printf("\n\t Enter Element to Search : ");
scanf("%d",&ele);
for(i=0;i<=tos;i++)
{
if(data[i]==ele)
{
cnt++;
}
}
if(cnt>0)
{
printf("\n\t Element %d  Found %d times in the Stack",ele,cnt);
}else
{
      printf("\n\t Element Not Found");
}
getch();
MENU();
}else
{
MSG("\n\t Stack is Empty");
}
}
void PRINT()
{
if(tos>-1)
{
int i;
clrscr();
for(i=0;i<=tos;i++)
{
printf("\n\t Element At Position %d is : %d",i,data[i]);
}
getch();
MENU();
}else
{
MSG("\n\t Stack is Empty");
}
}
void MSG(char msg[])
{
printf("\n\t %s ",msg);
getch();
MENU();
}

1 comment: