Following is a program I made in C for implementation of stack using array and pointers:
#include<stdio.h>
#include<stdlib.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(int) * stack->capacity));
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) {
if(!isFull(stack)) {
stack->top++;
stack->array[stack->top] = item;
} else {
printf("No more memory available!");
}
}
void pop(struct ArrayStack *stack) {
int item;
if(!isEmpty(stack)) {
item = stack->array[stack->top];
stack->top--;
} else {
printf("Memory is already empty!");
}
}
int main() {
struct ArrayStack *stack;
stack = createStack(10);
int choise;
int item;
while(1) {
system("clear");
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Exit");
printf("\n\n\n\t\tPlease choose your option!");
scanf("%d",&choise);
switch(choise) {
case 1:
printf("\nEnter a number");
scanf("%d",&item);
push(stack,item);
break;
case 2:
pop(stack);
break;
case 3:
exit(0);
break;
default :
printf("\nPlease enter a valid choise!");
break;
}
}
}
The following error is coming whenever I try to compile this code using gcc compiler:
prog.c:10:25: error: invalid application of 'sizeof' to incomplete type 'struct Arraystack'
stack = malloc(sizeof(struct Arraystack));
^
prog.c:13:3: error: called object is not a function or function pointer
stack->array(malloc(sizeof(int) * stack->capacity));
^
I have used online IDEs like ideone and codechef's ide but same error is coming again. I am totally struck, this is really annoying!
First your errors:
stack = malloc(sizeof(struct ArrayStack));
You typed Arraystack (lower case s).
stack->array=malloc(sizeof(int) * stack->capacity);
You typed stack->array(malloc(sizeof(int) * stack->capacity)); which is syntactically a function call which is why the compiler complains about array not being a function pointer.
In addition:
Introduce a function void destroyStack(ArrayStack* stack) to free() the malloc()ed space in createStack(). Call it towards the end of main() when you've finished with the stack.
Always render unto free() what malloc() rendered unto thee.
Your pop() doesn't return the popped value.
You should probably return values indicating failure when push() and pop() fail.
Related
I have initialised two stacks using a structure with which I am creating a queue. But the stack is not able to store the values which is why enqueue or dequeue operations are not working properly.
Here is the code:
#include<stdio.h>
#include<stdlib.h>
struct stack{
int top;
int size;
int *s;
};
int isfull(struct stack *st){
if(st->top==st->size-1){
return 1;
}
return 0;
}
int isempty(struct stack *st){
if(st->top==-1){
return 1;
}
return 0;
}
void push(struct stack *st,int x){
if(isfull(st)){
printf("FULL!!\n");
}
else{
st->top++;
st->s[st->top]=x;
}
}
int pop(struct stack *st){
int x=-1;
if(isempty(st)){
printf("EMPTY!!\n");
}
else{
x=st->s[st->top];
st->top--;
}
return x;
}
void enqueue(struct stack s1,int x){
push(&s1,x);
}
int dequeue(struct stack s1,struct stack s2){
int x=-1;
if(isempty(&s2)){
if(isempty(&s1)){
printf("QUEUE IS EMPTY!!\n");
return x;
}
else{
while(!isempty(&s1)){
push(&s2,pop(&s1));
}
}
}
return pop(&s2);
}
void display(struct stack st){
int i;
for(i=0;i<=st.top;i++){
printf("%d",st.s[i]);
}
}
int main(){
int n,choice;
struct stack s1,s2;
printf("ENTER SIZE OF QUEUE:");
scanf("%d",&n);
s1.size=n;
s2.size=n;
s1.top=-1;
s2.top=-1;
s1.s=(int *)malloc(s1.size*sizeof(int));
s2.s=(int *)malloc(s2.size*sizeof(int));
while(1){
printf("1.ENQUEUE\n");
printf("2.DEQUEUE\n");
printf("3.DISPLAY\n");
printf("4.EXIT\n");
printf("ENTER YOUR CHOICE:");
scanf("%d",&choice);
switch(choice){
case(1):
int x;
printf("ENTER DATA:");
scanf("%d",&x);
enqueue(s1,x);
break;
case(2):
int m;
m=dequeue(s1,s2);
printf("ELEMENT DELETED IS:%d\n",m);
break;
case(3):
display(s2);
break;
case(4):
exit(0);
}
}
return 0;
}
What is the error? I think there might be an issue with passing the values to the function.
The main issue is that the enqueue and dequeue don't take pointers as arguments, but struct stack. This means the function gets a copy of the given struct, and that the pointer you pass to push and pop (like &s1) is pointing to that local structure, not to the one in main. By consequence any update to the top member of that stack will not be seen by the caller.
I would suggest to:
Consistently pass pointers to struct typed arguments. This was well done for the push and pop functions, and there is no reason why it should not be done the same way for enqueue and dequeue functions.
Define a struct queue so that you abstract a bit that there are two stacks involved and don't have to pass both of them as argument to dequeue.
Create separate functions for:
creating a new stack
displaying a stack
creating a new queue
displaying a queue
checking if a queue is empty
Here is how your code would then look:
#include <stdio.h>
#include <stdlib.h>
struct stack {
int top;
int size;
int *s;
};
struct stack* newstack(int size) {
struct stack *s = malloc(sizeof(struct stack));
s->size = size;
s->s = malloc(size*sizeof(int));
s->top = -1;
return s;
}
int isfull(struct stack *st) {
return st->top == st->size - 1;
}
int isempty(struct stack *st) {
return st->top == -1;
}
void push(struct stack *st, int x) {
if (isfull(st)){
printf("Full!\n");
} else {
st->top++;
st->s[st->top] = x;
}
}
int pop(struct stack *st) {
int x = -1;
if (isempty(st)){
printf("Empty!\n");
} else {
x = st->s[st->top];
st->top--;
}
return x;
}
void displaystack(struct stack *st) {
for(int i = 0; i <= st->top; i++) {
printf("%d ", st->s[i]);
}
}
struct queue {
struct stack *s1;
struct stack *s2;
};
struct queue* newqueue(int size) {
struct queue *q = malloc(sizeof(struct queue));
q->s1 = newstack(size);
q->s2 = newstack(size);
return q;
}
int isemptyqueue(struct queue *q) {
return isempty(q->s1) && isempty(q->s2);
}
void enqueue(struct queue *q, int x) {
push(q->s1, x);
}
int dequeue(struct queue *q) {
int x = -1;
if (isemptyqueue(q)) {
printf("Queue is empty!\n");
return -1;
}
if (isempty(q->s2)) {
while (!isempty(q->s1)) {
push(q->s2, pop(q->s1));
}
}
return pop(q->s2);
}
void displayqueue(struct queue *q) {
displaystack(q->s1);
printf("| ");
displaystack(q->s2);
printf("\n");
}
int main() {
int n, choice, x, m;
printf("Enter the size of the queue: ");
scanf("%d", &n);
struct queue *q = newqueue(n);
while (choice != 4) {
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data: ");
scanf("%d", &x);
enqueue(q, x);
break;
case 2:
m = dequeue(q);
printf("The deleted element is: %d\n", m);
break;
case 3:
displayqueue(q);
break;
}
}
return 0;
}
This is a menu-driven program that carries out basic stack operations using arrays in the C programming language. The functions that are performed are push, pop, peep,isempty and isfull.
#include<stdio.h>
#include<stdlib.h>
struct stack
{
long int top;
long int size;
char* key;
};
int is_empty(struct stack *s) //check if its empty
{
if(s->top==-1)
{
return -1;
}
else
{
return 1;
}
}
int is_full(struct stack *s) //check if its full
{
if (s->top ==s->size-1)
{
return -1;
}
else
{
return 1;
}
}
void push(struct stack *s, char x) //pushes into stack
{
int check;
check = is_full(s);
if(check==-1)
{
printf("-1\n");
}
else
{
s->top = s->top+1;
s->key[s->top]=x;
}
}
void pop(struct stack *s) //deletes the last element
{
int check;
check = is_empty(s);
if(check==-1)
{
printf("-1\n");
}
else
{
char k;
k = s->key[s->top];
printf("%c\n",k);
s->top--;
}
}
void peep(struct stack *s) //prints the last element without deleting
{ int check;
char k;
check = is_empty(s);
if (check == -1)
{
printf("-1\n");
}
else
{
k = s->key[s->top];
printf("%c \n",k);
}
}
int main()
{
char ch;
char x;
long int n;
struct stack *s;
scanf("%ld ", &n);
s->size = n; //initialise the size
s->top = -1; //setting as -1 base case
s->key= (char *)malloc(n*sizeof(char)); //dynamic allocation of keys
while(1)
{
scanf("%c ",&ch);
switch(ch)
{
case 'i':
scanf("%c ",&x);
push(s,x);
break;
case 'd':pop(s);
break;
case 'p':peep(s);
break;
case 't':exit(0); //termination case
}
}
return 0;
}
This is a C program that is working for me in some online compilers but in VScode and other compilers, it's showing a segmentation fault without any output. This is an implementation of stack using arrays. Is it a problem with any of the scanf functions?
You have created a pointer variable s and then access the size field on that struct.
struct stack *s;
scanf("%ld ", &n);
s->size = n; //initialise the size
Except s doesn't actually point to anything at this point. You need to either statically or dynamically allocate memory for that struct.
struct stack s;
Or:
struct stack *s = malloc(sizeof(struct stack));
I was trying to make a simple Linked List program, also when I'm trying to pop the first element from the list , it's not popping and it still remains the first element in the list, please help me resolve this error.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
void create(stack *s){
if(s == NULL){
s = (stack*)malloc(sizeof(stack)*1);
(s->next)=NULL;
}
else{
stack *temp = (stack*)malloc(sizeof(stack)*1);
(temp->next)=s;
s=temp;
}
}
void push(stack *s, char x){
create(s);
(s->value)=x;
}
void isEmpty(stack *s){
if(s == NULL){
printf("List is Empty!\n");
}
else{
printf("List is not Empty!\n");
}
}
char pop(stack *s){
if(s == NULL){
isEmpty(s);
return -1;
}
char x=s->value;
s=(s->next);
return x;
}
int main(int argc , char* argv[]){
stack *s;
create(s);
char choice,data;
printf("Stack Created\n\n");
do{
printf("Choose Option: pUsh, pOp, pEek, iseMpty, getSize, eXit: \n");
scanf(" %c",&choice);
switch(choice){
case 'U':{
printf("Enter the element to be pushed: \n");
scanf(" %c",&data);
push(s, data);
break;
}
case 'O':{
data=pop(s);
if(data != NULL){
printf("Popped: %c\n", data);
}
break;
}
}
}while(1);
return 0;
}
The line s=s->next; has no effect because s is a local varaible. You need to return the new value of s or use pointers to modify the caller's version.
I changed the argument to pop() and push() from stack * to stack **, so that we can update the stack not a local variable. I removed create() as it is basically what happens during the push and I integrated it with that.
The rest is straightforward and I also added the free() call in pop(). Take a look:
#include <stdio.h>
#include <stdlib.h>
typedef struct stack{
int value;
struct stack *next;
}stack;
void push(stack **s, int x){
stack *temp = (stack*)malloc(sizeof(stack)*1);
temp->value = x;
temp->next = NULL;
if(*s == NULL){
*s = temp;
}else{
temp->next = *s;
*s=temp;
}
}
char pop(stack **s){
if(*s == NULL){
return -1;
}
char x=(*s)->value;
stack *tmp = *s;
*s=(*s)->next;
free(tmp);
return x;
}
int main(int argc , char* argv[]){
stack *s;
char choice,data;
printf("Stack Created\n\n");
do{
printf("Choose Option: pUsh, pOp, pEek, iseMpty, getSize, eXit: \n");
scanf(" %c",&choice);
switch(choice){
case 'U':{
printf("Enter the element to be pushed: \n");
scanf(" %c",&data);
push(&s, data);
break;
}
case 'O':{
data=pop(&s);
if(data != -1){
printf("Popped: %c\n", data);
} else {
printf("Stack is empty. nothing popped");
}
break;
}
}
}while(1);
return 0;
}
Here you have to use pointer to pointer if u want to pass pointer as an argument
char pop(stack **s)
{
**s=s->next;
}
this is a temporary solution u should also consider deleting the memory you allocated using the malloc function or else it will lead to memory leak
also pass the address of the pointer s while you call it in the pop function
Guys what is wrong with this program. I am having problems with pop operation, it shows an extra value even after stack is empty. ??
void initstack (struct stack * p, int maxSize)
void push (struct stack * p, int item)
int pop (struct stack * p)
void display (struct stack p)
struct stack
{
int * a;
int top;
int maxSize;
};
Note:using d above structure and functions are mandatory..
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct stack {
int * a;
int top;
int maxSize;
};
void initstack(struct stack * p, int maxSize);
void push(struct stack * p, int item);
int pop(struct stack * p);
void display(struct stack p);
int StackIsEmpty(struct stack * p);
int StackIsFull(struct stack * p);
void printMenu();
int main() {
struct stack p;
int data,ch, data1, m;
printf("Enter the maximum size of the stack\n");
scanf("%d",&m);
initstack(&p,m);
do {
printMenu();
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch) {
case 1:
printf("Enter the element to be pushed\n");
scanf("%d",&data);
push(&p, data);
break;
case 2:
data1 = pop(&p);
if(data1 != -1000)
printf("The popped element is %d\n",data1);
break;
case 3:
printf("The contents of the stack are");
display(p);
printf("\n");
break;
default:
exit(0);
}
} while(1);
return 0;
}
void printMenu()
{
printf("Choice 1 : Push\n");
printf("Choice 2 : Pop\n");
printf("Choice 3 : Display\n");
printf("Any other choice : Exit\n");
}
void initstack(struct stack * p, int maxSize) {
int *newContents;
newContents=(int *)malloc(sizeof(int)*maxSize);
p->a=newContents;
p->maxSize=maxSize;
p->top=-1;
}
void push(struct stack * p, int item) {
if(StackIsFull(p))
{
printf("Stack is full\n");
}
p->a[++p->top]=item;
}
void display(struct stack p) {
int i;
struct stack *b=&p;
if(StackIsEmpty(b))
printf(" {}");
for(i=0;i<b->top;i++)
{
printf(" %d",b->a[i]);
}
}
int pop(struct stack * p) {
if(StackIsEmpty(p))
{
printf("Stack is empty\n");
return -1000;
}
else
return p->a[--p->top];
}
int StackIsEmpty(struct stack *p)
{
return p->top == -1; //p->top==-1;
}
int StackIsFull(struct stack *p)
{
return p->top >= p->maxSize-1;
}
Let's look at your push and pop operations:
p->a[++p->top]=item; // push
p->a[--p->top]; // pop
Let's assume the stack is empty and top is -1. When you do a push, you increment top to 0 and write your element to p->a[0]. When you pop that element, you first decrement top back to -1, and then try to access the element p->a[-1].
This is a problem. Not only are you popping the wrong element, you're accessing an element outside the range of your array and invoking undefined behavior.
You need to change the stack pointer after you access the element you want, like so:
p->a[++p->top] = item; // push
item = p->a[p->top--]; // pop
For array-based stacks, it's actually a little more natural for the stack to grow "downwards", like so:
p->top = p->maxSize = maxSize; // init
if ( p->top ) // p->top != 0 means room left on the stack
p->a[--p->top] = item; // push
if ( p->top < p->maxSize ) // p->top < p->maxSize means elements left on stack
return p->a[p->top++]; // pop
This way, you don't run the risk of accessing an element outside the range of the array. p->top will always be between 0 and maxSize - 1.
Finally, a style note:
You don't need to cast the result of malloc; it just adds visual clutter, and in some cases can suppress a useful diagnostic. You can clean it up by simply writing:
/**
* Whitespace is your friend. Use it.
*/
newContents = malloc( sizeof *newContents * maxSize );
sizeof *newContents is the same as sizeof (int); this way, if you ever decide to change the type of the stack array, you don't have to worry about changing the malloc call itself. Saves some maintenance headaches, reads a little easier.
Edit
Here's part of what's causing your headaches:
void push(struct stack * p, int item) {
if(StackIsFull(p))
{
printf("Stack is full\n");
}
p->a[++p->top]=item; // DANGER WILL ROBINSON!
}
If the stack is full you print a warning, and then you push the element anyway.
You need an else branch in there
void push(struct stack * p, int item)
{
if(StackIsFull(p))
{
printf("Stack is full\n");
}
else
{
p->a[++p->top]=item;
}
}
Thanks guys, I fixed it.. works fine. thanks for all ur suggestions.
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct stack {
int * a;
int top;
int maxSize;
};
void initstack(struct stack * p, int maxSize);
void push(struct stack * p, int item);
int pop(struct stack * p);
void display(struct stack p);
int StackIsEmpty(struct stack * p);
int StackIsFull(struct stack * p);
void printMenu();
int main() {
struct stack p;
int data,ch, data1, m;
printf("Enter the maximum size of the stack\n");
scanf("%d",&m);
initstack(&p,m);
do {
printMenu();
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch) {
case 1:
printf("Enter the element to be pushed\n");
scanf("%d",&data);
push(&p, data);
break;
case 2:
data1 = pop(&p);
if(data1 != -1000)
printf("The popped element is %d\n",data1);
break;
case 3:
printf("The contents of the stack are");
display(p);
printf("\n");
break;
default:
exit(0);
}
} while(1);
return 0;
}
void printMenu()
{
printf("Choice 1 : Push\n");
printf("Choice 2 : Pop\n");
printf("Choice 3 : Display\n");
printf("Any other choice : Exit\n");
}
void initstack(struct stack * p, int maxSize) {
int *newContents;
newContents=malloc(sizeof(int)*maxSize);
p->a=newContents;
p->maxSize=maxSize;
p->top=-1;
}
void push(struct stack * p, int item) {
if(StackIsFull(p))
{
printf("Stack is full\n");
}
else
{
p->a[++p->top]=item; //FIXED LINE, ELSE BLOCK ADDED
}
}
void display(struct stack p) {
int i;
struct stack *b=&p;
if(StackIsEmpty(b))
printf(" {}");
for(i=0;i<=b->top;i++) //FIXED PREVIOUSLY for(i=0;i<b->top;i++)
{
printf(" %d",b->a[i]);
}
}
int pop(struct stack * p) {
if(StackIsEmpty(p))
{
printf("Stack is empty\n");
return -1000;
}
else
return p->a[p->top--]; //FIXED PREVIOUSLY p->a[--p->top];
}
int StackIsEmpty(struct stack *p)
{
return p->top < 0; //FIXED PREVIOUSLY p->top==-1;
}
int StackIsFull(struct stack *p)
{
return p->top >= p->maxSize-1;
}
Your display logic is faulty. It has an off-by-one error and skips the topmost element.
return p->a[--p->top];
In this part I think you should first
int result = p->a[p->top];
then
p->top --;
return result;
I had same issue and that helped me.
It hink the problem is related to push and pop functions. Both use preincrements, so the last item you push is not the first item you pop.
Your push does: first increment top, then store value in a[top]. So top is pointing to the newly pushed element.
Your pop does: first decrement top, then retrieve the value at a[top]. The value retrieved won't be the last pushed, but the previous-to-last. top keeps pointing at this very same value.
My suggestion: leave push as is, and change pop as this:
int pop(struct stack * p) {
if(StackIsEmpty(p))
{
printf("Stack is empty\n");
return -1000;
}
else
return p->a[p->top--]; /* post-increment!! */
}
So, push will leave top pointing at the last pushed value. pop will retrieve this value, then decrement top, leaving it pointing at the next value to retrieve.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am creating a stack. But when trying to push data into the stack it is having a runtime crash.Can you explain why is this happening and provide me the correct code? This is my program-
#include<stdio.h>
#include<stdlib.h>
struct ArrayStack* create_stack(void);
int IsStackFull(struct ArrayStack *);
int IsStackEmpty(struct ArrayStack *);
struct ArrayStack * push(struct ArrayStack *,int );
struct ArrayStack
{
int capacity;
int top;
int *array;
};
int main()
{
int choice1=0;
int choice=0,data=0,var=0;
struct ArrayStack *s=create_stack();
printf("\n STACK CREATED");
do
{
printf("\n 1= Test If Stack Is Empty or not");
printf("\n 2= Test If Stack Is Full Or Not");
printf("\n 3= Push Element Into The Stack");
printf("\n 4= Pop Element from the stack");
printf("\n\n Enter yOur choice:: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
var=IsStackEmpty(s);
if(var)
{
printf("\n Yes Stack Is eMpty for Now");
break;
}
else
{
printf("\n No Stack Is Not Empty ");
break;
}
}
case 2:
{
var=IsStackFull(s);
if(var)
{
printf("\n Yes Stack Is Full for Now");
break;
}
else
{
printf("\n No Stack Is Not Full ");
break;
}
}
case 3:
{
printf("\n Provide The Input For stack::");
scanf("%d",&data);
struct ArrayStack *s=push(s,data);
printf("\n Element inerted into the Stack");
break;
}
case 4:
{
var=pop(s);
if(var)
{
printf("\n Removed The element from the Stack");
break;
}
break;
}
default:
{
printf("\n Wrong Input");
}
}
printf("\n Do you want to countinue(1 for yes/0 for no):: ");
scanf("%d",&choice1);
}while(choice1);
}
struct ArrayStack* create_stack()
{
struct ArrayStack *s=(struct ArrayStack *)malloc(sizeof(struct ArrayStack));
if(!s)
{
printf("\nNot enough Memory");
return NULL;
}
s->capacity=4;
s->top=-1;
s->array=(int *)malloc(s->capacity*sizeof(int));
if(!s->array)
{
return NULL;
}
else
return s;
}
int IsStackEmpty(struct ArrayStack *s)
{
if(s->top=-1)
return 1;
else
return 0;
}
int IsStackFull(struct ArrayStack *s)
{
if(s->top==s->capacity-1)
{
return 1;
}
return 0;
}
struct ArrayStack * push(struct ArrayStack *s,int data)
{
if(IsStackFull(s))
{
printf("\n Sorry. The stack is full already");
}
else
{
s->array[++s->top]=data;
printf("\n Element Inserted Successfully");
return s;
}
}
int pop(struct ArrayStack *s)
{
if(IsStackEmpty(s))
{
printf("\n Sorry. Trying to pop element from an empty stack");
return 0;
}
else
{
s->array=s->array[s->top--];
return 1;
}
}
In your Push element case, you are creating one local instance of s, which is confusing with s you have created before do..while loop.
Change code in switch case 3 to
s=push(s,data); //(remove struct ArrayStack *)
Your if test in IsStackEmpty() is doomed. With
if(s->top=-1)
You are assigning instead of comparing. This always evaluates to true, so your IsStackEmpty() function always says the stack is empty. Use == instead.
Your pop function is wrong. You don't want to change the pointer returned by malloc, given your structure definition, I believe you would want to decrement top, and return the popped element (notice you never return the popped element...)
int pop(struct ArrayStack *s)
{
if(IsStackEmpty(s))
{
printf("\n Sorry. Trying to pop element from an empty stack");
return 0;
}
else
{
return s->top--;
}
}
This assumes that 0 is never pushed, so you can distinguish error conditions from normal flow. Otherwise, you'll have to find some other way to report errors.
case 3:
{
printf("\n Provide The Input For stack::");
scanf("%d",&data);
struct ArrayStack *s=push(s,data);
printf("\n Element inerted into the Stack");
break;
}
In above code, ArrayStack s is not initialised and it is used in IsStackEmpty() functions , so it crashes with access violation, use the ArrayStack s already initialised in main program using Create_Stack()
struct ArrayStack *s=create_stack();