Re: Print Method for Queue List
Author |
Message |
Shiro786
|
Posted: Sat Oct 15, 2011 3:47 pm Post subject: Re: Print Method for Queue List |
|
|
**EDIT**: I figured it out, you can delete this post. I'm leaving it just in case someone else needs help.
I'm trying to figure out how to print a list of Queues. For some reason whenever I try using ' -> ' it says that I'm using an invalid argument. The code is a bit weird; you have to compile an executable before you can run the program itself. My code is at the bottom.
QueueImplementation.c :
code: |
#include <stdio.h> /* the "QueueTypes.h" file, */
#include <stdlib.h> /* given above on lines 1:15, is */
#include "QueueInterface.h" /* included in "QueueInterface.h" */
/* on line 3 of Program 7.4. */
void SystemError(char *errorMsg) {fprintf(stderr,errorMsg);}
void InitializeQueue(Queue *Q)
{
Q->Front = NULL;
Q->Rear = NULL;
}
int Empty(Queue *Q)
{
return (Q->Front == NULL);
}
int Full(Queue *Q)
{ /* we assume an already constructed queue, Q, is */
return 0; /* not full, since it could potentially grow */
} /* as a linked structure */
int Insert(ItemType R, Queue *Q)
{
QueueNode *Temp;
/* attempt to allocate */
Temp = (QueueNode *) malloc(sizeof(QueueNode)); /* a new node */
if (Temp == NULL) { /* Temp = NULL signals allocation */
SystemError("system storage is exhausted"); /* failure */
return 0;
} else {
Temp->Item = R;
Temp->Link = NULL;
if ( Q->Rear == NULL ) {
Q->Front = Temp;
Q->Rear = Temp;
} else {
Q->Rear->Link = Temp;
Q->Rear = Temp;
}
}
return 1;
}
int Remove(Queue *Q, ItemType *F)
{
QueueNode *Temp;
if (Q->Front == NULL) {
SystemError("attempt to remove item from empty Queue");
return 0 ;
} else {
*F = Q->Front->Item;
Temp = Q->Front;
Q->Front = Temp->Link;
free(Temp);
if (Q->Front == NULL) Q->Rear = NULL;
return 1;
}
}
|
My Code:
QueueInterface.h :
code: |
#include "QueueTypes.h" /* imports the data type definitions of */
/* ItemType and Queue */
/* defined operations */
extern void InitializeQueue(Queue *Q);
/* Initialize the queue Q to be the empty queue */
extern int Empty(Queue *Q);
/* Returns TRUE == 1 if and only if the queue Q is empty */
extern int Full(Queue *Q);
/* Returns TRUE == 1 if and only if the queue Q is full */
extern int Insert(ItemType R, Queue *Q);
/* If Q is not full, insert a new item R onto the rear of Q */
extern int Remove(Queue *Q, ItemType *F);
/* If Q is non-empty, remove the frontmost item of Q and put it in F */
|
QueueTypes.c :
code: |
typedef int ItemType; /* the ItemType can be arbitrary. */
typedef struct QueueNodeTag {
ItemType Item;
struct QueueNodeTag *Link;
}QueueNode;
typedef struct { /* a queue is empty iff */
QueueNode *Front; /* its Front == NULL */
QueueNode *Rear;
}Queue;
|
QueueDriver.c :
code: |
#include <stdio.h>
#include <stdlib.h>
#include "QueueInterface.h"
int main(void)
{
Queue Q;
InitializeQueue(&Q);
if (Empty(&Q) == 1)
{
printf("Queue is empty\n");
}
int number = 1;
while (number <= 7)
{
Insert(number, &Q);
if (Empty(&Q) != 1)
{
printf("Queue is not empty\n");
}
number++;
}
}
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
md
![](http://compsci.ca/v3/uploads/user_avatars/1849317514ed6c4399768d.png)
|
Posted: Sat Oct 15, 2011 4:16 pm Post subject: RE:Re: Print Method for Queue List |
|
|
If you figured it out then post the solution too. There's nothing worse then when someone says "never mind I figured it out" without posting what they did. Makes finding the solution infinitely more painful. |
|
|
|
|
![](images/spacer.gif) |
|
|