Hi all i am learning C now and i want to make a program that for a simple stack (an integer array) i will insert elements (integers) in the stack until it will be full or remove them until it will be empty.Here is my code:
My problem is that here i declare the array and the index of the stack(head) as global variables but when i try to declare them as local variables in main and pass them to push and pop functions i can't get it to work.Any ideas?Code:#define N 2
int stack[N];
int head=0;
#include<stdio.h>
void push(int);
int pop(void);
int main()
{
int s,k;
do
{
printf("Give your selection\n");
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Finish\n");
scanf("%d",&s);
switch(s)
{
case 1:
{
printf("give an element to push in the stack:\n");
r=scanf("%d",&k);
if (r==1)
push(k);
else
{
printf("You don't give a valid number");
return 0;
}
break;
}
case 2:
{
pop();
break;
}
}
}
while(s!=3);
}
void push(int e)
{
if (head<N)
stack[head++]=e;
else
printf("STACK IS FULL\n");
}
int pop(void)
{
if(head>0)
return stack[--head];
else
printf("STACK IS EMPTY\n");
}