Anil

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

#include<stdio.

h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
}*top=NULL,*top1=NULL;
void push(int);
void push1(int);
int pop();
int isEmpty();
void traverse();
int count_num(int);
int count_num1(int);
int count_non_zero();
int count_unique_num();
void add_num(int);
int size=0,size1=0;
int main()
{
int x,item;
while(1){
printf("\n\nSTACK OPTIONS:\n\n1.Insert\n2.Delete\n3.Top\n4.Trave
rse\n5.Other Options\n6.Exit\n\nEnter your choice: ");
scanf("%d",&x);
switch(x){
case 1:printf("\nEnter the element: ");
scanf("%d",&item);
push(item);
break;
case 2:if(!isEmpty())
printf("\nThe element deleted is %d.",po
p());
else printf("\nStack Underflow.");
break;
case 3:if(isEmpty())
printf("\nStack Underflow.");
else printf("\nThe top element is %d",top->data)
;
break;
case 4:traverse();
break;
case 5:if(size<5) printf
("\nToo Few Elements...");
else{
int x1,x2;
printf("\n1.Count_Num\n2.Count_Non_Zero\n3.Count_Unique_Num\
n4.Add_Num\n\nEnter Your Choice: ");
scanf("%d",&x2);
switch(x2){
case 1:printf("\nEnter the no to be counted: ");
scanf("%d",&x1);
printf("\nNo of given no is %d.",count_num(x1));
break;
case 2:printf("\nNo of non zero elements is %d.",count_n
on_zero());
break;
case 3:printf("\nNo of unique elements is %d",count_uniq

ue_num());
break;
case 4:printf("\nEnter a no: ");
scanf("%d",&x1);
add_num(x1);
printf("Given no added to the stack.");
}
}
break;
case 6:exit(0);
}
}
return(0);
}
void push(int item)
{
struct node *temp=(struct node*) malloc(sizeof(struct node));
temp->data=item;
temp->next=top;
top=temp;
printf("\nItem successfully entered.");
size++;
}
void push1(int item)
{
struct node *temp=(struct node*) malloc(sizeof(struct node));
temp->data=item;
temp->next=top1;
top1=temp;
size1++;
}
int pop()
{
struct node *ptr=top;
int value=top->data;
top=top->next;
free(ptr);
size--;
return(value);
}
int isEmpty()
{
if(top==NULL)
return 1;
return 0;
}
void traverse()
{
if(isEmpty()){
printf("\nStack Underflow");
return;
}
struct node *ptr=top;
printf("\nThe elements in the stack are:\n");
while(ptr!=NULL){
printf("%d ",ptr->data);

ptr=ptr->next;
}
}
int count_num(int num){
struct node *ptr=top;
int count=0;
while(ptr!=NULL){
if(ptr->data==num)
count++;
ptr=ptr->next;
}
return(count);
}
int count_num1(int num){
struct node *ptr=top1;
int count=0;
while(ptr!=NULL){
if(ptr->data==num)
count++;
ptr=ptr->next;
}
return(count);
}
int count_non_zero(){
struct node *ptr=top;
int count=0;
while(ptr!=NULL){
if(ptr->data!=0)
count++;
ptr=ptr->next;
}
return(count);
}
int count_unique_num(){
struct node*ptr=top;
while(ptr!=NULL){
if(count_num1(ptr->data)==0)
push1(ptr->data);
ptr=ptr->next;
}
return(size1);
}
void add_num(int num){
struct node *ptr=top;
while(ptr!=NULL){
ptr->data+=num;
ptr=ptr->next;
}
}
5g4thg453twrf

You might also like