i tried everything but still cant get it working. what i'm trying to do is make an array and enter values in it. when all the values have been inserted i want it to return the values in the order that the first value should come out first. i was able to make a code for a stack process in which the last value comes out first and then the next. i'm pasting both my codes but i dunt know where i'm making a mistake in the queue code. pls help me out.
queue code:
Code:
#include<stdio.h>
#define SIZE 2
struct queue
{
int term[SIZE];
int rear;
int front;
};
void initqueue( struct queue*);
void join(int, struct queue*);
int leave( struct queue*);
int main()
{
struct queue q;
int a, var;
initqueue(&q);
while(1)
{
printf("press 1 to join value press 2 to leave value, 3 to exit \n");
scanf("%d", &a);
switch(a)
{
case 1 :
scanf("%d", &var);
join(var, &q);
break;
case 2:
printf("leave value: %d \n", leave(&q));
break;
case 3:
return 0;
}
}
}
void join(int a, struct queue *q)
{
if(q->rear == SIZE)
{
q->term[q->rear]=a;
q->rear--;
}
if(q->rear == 0)
{
printf ( "queue is full");
}
}
int leave(struct queue*q)
{
if(q->front == q->rear )
{
printf("queue is empty");
}
else
{
q->front--;
return q->term[q->front];
}
}
void initqueue(struct queue*q)
{
q->front = SIZE;
q->rear = SIZE;
}
stack code:
Code:
#include<stdio.h>
#define SIZE 5
struct stack
{
int term[SIZE];
int top;
};
void initstack( struct stack*);
void push(int, struct stack*);
int pop( struct stack*);
int main()
{
struct stack st;
int a, var;
initstack(&st );
while(1)
{
printf("press 1 to push value press 2 to pop value, 3 to exit \n");
scanf("%d", &a);
switch(a)
{
case 1 :
scanf("%d", &var);
push(var, &st);
break;
case 2:
printf("pop value: %d \n", pop(&st));
break;
case 3:
return 0;
}
}
}
void push(int a, struct stack*st)
{
if(st->top == SIZE)
printf ( "Stack is full");
else{
st->term[st->top]=a;
st->top++;
}
}
int pop(struct stack*st)
{
if(st->top == 0 )
printf("Stack is empty");
else{
st->top--;
return st->term[st->top];
}
}
void initstack(struct stack*st)
{
st->top = 0;
}