Stack all possible operations-TCPTUTOR

8
112

Stack all possible operations

Source code

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct arraystack
{ int top;
int capacity;
int *array;
};
struct arraystack *createstack(int cap)
{ struct arraystack *stack;
stack=malloc(sizeof(struct arraystack));
stack->capacity=cap;
stack->top=-1;
stack->array=malloc(sizeof(struct arraystack));
return(stack);
}
int isfull(struct arraystack *stack)
{ if(stack->top==stack->capacity-1)
return(1);
else
return(0);
}
int isempty(struct arraystack *stack)
{ if(stack->top==-1)
return(1);
else
return(0);
}
void push(struct arraystack *stack,int item)
{ stack->top++;
stack->array[stack->top]=item;
}
int pop(struct arraystack *stack)
{ int item;
if(!isempty(stack))
{ item=stack->array[stack->top];
stack->top–;
return(item);
}
return(-1);
}
void display(struct arraystack *stack)
{ int tap;
tap=stack->top;
while(tap>-1)
{ printf(“%d,”,stack->array[tap]);
tap–;
}
}
void del(struct arraystack *stack)
{
while(!isempty(stack))
{ printf(“%d,”,stack->array[stack->top]);
stack->top–;
}
}
void capa(struct arraystack *stack)
{ int cap;
printf(“last capacity is:%d\n”,stack->capacity);
printf(“Enter new capacity:”);
scanf(“%d”,&cap);
if(stack->capacity>cap)
{ printf(“Poped values are:”);
while(cap<=stack->top)
{ printf(“%d,”,stack->array[stack->top]);
stack->top–;
}
stack->capacity=cap;
}
else
stack->capacity=cap;

}
void max(struct arraystack *stack)
{ int tap,maxi;
maxi=stack->array[stack->top];
tap=stack->top;
while(tap>-1)
{ if(maxi<=stack->array[tap])
maxi=stack->array[tap];
tap–;
}
printf(“Maximum value is:%d”,maxi);
}
void min(struct arraystack *stack)
{ int tap,mini;
mini=stack->array[stack->top];
tap=stack->top;
while(tap>-1)
{ if(mini>=stack->array[tap])
mini=stack->array[tap];
tap–;
}
printf(“Minimum value is:%d”,mini);
}
void sum(struct arraystack *stack)
{ int tap,sum=0;
tap=stack->top;
while(tap>-1)
{ sum=sum+stack->array[tap];
tap–;
}
printf(“Sum=%d”,sum);
}
void divi(struct arraystack *stack,int number)
{ int tap,c=0;
tap=stack->top;
while(tap>-1)
{ if(stack->array[tap]%number==0)
{ c++;
}
tap–;
}
printf(“\nTotal numbers of element divisible by%d:%d”,number,c);
if(c!=0)
{ tap=stack->top;
printf(“\nAnd the numbers are:”);
while(tap>-1)
{ if(stack->array[tap]%number==0)
{ printf(“%d,”,stack->array[tap]);
}
tap–;
}
}
else
printf(“No such numbers found”);
}
void main()
{ int choice,item,capacity,maxi,mini,num;
struct arraystack *stack;
printf(“Enter the capacity of stack:”);
scanf(“%d”,&capacity);
stack=createstack(capacity);
while(1)
{ printf(“\n1.PUSH”);
printf(“\n2.POP”);
printf(“\n3.Display”);
printf(“\n4.Delete stack”);
printf(“\n5.Change capatity”);
printf(“\n6.Maximum value of stack”);
printf(“\n7.Minimum vlaue of stack”);
printf(“\n8.To find the sum”);
printf(“\n9.Total numbers divisible by given number”);
printf(“\n10.Exit”);
printf(“\nEnter your choice:”);
scanf(“%d”,&choice);
system(“cls”);
switch(choice)
{ case 1:
if(!isfull(stack))
{ printf(“\nEnter a number:”);
scanf(“%d”,&item);
push(stack,item);
}
else
printf(“Stack is full”);
break;
case 2:
item=pop(stack);
if(item==-1)
printf(“\nStack is empty”);
else
printf(“\npoped value is %d”,item);
break;
case 3:
if(!isempty(stack))
display(stack);
else
printf(“Stack is empty”);
break;
case 4:
if(!isempty(stack))
{ printf(“Deleted values are:”);
del(stack);
}
else
printf(“Stack is empty”);
break;
case 5:
capa(stack);
break;
case 6:
if(!isempty(stack))
max(stack);
else
printf(“Stack is empty”);
break;
case 7:
if(!isempty(stack))
min(stack);
else
printf(“Stack is empty”);
break;
case 8:
if(!isempty(stack))
sum(stack);
else
printf(“Stack is empty”);
break;
case 9:
if(!isempty(stack))
{ printf(“Enter the number you wants to find the divisions:”);
fflush(stdin);
scanf(“%d”,&num);
divi(stack,num);
}
else
printf(“Stack is empty”);
break;
case 10:
exit(0);
default:
printf(“Enter correct choice”);
}
}
getch();
}

8 COMMENTS

  1. 不會令牙齒變黃的「透明咖啡」運動前來一口吧! Marie Claire (HK) Edition 有很多人在做運動前喜歡喝杯黑咖啡,來增加血液中的脂肪酸濃度,從而有效地將脂肪酸變成熱能,燃燒更多的脂肪。然而,經常喝咖啡會令牙齒變黃,有損儀容。為了解決這個問題,英國一家公司 CLRCFF 研發了一款

LEAVE A REPLY

Please enter your comment!
Please enter your name here