I am having some trouble with my program. I am trying to print out my queue but its just printing garbage values. Hopefully can maybe take a few minutes to go over the code and see what is going wrong. I know the actual program really doesn't make sense but this is the way I need to do it. Thanks
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
char *name;
int number;
} SomeSeq;
typedef struct QueueNodeTag{
SomeSeq *item;
struct QueueNodeTag *link;
}QueueNode;
typedef struct{
QueueNode *front;
QueueNode *rear;
}Queue;
//function prototypes
void getbuf(Queue **Q,QueueNode **Temp);
int GetNumberOfSomeSeqsToGenerate();
SomeSeq * GetNextSomeSeq(void);
void Print(void);
int main()
{
/* initialize random seed: */
srand ( time(NULL) );
Print();
}
void getbuf(Queue **Q,QueueNode **Temp)
{
int n = GetNumberOfSomeSeqsToGenerate();
//creates as many SomeSeqs as user requests and
//adds them to the list
for(int i =0;i<n;i++)
{
(*Temp) = (QueueNode*)malloc(sizeof(QueueNode));
if ((*Temp) == NULL)
{
return;// (char*) buff;
}else{
(*Temp)->item = GetNextSomeSeq();
(*Temp)->link = NULL;
if ((*Q)->rear == NULL){
(*Q)->front = (*Temp);
(*Q)->rear = (*Temp);
}else{
(*Q)->rear->link = (*Temp);
(*Q)->rear = (*Temp);
}
}
}
}
//
int GetNumberOfSomeSeqsToGenerate()
{
int n = 0;
printf("GetNumberOfSomeSeqsToGenerate():");
scanf("%d",&n);
return n;
}
// Return the next structure (to fill the
// abovementioned list with).
SomeSeq * GetNextSomeSeq()
{
SomeSeq p;
int names;
p.number = rand() % 100 + 1;
names = rand() % 10 + 1 ;
switch(names)
{
case 1:
p.name="John";
break;
case 2:
p.name="Mike";
break;
case 3:
p.name="Sally";
break;
case 4:
p.name="Samus";
break;
case 5:
p.name ="Epona";
break;
case 6:
p.name = "JJ";
break;
case 7:
p.name = "Matt";
break;
case 8:
p.name = "Sandy";
break;
case 9:
p.name="Ken";
break;
case 10:
p.name="Todd";
break;
}
return &p ;
}
void Print()
{
Queue *Q;
Q =(Queue*)malloc(sizeof(Queue));
Q->front=NULL;
Q->rear=NULL;
QueueNode *Temp;
Temp = NULL;
getbuf(&Q,&Temp);
while(Q->front->link != NULL)
{
printf("%d %s\n",Q->front->item->number,Q->front->item->name);
//Its printing garbage values here
Q->front = Q->front->link;
}
free(Temp);
free(Q);
}