Add the three classic operations of Queues in the MagazineList.java to make it become a queue. Add testing code in MagazineRack.java to test your code. (You do not need to modify Magazine.java)

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Add the three classic operations of Queues in the MagazineList.java to make it become a queue. Add testing code in MagazineRack.java to test your code. (You do not need to modify Magazine.java)

Magazine.java :

public class Magazine
{
private String title;
//-----------------------------------------------------------------
// Sets up the new magazine with its title.
//-----------------------------------------------------------------
public Magazine(String newTitle)
{
title = newTitle;
}
//-----------------------------------------------------------------
// Returns this magazine as a string.
//-----------------------------------------------------------------
public String toString()
{
return title;
}
}

MagazineList.java : attached the image below

MagazineRack.java:

public class MagazineRack
{
//----------------------------------------------------------------
// Creates a MagazineList object, adds several magazines to the
// list, then prints it.
//----------------------------------------------------------------
public static void main(String[] args)
{
MagazineList rack = new MagazineList();

rack.add(new Magazine("Time"));
rack.add(new Magazine("Woodworking Today"));
rack.add(new Magazine("Communications of the ACM"));
rack.add(new Magazine("House and Garden"));
rack.add(new Magazine("GQ"));

System.out.println(rack);
}
}

```java
public class MagazineList
{
    private MagazineNode list;

    //-----------------------------------------------------------------
    // Sets up an initially empty list of magazines.
    //-----------------------------------------------------------------
    public MagazineList()
    {
        list = null;
    }

    //-----------------------------------------------------------------
    // Creates a new MagazineNode object and adds it to the end of 
    // the linked list.
    //-----------------------------------------------------------------
    public void add(Magazine mag)
    {
        MagazineNode node = new MagazineNode(mag);
        MagazineNode current;

        if (list == null)
            list = node;
        else
        {
            current = list;
            while (current.next != null)
                current = current.next;
            current.next = node;
        }
    }

    public String toString()
    {
        String result = "";

        MagazineNode current = list;

        while (current != null)
        {
            result += current.magazine + "\n";
            current = current.next;
        }

        return result;
    }

    /******************************************************************
    // An inner class that represents a node in the magazine list.
    // The public variables are accessed by the MagazineList class.
    ******************************************************************/
}

class MagazineNode
{
    public Magazine magazine;
    public MagazineNode next;

    //-----------------------------------------------------------------
    // Sets up the node
    //-----------------------------------------------------------------
    public MagazineNode(Magazine mag)
    {
        magazine = mag;
        next = null;
    }
}
```

### Explanation

This Java program consists of two classes: `MagazineList` and `MagazineNode`. The purpose of these classes is to manage a linked list of magazines. 

1. **MagazineList Class**
   - **Attributes:**
     - `private MagazineNode list`: This is the head of the linked list.
   - **Methods:**
     - `public MagazineList()`: Initializes an empty list of magazines by setting `list` to `null`.
     - `public void add(Magazine mag)`: This method adds a new `MagazineNode` containing the magazine to the end of the linked list. If the list is initially empty, it sets the new node as the head of the list. Otherwise, it traverses the list to the last node and adds the new node there.
     - `public String toString()`: Returns a string representation of the magazine list by traversing all nodes and concatenating their magazine information.

2. **MagazineNode Class**
   - **Attributes:**
     - `public Magazine
Transcribed Image Text:```java public class MagazineList { private MagazineNode list; //----------------------------------------------------------------- // Sets up an initially empty list of magazines. //----------------------------------------------------------------- public MagazineList() { list = null; } //----------------------------------------------------------------- // Creates a new MagazineNode object and adds it to the end of // the linked list. //----------------------------------------------------------------- public void add(Magazine mag) { MagazineNode node = new MagazineNode(mag); MagazineNode current; if (list == null) list = node; else { current = list; while (current.next != null) current = current.next; current.next = node; } } public String toString() { String result = ""; MagazineNode current = list; while (current != null) { result += current.magazine + "\n"; current = current.next; } return result; } /****************************************************************** // An inner class that represents a node in the magazine list. // The public variables are accessed by the MagazineList class. ******************************************************************/ } class MagazineNode { public Magazine magazine; public MagazineNode next; //----------------------------------------------------------------- // Sets up the node //----------------------------------------------------------------- public MagazineNode(Magazine mag) { magazine = mag; next = null; } } ``` ### Explanation This Java program consists of two classes: `MagazineList` and `MagazineNode`. The purpose of these classes is to manage a linked list of magazines. 1. **MagazineList Class** - **Attributes:** - `private MagazineNode list`: This is the head of the linked list. - **Methods:** - `public MagazineList()`: Initializes an empty list of magazines by setting `list` to `null`. - `public void add(Magazine mag)`: This method adds a new `MagazineNode` containing the magazine to the end of the linked list. If the list is initially empty, it sets the new node as the head of the list. Otherwise, it traverses the list to the last node and adds the new node there. - `public String toString()`: Returns a string representation of the magazine list by traversing all nodes and concatenating their magazine information. 2. **MagazineNode Class** - **Attributes:** - `public Magazine
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Threads in linked list
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education