Here is improved version of my queue problem. To make it data nutural I'm passing the pointer to double that supposed to be converted to pointer to void impicitely.(See QueAdd function in the code) It still does not work. I can not get any values into the array, or I can not print them out. I'm not sure.
Thanks for your time,
Pete
PS(I programming in C/C++);
Here is the code:
//Peter Yelagin
//Queue4.c
#include <stdio.h>
#include<stdlib.h>
typedef struct queuetemplate
{
void** array;
int size;
int first;
int last;
}QUE;
QUE* QueConstruct(int Size);
void QueAdd(QUE *Q, void* Value);
void QueRemove(QUE *Q, void* Destination);
void QueDestruct(QUE *Q);
int QueIsFull(QUE *Q);
int QueIsEmpty(QUE *Q);
int main(void)
{
double Destination;
double Value;
double* pVal;
char command[60];
int size=5;
QUE* pQue;
pQue=QueConstruct(size);
for(;
{
fgets(command, sizeof(command), stdin);
if(feof(stdin)) break;
if(command[0]=='a')
{
if(QueIsFull(pQue)) printf("Queue is full.\n");
else
{
pVal=(double*)malloc(sizeof(double));
Value=atof(command+1);
pVal=&Value;
QueAdd(pQue, pVal);
}
}
else if(command[0]=='r')
{
printf("%d\n", pQue->last);
if(QueIsEmpty(pQue)) printf("Queue is empty.\n");
else QueRemove(pQue, &Destination);
}
else continue;
}
for(int i=0; i<size; i++)
{
printf("%fd\n", *((double*)(pQue->array+i)));
}
QueDestruct(pQue);
return 0;
}
QUE* QueConstruct(int Size)
{
QUE* que=(QUE*)malloc(sizeof(QUE));
if(que==NULL)
{
perror("Oueue");
exit(1);
}
que->array=(void**)malloc(sizeof(void*)*Size);
if(que->array==NULL)
{
perror("Queue");
exit(1);
}
que->size=Size;
que->first=-1;
que->last=-1;
return que;
}
void QueAdd(QUE *Q, void* Value)
{
if(Q->last+1>Q->size-1) Q->last=-1;
if(Q->last+1==Q->last) free(Value);
Q->last++;
Q->array[Q->last]=Value;//I think problem is here
printf("%d\n", Q->last);//test output
printf("%fd\n", *((double*)Value));//test output
printf("%fd\n", *(double*)(Q->array)); //test output
return;
}
void QueRemove(QUE *Q, void* Destination)
{
int next=Q->first;
if(next+1>Q->size-1) next=-1;
next++;
Destination=Q->array[next];
Q->first=next;
return;
}
void QueDestruct(QUE *Q)
{
free(Q->array);
free(Q);
return;
}
int QueIsFull(QUE* Q)
{
//int next=Q->last;
if(Q->last+1==Q->first) return 1;
else return 0;
}
int QueIsEmpty(QUE* Q)
{
printf("%d\n", Q->last); //test out
if(Q->last<0) return 1;
//else if(Q->first==Q->last) return 1;
else return 0;
}