Implement a Queue using either a LinkedList or an ArrayList as the underlying st
ID: 3775453 • Letter: I
Question
Implement a Queue using either a LinkedList or an ArrayList as the underlying structure. You only need to write methods for enqueue, dequeue, hasItems, and likely a size method. You will also need a constructor which will be responsible for creating the list. Add a Student class (you can use one of the existing Student classes you have written), which has a String name, static int id, String courseName and an int waitTime. When a student is created, assign it a random wait time (between 1 and 10 seconds) and increment the static counter. Next, you will implement the classic producer/consumer problem. Create a class which will act as a Producer for Student objects. The Producer class will extend the Thread class. It will share a Queue with the Consumer class, and it will add Students to the Queue at a random time interval (between 1 and 5 seconds). This class will run as a Thread like this: Thread producerThread = new Thread(new Producer(sharedQueue), "Producer"); Then, implement a Consumer class which will simply pull the next Student out of the Queue and sleep for the dedicated waitTime. This will simulate the waiting time while that Student is being served at the registrar window. You can use Thread.sleep(n) for pausing a Thread. n is in milliseconds, so remember to multiply by 1000. If there are no Students in the Queue, you will want to pause your Thread for a few seconds and wait for the Producer to add more. The entirety of your main method should look something like this: Queue sharedQueue = new Queue< Student >(); Thread prodThread = new Thread(new Producer(sharedQueue), "Producer"); Thread consThread = new Thread(new Consumer(sharedQueue), "Consumer"); prodThread.start(); consThread.start();
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Function to get the middle of the linked list*/
void printMiddle(struct node *head)
{
struct node *slow_ptr = head;
struct node *fast_ptr = head;
if (head!=NULL)
{
while (fast_ptr != NULL && fast_ptr->next != NULL)
{
fast_ptr = fast_ptr->next->next;
slow_ptr = slow_ptr->next;
}
printf("The middle element is [%d] ", slow_ptr->data);
}
}
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
// A utility function to print a given linked list
void printList(struct node *ptr)
{
while (ptr != NULL)
{
printf("%d->", ptr->data);
ptr = ptr->next;
}
printf("NULL ");
}
/* Drier program to test above function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
int i;
for (i=5; i>0; i--)
{
push(&head, i);
printList(head);
printMiddle(head);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.