--- In [email protected], "meeda91" <meed...@...> wrote: > > The program ask the user to enter 5 numbers +ve and negative ... and the > program will put the positive numbers in a queue named pos and negative > numbers in queue named neg > the program works but when it reach the funciton push it stop working > need help please :) > > #include<stdio.h> > #include<stdlib.h> > > > struct queue /struct the queue > { > int count; > int front; > int rear; > int maxsize; > int *qarray; > }; > > > > queue * initialize(struct queue * q, int m) //state size of queue > { > q=(struct queue*)malloc(sizeof(struct queue)); > if(q!=NULL) > { > q->qarray=(int*)malloc(sizeof(int)*m); > q->count=0; > q->front=0; > q->rear=-1; > q->maxsize=m; > return q; > } > } > > void push(struct queue*q,int x) //push number in queue > { > q->count++; > q->rear=(q->rear+1)%(q->maxsize); > q->qarray[q->rear]=x; <----------------here the program stops > } > > int pop(struct queue*q) //take out a number frim queue > { > q->count--; > int x; > x=q->qarray[q->front]; > q->front=(q->front+1)%q->maxsize; > return(x); > } > > main() > { > struct queue pos,neg; > int countpos=0,countneg=0,i,x,a[5]; > printf("please enter 30 number:\n"); > for(i=0;i<5;i++) > { > scanf("%d",&a[i]); > if(a[i]>0) > countpos++; > else > countneg++; > } > initialize(&pos,countpos); > initialize(&neg,countneg); > for(i=0;i<5;i++) > { > if(a[i]>0) > push(&pos,a[i]); > else > push(&neg,a[i]); > } > for(i=1;i<=countpos;i++) > { > x=pop(&pos); > printf("%d",x); > } > for(i=1;i<=countneg;i++) > { > x=pop(&neg); > printf("%d",x); > } > } >
Hmmm. Maybe nothing really was allocated for q->array during the initialization of the queue, that's why accessing q->qarray[q->rear] in your function push() doesn't make sense. Check first if it is not null before assigning anything into it.
