Question

CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int heig...

CODE IN C

Objectives:

Queue operations.

Data structure:

typedef struct {

int width;

int height;

}Rect;

typedef struct node {

Rect* r;

struct node* next;

}Node;

typedef struct {

Node* head;

Node* tail;

}Queue;

Specification:

In this lab six Queue-related operation functions need to be implemented by using the given function prototypes and data structures.

1. List* createQueue(void);

This function initializes an empty “Queue” with the “Queue” data structure and returns an empty queue.

2. int enQueue(List*);

This function receives a queue, and inserts one more Node with random width and height values into the current queue. This operation should be done within O(1) time. It returns 1 if the insertion succeeded and -1 if the insertion failed.

3. void* deQueue(List*);

This function receives a queue, and deletes the earliest Node in the queue. This operation should be done within O(1) time. It returns the pointer pointing to the Rect inside the deleted Node if the deletion succeeded and NULL if the deletion failed.

4. int deQueueAll(List*);

This function receives a queue, and deletes all the Nodes in the current queue. It returns 1 if the deletion succeeded and -1 if the deletion failed.

5. void printQueue(List*);

This function receives a queue, and prints all the rectangle areas stored in the current queue.

6. void freeQueue(List*);

This function receives a queue, and frees all the allocated memories in the current queue.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct {
    int width;
    int height;
}Rect;

typedef struct node {
    Rect* r;
    struct node* next;
}Node;

typedef struct {
    Node* head;
    Node* tail;
}Queue;

Queue* createQueue(void);
int enQueue(Queue*);
void* deQueue(Queue*);
int deQueueAll(Queue*);
void printQueue(Queue*);
void freeQueue(Queue*);

int main(void){

    srand(time(NULL));

    Queue* queue = createQueue();
    if (queue == NULL){
        return -1;
    }
    int check = 1;;

    for (int i = 0; i < 10; i++){
        check = enQueue(queue);
        if (check == -1){
            return -1;
        }
    }
    printf("(printQueue)");
    printQueue(queue);

    Rect *r = NULL;
    for (int i = 0; i < 2; i++){
        r = (Rect*)deQueue(queue);
        if (r != NULL){
            printf("(deQueue)");
            printf("Rectangle with area %d has been dequeued\n",r->height*r->width);
        }
    }
    printf("(printQueue)");
    printQueue(queue);

    printf("(deQueueAll)");
    check = deQueueAll(queue);
    printf("(printQueue)");
    printQueue(queue);

    // final test for deQueue
    printf("(deQueue)");
    r = deQueue(queue);

    printf("(freeQueue)\n");
    freeQueue(queue);
    queue = NULL;

    return 0;
}

Queue* createQueue(void){
    Queue *queue = malloc(sizeof(Queue));
    
    Node* node = malloc(sizeof(Node));
    
    //create an empty queue and make it circular linked list
    queue->head=node;
    queue->tail=node;
    queue->head->next = queue->tail;
    queue->tail->next = queue->head;
    
    return queue;
}

int enQueue(Queue* queue){
    if(queue==NULL){
        printf("could not find queue for enqueue");
        return -1;
    }
    Node *newNode = malloc(sizeof(Node));
    if(newNode==NULL){
        printf("could not malloc newNode for enqueue");
        return -1;
    }
    Node *current =queue->head;
    Rect *r = malloc(sizeof(Rect));
    if(r==NULL){
        printf("could not malloc rectangle r for enqueue");
        return -1;
    }
    
    //random values for height and width into r
    r->height = rand()%10+1;
    r->width = rand()%10+1;
    
    newNode->r=r;
    
    
    if(queue->head->next == queue->tail){
        //if head and tail are the only things in queue then put newNode after head and before tail
        newNode->next = queue->tail;
        queue->head->next=newNode;
        return 1;
    }else{
        //if there are more nodes than head and tail go to the last spot before the tail
        while(current->next != queue->tail){
            current=current->next;
        }
        //set the newNode to the spot before tail and set its next value to tail
        newNode->next = queue->tail;
        current->next = newNode;
        current=newNode;
        return 1;
    }
    return -1;
    
}

void* deQueue(Queue* queue){
    //grab the deQueued node and store it in deleted
    Node *deleted = queue->head->next;
    
    //check that there is something to delete
    if(queue->head == NULL){
        return NULL;
    }else{
        //remove the value after the head dummy node and set head->next to the new node
        queue->head->next=queue->head->next->next;    
        return deleted->r;
    }
}

int deQueueAll(Queue* queue){
    Node* current = queue->head;//setting current the empty head node
    
    //as long as current->next isnt the tail go through the queue
    while(current->next != queue->tail){
        
        //remove the value after the dummy head and set it to the next node
        current->next=current->next->next;
    }
    //if everything but the dummy nodes were deleted then return 
    if(current->next == queue->tail){
        return 1;
    }else{
        return -1;
    }
}

void printQueue(Queue* queue){
    //set current to the head node
    Node *current = queue->head;
    
    //as long as current->next isnt the tail print the area of the current node and go to the next node
    while(current->next != queue->tail){
        current=current->next;
        printf("Rectangle with area %d\n",current->r->height*current->r->width);
    }
}

void freeQueue(Queue* queue){
    //make sure the queue isnt already empty
    if(queue==NULL){
        return;
    }else{
        Node *current = queue->head;
        Node *p=queue->head;
        
        //while current->next isnt tail go through the queue and free what the current node is
        for(current=current->next; current->next != queue->tail; current = current->next){
            free(p);
            p=current;
        }
        //if there is only the dummy nodes left free them
        if(queue->head->next==queue->tail ){
            free(queue->tail);//queue->head gets freed in main otherwise double free error
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int heig...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect;...

    CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect; typedef struct node { Rect* r; struct node* next; }Node; typedef struct { Node* head; Node* tail; }Queue; Specification: In this lab six Queue-related operation functions need to be implemented by using the given function prototypes and data structures. 1. List* createQueue(void); This function initializes an empty “Queue” with the “Queue” data structure and returns an empty queue. 2. int enQueue(List*); This function receives...

  • Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype...

    Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype data; node *tail; }; class LinkedList{ private: node *head; node *current;public: //constructors LinkedList(); LinkedList(int i); //destructor ~LinkedList(); bool start(); //sets list postion to header bool nextNode(); //increments to next node in list int getCurrent(); //returns data from current node void insertNode(int i); //inserts node after current node    //then sets current node to new node bool deleteNode();//deletes currentnode void deleteAll(); //deletes all nodes };...

  • Consider the following structure definitions and fill in the blanks for the dequeue function: typedef struct...

    Consider the following structure definitions and fill in the blanks for the dequeue function: typedef struct queue queue_t; typedef struct node node_t; struct node { node_t *next; void *val; }; struct queue { node_t *head; node_t *tail;}; /* removes and returns the item at the head of the queue q, * or NULL if q is empty. */ void *dequeue (queue_t *q) { if (q->head == NULL) { return NULL; } void *val = __________________________; node_t *p = __________________________; q->head...

  • () Given the following structure definition and typedef for a linked list of strings: typedef struct...

    () Given the following structure definition and typedef for a linked list of strings: typedef struct node st node; struct node st { char *word; /* a valid string pointer or NULL */ node *next; /* next node in the list or NULL */ }; Write a C function, free list(), that takes as an argument one of these lists, possibly NULL, and frees all the strings as well as the list itself. Write robust code. void free list(node *list){

  • /* * struct for a single node in a binary tree. data contains the int *...

    /* * struct for a single node in a binary tree. data contains the int * stored in this node. left and right contain pointers to the left and * right subtrees respectively. * * All of the ints stored in the left subtree is smaller than data. * All of the ints stored in the right subtree is larger than data. */ struct node { int data; struct node *left; struct node *right; }; typedef struct node node; Write...

  • Code in C language ADT: typedef struct{ int ID; float salary; int age; }Employee; ...

    code in C language ADT: typedef struct{ int ID; float salary; int age; }Employee; Specification: In this lab, five functions need to be implemented using the given ADT. 1. Employee* readRecord(FILE*) This function receives a FILE pointer created before. It reads a line from the provided csv file, and creates an Employee struct pointer with the information from that line then returns the pointer back to the calling function. Each line in the provided csv file contains the id, salary,...

  • typedef struct queue * Queue; Queue queueCreate(int maxSize); void enqueue(Queue q, int item); int queueFront(Queue q);...

    typedef struct queue * Queue; Queue queueCreate(int maxSize); void enqueue(Queue q, int item); int queueFront(Queue q); int dequeue(Queue q); int queueSize(Queue q); void queueDestroy(Queue q)Exercise 1: Writing Assert Based Black Box Unit Tests for a Queue For this exercise you will need a copy of Queue.h, the queue interface for this exercise. You can download it or use the following command to copy it into your current directory cp «dp1091/public_html/2012/tlb/11/Queue.h. And a copy of the testQueue.c, the testing code we...

  • IN C Programming #include<stdio.h> #include<stdlib.h> typedef struct nodestruct { int item; struct nodestruct *next; } Node;...

    IN C Programming #include<stdio.h> #include<stdlib.h> typedef struct nodestruct { int item; struct nodestruct *next; } Node; typedef struct { int size; // Number of items on user’s list Node *head, *tail; } List; //In addition to creating an empty list, this function will also create two dummy nodes; one for the head, and the other for the tail. List* createList(); //This function creates a node containing the provided item, then inserts it into the list pointed by the provided list...

  • A linked list of integers is built using the following struct: struct node {   int data;...

    A linked list of integers is built using the following struct: struct node {   int data;   struct node *next; }; Define a function named max that returns the maximum integer in a list. The function takes one arguments, a pointer to the head of the list. The function returns an integer, which is the maximum value. If the list is empty, return zero. NOTE: You know nothing about the values in the list. They could all be negative!

  • // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations // ...

    // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations //   - (i.e. queue_t* create_queue(unsigned int _capacity) should not have additional arguments) // - You should not have any 'printf' statements in your queue functions. //   - (You may consider using these printf statements to debug, but they should be removed from your final version) // ==================================================...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT