program-
#include< stdio.h>
#include< conio.h>
#include< alloc.h>
struct stack
{
int data;
struct stack *back;
};
struct stack *top;
void create()
{
char ch;
struct stack *ptr,*cpt;
printf("Enter Number\n");
cpt=(struct node*)malloc(sizeof(struct stack));
scanf("%d",&cpt->data);
cpt->back=NULL;
do
{
printf("Enter Number\n");
ptr=(struct stack*)malloc(sizeof(struct stack));
scanf("%d",&ptr->data);
ptr->back=cpt;
cpt=ptr;
printf("Continue(y/n)?\n");
ch=getch();
}while(ch=='y');
top=cpt;
}
void traverse()
{
struct stack *ptr=top;
printf("Stack is :\n");
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->back;
}
}
void count()
{
struct stack *ptr=top;
int n,count=0;
printf("Enter number to count in stack\n");
scanf("%d",&n);
while(ptr!=NULL)
{
if(ptr->data==n)
count++;
ptr=ptr->back;
}
printf("Occurrence of %d is %d",n,count);
}
void main()
{
clrscr();
create();
traverse();
count();
getch();
}
Enter Number
3
Enter Number
4
Continue(y/n)?
Enter Number
7
Continue(y/n)?
Enter Number
3
Continue(y/n)?
Enter Number
1
Continue(y/n)?
Enter Number
3
Continue(y/n)?
Enter Number
1
Continue(y/n)?
Stack is :
1 3 1 3 7 4 3
Enter number to count in stack
3
Occurrence of 3 is 3