Hey everyone, I'm new to C but trying to learn but so far it's not going so well
I have this lab:
I have this so far in order in a linked queue: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
common.c:
and the header, common.h: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); }
queue.c: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
and the header queue.h: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; } }
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..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
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![]()



LinkBack URL
About LinkBacks



