Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

by java Implement a linked list generic queue. Remember queues are first in firs

ID: 3785791 • Letter: B

Question

by java

Implement a linked list generic queue. Remember queues are first in first out (FIFO). Use the driver to then test each of the methods. Simply download it and place it with the other source files.

Create a class GenLLQueue which has the following:

Internal class ListNode which contains:

Instance variable data of type T

Instance variable link of type ListNode

Default constructor that sets both instance variables to null

Instance Variables

head which is of type ListNode which points to the first element in the queue

tail which of type ListNode which points to the last element in the queue

Constructor

A default constructor that initializes head and tail to null

Methods

enqueue – This method returns no value and takes a variable of type T and adds it after the tail. The moves to tail to point to the newly added element.

dequeue – This method removes and returns the first element in the queue

peek – This method returns the first element of the queue without removing it

showQueue – Prints out the queue in order

Driver:

example output:

Testing Generic Linked List Queue

Enqueue'ing 10 numbers 0-9

Dequeue'ing all numbers and printing them out.

0

1

2

3

4

5

6

7

8

9

Testing peek

5

Testing show queue by adding all even numbers 0 to 8

5

0

2

4

6

8

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

struct node

{

    int info;

    struct node *ptr;

}*front,*rear,*temp,*front1;

int frontelement();

void enq(int data);

void deq();

void empty();

void display();

void create();

void queuesize();

int count = 0;

void main()

{

    int no, ch, e;

    printf(" 1 - Enque");

    printf(" 2 - Deque");

    printf(" 3 - Front element");

    printf(" 4 - Empty");

    printf(" 5 - Exit");

    printf(" 6 - Display");

    printf(" 7 - Queue size");

    create();

    while (1)

    {

        printf(" Enter choice : ");

        scanf("%d", &ch);

        switch (ch)

        {

        case 1:

            printf("Enter data : ");

            scanf("%d", &no);

            enq(no);

            break;

        case 2:

            deq();

            break;

        case 3:

            e = frontelement();

            if (e != 0)

                printf("Front element : %d", e);

            else

                printf(" No front element in Queue as queue is empty");

            break;

        case 4:

            empty();

            break;

        case 5:

            exit(0);

        case 6:

            display();

            break;

        case 7:

            queuesize();

            break;

        default:

            printf("Wrong choice, Please enter correct choice ");

            break;

        }

    }

}

/* Create an empty queue */

void create()

{

    front = rear = NULL;

}

/* Returns queue size */

void queuesize()

{

    printf(" Queue size : %d", count);

}

/* Enqueing the queue */

void enq(int data)

{

    if (rear == NULL)

    {

        rear = (struct node *)malloc(1*sizeof(struct node));

        rear->ptr = NULL;

        rear->info = data;

        front = rear;

    }

    else

    {

        temp=(struct node *)malloc(1*sizeof(struct node));

        rear->ptr = temp;

        temp->info = data;

        temp->ptr = NULL;

        rear = temp;

    }

    count++;

}

/* Displaying the queue elements */

void display()

{

    front1 = front;

    if ((front1 == NULL) && (rear == NULL))

    {

        printf("Queue is empty");

        return;

    }

    while (front1 != rear)

    {

        printf("%d ", front1->info);

        front1 = front1->ptr;

    }

    if (front1 == rear)

        printf("%d", front1->info);

}

/* Dequeing the queue */

void deq()

{

    front1 = front;

    if (front1 == NULL)

    {

        printf(" Error: Trying to display elements from empty queue");

        return;

    }

    else

        if (front1->ptr != NULL)

        {

            front1 = front1->ptr;

            printf(" Dequed value : %d", front->info);

            free(front);

            front = front1;

        }

        else

        {

            printf(" Dequed value : %d", front->info);

            free(front);

            front = NULL;

            rear = NULL;

        }

        count--;

}

/* Returns the front element of queue */

int frontelement()

{

    if ((front != NULL) && (rear != NULL))

        return(front->info);

    else

        return 0;

}

/* Display if queue is empty or not */

void empty()

{

     if ((front == NULL) && (rear == NULL))

        printf(" Queue empty");

    else

       printf("Queue not empty");

}