Question

In: Computer Science

by java Implement a linked list generic queue. Remember queues are first in first out (FIFO)....

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:

public class QueuesTester {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                System.out.println("Testing Generic Linked List Queue");
                System.out.println("Enqueue'ing 10 numbers 0-9");
                GenLLQueue<Integer> qLLInts = new GenLLQueue();
                for(int i=0;i<10;i++)
                {
                        qLLInts.enqueue(i);
                }
                System.out.println("Dequeue'ing all numbers and printing them out.");
                for(int i=0;i<10;i++)
                {
                        System.out.println(qLLInts.dequeue());
                }
                System.out.println("Testing peek");
                qLLInts.enqueue(5);
                System.out.println(qLLInts.peek());
                System.out.println("Testing show queue");
                for(int i=0;i<10;i+=2)
                {
                        qLLInts.enqueue(i);
                }
                qLLInts.showQueue();
        }

}

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

Solutions

Expert Solution

#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("\n 1 - Enque");

    printf("\n 2 - Deque");

    printf("\n 3 - Front element");

    printf("\n 4 - Empty");

    printf("\n 5 - Exit");

    printf("\n 6 - Display");

    printf("\n 7 - Queue size");

    create();

    while (1)

    {

        printf("\n 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("\n 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("\n 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("\n Error: Trying to display elements from empty queue");

        return;

    }

    else

        if (front1->ptr != NULL)

        {

            front1 = front1->ptr;

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

            free(front);

            front = front1;

        }

        else

        {

            printf("\n 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("\n Queue empty");

    else

       printf("Queue not empty");

}


Related Solutions

write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Linked List-Based Queue Sketches In this section you will sketch several linked list-based queues. Hint: Recall...
Linked List-Based Queue Sketches In this section you will sketch several linked list-based queues. Hint: Recall that a proper sketch of a linked list-based queue is just a series of boxes with elements inside. 2, 3, 4 Sketch the linked list-based queue that results from the following operations: Default constructor (create empty) Add(2) Add(3) Add(4) ? Replace this with your sketch Add and Remove Sketch the linked list-based queue that results from the following operations: Default constructor (create empty) Add(2)...
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked...
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked list. One way means that each node has only one pointer, Q, (not both a rear and a front pointer), so the program must use pointers-to-pointers. Include functions to insert(), remove(), for use in main() where the user is prompted "Enter "i" to insert a new element, "r" to remove an element, "q" to quit:"
One way to implement a queue is to use a circular linked list. In a circular...
One way to implement a queue is to use a circular linked list. In a circular linked list, the last node’s next pointer points at the first node. Assume the list does not contain a header and that we can maintain, at most, one iterator corresponding to a node in the list. For which of the following representations can all basic queue operations be performed in constant worst time? Justify your answers. Maintain an iterator that corresponds to the first...
Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
The BBQ implementation of the Q interface, was a FIFO (First-In, First-Out) queue. Using BBQ as...
The BBQ implementation of the Q interface, was a FIFO (First-In, First-Out) queue. Using BBQ as a guide, write a class called QBB that implements the Q interface as a LIFO (Last-In, First Out) queue. In a FIFO queue elements are removed in the ordered in which they added. In a LIFO queue, elements are removed in the reverse order of arrival. Use a String array as the basis of your implementation. /** * A simplified queue interface to manage...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT