Thread: Lost on Queues - Help GREATLY appreciated.

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    5

    Lost on Queues - Help GREATLY appreciated.

    Hey everyone, I'm new to C but trying to learn but so far it's not going so well

    I have this lab:
    You will need to write the following functions:
    -void printLinkedQueue(Queue *q)- prints the contents of an linked list-based queue on
    the screen
    -QueueNode get(Queue *q, int i) - returns the contents of the queue node at location
    i, NULL if the location doesn't exist
    -QueueNode remove(Queue *q, int i) - returns the queue node at location i and
    removes it from the queue. If the location doesn't exist, the function should return NULL.
    Write a main function that will:
    - Create a queue
    - Append three elements
    - Print the queue contents
    - Get the value at location 2
    - Get the value at location 3
    - Remove the value at location 1
    - Print the queue
    - Append an element
    - Print the queue contents
    - Serve an element
    - Print the queue contents
    I have this so far in order in a linked queue:

    common.c:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #include "common.h"
    
    void Error(char *s)
    {
        fprintf(stderr, "ERROR: %s\n", s);
        exit(1);
    }
    
    void Warning(char *s)
    {
        fprintf(stderr, "WARNING: %s\n", s);
    }
    
    Boolean UserSaysYes(void)
    {
        int  c;
    
        printf(" (y,n)? ");
        do {
            while ((c = getchar()) == '\n')
                ;           /* Ignore new line character.   */
            if (c == 'y' || c == 'Y' || c == 'n' || c == 'N')
                return (c == 'y' || c == 'Y');
            printf("Please respond by typing one of the letters y or n\n");
        } while (1);
    }
    and the header, common.h:
    Code:
    #ifndef COMMON_H__
    #define COMMON_H__
    
    
    typedef enum boolean { FALSE, TRUE } Boolean;
    
    
    /* Error: report program error.
    Pre:   s points to the message to be printed.
    Post:  The function prints the message and terminates the program.
     */
    void Error(char *s);
    
    
    /* Warning: print a warning.
    Pre:   s points to the message to be printed.
    Post:  The function prints the warning but does not terminate the program.
     */
    void Warning(char *s);
    
    
    /* UserSaysYes:  TRUE if the user wants to continue execution.
    Pre:  None.
    Post: Returns TRUE if the user's answer begins with either y or Y,
          FALSE if the user responds with any response beginning with either
          n or .
     */
    Boolean UserSaysYes(void);
    
    #endif
    queue.c:
    Code:
    #include <stdlib.h>
    
    #include "queue.h"
    
    
    /*  function prototypes for node management functions */
    QueueNode *MakeNode(QueueEntry item);
    void DestroyNode(QueueNode *p);
    void AppendNode(QueueNode *p, Queue *q);
    void ServeNode(QueueNode **p, Queue *q);
    
    
    
    /* CreateQueue:  create the queue.
    Pre:  None.
    Post: The queue q has been initialized to be empty.
    */
    void CreateQueue(Queue *q)
    {
        q->front = q->rear = NULL;
        q->count = 0;
    }
    
    
    /* Append:  append an entry to the queue.
    Pre:  The queue q has been created and is not full.
    Post: The entry x has been stored in the queue as its last entry.
    Uses: QueueFull, Error.
    */
    void Append(QueueEntry item, Queue *q)
    {
    	QueueNode *nodepointer = MakeNode(item);
    
    	q->count++;
    
    	AppendNode(nodepointer, q);
    }
    
    
    /* Serve:  remove the first entry in the queue.
    Pre:  The queue q has been created and is not empty.
    Post: The first entry in the queue has been removed and returned as the
          value of x.
    Uses: QueueEmpty, Error.
    */
    void Serve(QueueEntry *item, Queue *q)
    {
        if (QueueEmpty(q))
            Error("Cannot serve from an empty queue.");
        else {
    		QueueNode *nodepointer;
    		ServeNode(&nodepointer, q);
            q->count--;
            *item = nodepointer->info;
            DestroyNode(nodepointer);
        }
    }
    
    
    /* QueueSize: return the number of entries in the queue.
    Pre:  The queue q has been created.
    Post: The function returns the number of entries in the queue q.
    */
    int QueueSize(Queue *q)
    {
        return q->count;
    }
    
    
    /* QueueEmpty: returns non-zero if the queue is empty.
    Pre:  The queue q has been created.
    Post: The function returns non-zero if the queue q is empty,
          zero otherwise.
    */
    Boolean QueueEmpty(Queue *q)
    {
        return q->front == NULL;
    }
    
    
    /* QueueFull: returns non-zero if the queue is full.
    Pre:  The queue q has been created.
    Post: The function returns non-zero if the queue is full,
          zero otherwise.
    */
    Boolean QueueFull(Queue *q)
    {
        return FALSE; /* dynamic queue is never full */
    }
    
    
    /* MakeNode: make a new node and insert item.
    Pre:  None.
    Post: Create a new node and insert item in it.
    */
    QueueNode *MakeNode(QueueEntry item)
    {
        QueueNode *nodepointer = malloc(sizeof(QueueNode));
    
        if (nodepointer == NULL)
            Error("Exhausted memory.");
        else {
            nodepointer->info = item;
            nodepointer->next = NULL;
        }
    
        return nodepointer;
    }
    
    
    /* DestroyNode: deallocate node memory
    Pre:  p is a pointer to a node created using MakeNode
    Post: the node memory was deallocated
    */
    void DestroyNode(QueueNode *p)
    {
        if (p == NULL)
            Error("Trying to destroy nonexistent node.");
        else
            free(p);
    }
    
    
    /* AppendNode:  append an entry to the queue.
    Pre:  The linked queue q has been created and p points to a node
          not already in q.
    Post: The node to which p points has been placed in the queue as its
          last entry.
    Uses: QueueEmpty, Error.
    */
    void AppendNode(QueueNode *p, Queue *q)
    {
        if (!p)
            Error("Attempt to append a nonexistent node to the queue.");
        else if (QueueEmpty(q)) /* Set both front and rear to p.          */
            q->front = q->rear = p;
        else {                  /* Place p after previous rear of the queue.  */
            q->rear->next = p;
            q->rear = p;
        }
    }
    
    
    /* ServeNode:  remove the first entry in the queue.
    Pre:  The linked queue q has been created and is not empty.
    Post: The first node in the queue has been removed and parameter
          p points to this node.
    Uses: QueueEmpty, Error.
    */
    void ServeNode(QueueNode **p, Queue *q)
    {
        if (QueueEmpty(q))
            Error("Attempt to delete a node from an empty queue.");
        else {
            *p = q->front;             /* Return pointer to front node. */
            q->front = q->front->next; /* Advance front of queue to the next node. */
            if (QueueEmpty(q))         /* Is the queue now empty? */
                q->rear = NULL;
        }
    }
    and the header queue.h:
    Code:
    #ifndef QUEUE_H__
    #define QUEUE_H__
    
    #include "common.h"
    
    typedef int QueueEntry;
    
    typedef struct queuenode {
        QueueEntry info;
        struct queuenode *next;
    } QueueNode;
    
    /* a queue implemented using linked nodes */
    typedef struct queue {
        QueueNode *front;
        QueueNode *rear;
        int count;
    } Queue;
    
    
    
    /* CreateQueue:  create the queue.
    Pre:  None.
    Post: The queue q has been initialized to be empty.
    */
    void CreateQueue(Queue *q);
    	
    
    /* Append:  append an entry to the queue.
    Pre:  The queue q has been created and is not full.
    Post: The entry x has been stored in the queue as its last entry.
    Uses: QueueFull, Error.
    */
    void Append(QueueEntry entry, Queue *q);
    
    
    /* Serve:  remove the first entry in the queue.
    Pre:  The queue q has been created and is not empty.
    Post: The first entry in the queue has been removed and returned as the
          value of x.
    Uses: QueueEmpty, Error.
    */
    void Serve(QueueEntry *entry, Queue *q);
    
    
    /* QueueSize: return the number of entries in the queue.
    Pre:  The queue q has been created.
    Post: The function returns the number of entries in the queue q.
    */
    int QueueSize(Queue *q);
    
    
    /* QueueEmpty: returns non-zero if the queue is empty.
    Pre:  The queue q has been created.
    Post: The function returns non-zero if the queue q is empty,
          zero otherwise.
    */
    Boolean QueueEmpty(Queue *q);
    
    
    /* QueueFull: returns non-zero if the queue is full.
    Pre:  The queue q has been created.
    Post: The function returns non-zero if the queue is full,
          zero otherwise.
    */
    Boolean QueueFull(Queue *q);
    
    
    #endif
    All of that code was provided by our teacher...So far all I can do is create a queue and i'm very lost on how to do the rest..

    what I have so far:

    Code:
    #include <stdio.h>
    #include "queue.h"
    
    void printLinkedQueue(Queue *q);
    QueueNode get(Queue *q,  int i);
    QueueNode remove(Queue *q, int i);
    
    int main(int argc, char *argv[])
    {
    	Queue q1;
    	Queue* q = &q1;
    	CreateQueue(q);
    
    	return 0;
    
    }
    
    void printLinkedQueue(Queue *q){

    ...The help my teacher has given me isn';t really helping me and I'm starting to panic..any help would be greatly appreciated.

    Thanks for any of you in advance who have read/helped

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    What do you need help with? Looks like all the hard part is already done. All you need to do is find out which function calls to write to meet the requirements.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Lets start on what you have so far
    Code:
    	Queue q1;
    	Queue* q = &q1;
    	CreateQueue(q);
    You are creating a queue q1. That's fine. I would suggest using more distinct names since you are starting to learn C. Something like mainQueue or simply queue or even myQ. For example
    Code:
    Queue queue;  //instead of Queue q1
    Because you might easily confuse q with q1 etc etc.

    Also, the second line is not really needed. You can do this instead
    Code:
    Queue q1;
    CreateQueue(&q1);
    If you want to use the method you use, which is perfectly fine, you should give better names! This for example
    Code:
    Queue queue;
    Queue* ptrQueue = &queue; //ptr=pointer
    CreateQueue(ptrQueue);
    Is more descriptive.

    So you have now
    Code:
    void printLinkedQueue(Queue *q);
    What you want is to print every QueueNode inside the Queue.
    Let me get you started.
    Code:
    void printLinkedQueue(Queue *q)
    {
        QueueNode* iterator = q->front; //start from the front
        PrintQueueEntry(iterator->info);  //print the info
        iterator = iterator->next;             //go to the next element
        ...    
    }
    I will let you implement PrintQueueEntry (using printf and note that QueueEntry is an int).
    A hint is using a while loop checking if the next is NULL or checking if next is equal to rear. One of the two
    Code:
    while (iterator != NULL)
    {
       ...
    }
    or
    Code:
    while(iterator != q->rear)
    {
       ...
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    'All the working code are written by my teacher and now I don't know how to call them to do this and that' is the same as 'I've to do this and that, Can you help me write?'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck......help greatly appreciated.
    By matt.s in forum C Programming
    Replies: 5
    Last Post: 10-14-2009, 06:10 PM
  2. Noobie question, all help greatly appreciated!
    By axehero in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2007, 09:47 PM
  3. Replies: 2
    Last Post: 05-10-2006, 10:43 PM
  4. Any assistance would be greatly appreciated
    By iiwhitexb0iii in forum C Programming
    Replies: 18
    Last Post: 02-26-2006, 12:06 PM
  5. Your help would be greatly appreciated
    By Sway2 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2002, 07:55 AM