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

Consider a user interface menu using doubly linked lists as main data structures

ID: 3550434 • Letter: C

Question

Consider a user interface menu using doubly linked lists as main data structures.




Consider a user interface menu using doubly linked lists as main data structures. The menu consists of a set of main menu items, each main menu item having associated a submenu consisting of a set of submenu items (see the example in the attached file). The set of main menu items should be implemented as a doubly linked list of mNodes, each mNode representing a main menu item. Each mNode defines the main menu item name, two links to its mNode neighbors (the previous and next mNode) and a link to its first submenu node. Each submenu should be implemented as a doubly linked list of smNodes. Each smNode contains its name and two links to its smNode neighbors (the up and down smNode). The data structures of a particular user interface menu are shown in the attached file. Your task is to design, write and test a java program Menu. java executing the following tasks: Read a menu from an input file Menu. txt and build the linked lists data structures. Each file line contains a main menu item followed by its submenu items, all items being separated by the semicolon character. Example of input file content: File;New;Open;Save;SaveAs;Close View;Color;Gray;Zoom;Ruler;Grid Process;Select;Move ;Rotate ;Scale ;Delete ;Group About;Release;Version;Contact;Description Traverse the linked lists data structures and display the menu, using the same format as the one in the input file. The main menu items and the submenu items will be traversed in forward direction. Add a new main menu item and its associated submenu to the existing menu. Read the new main menu item data from an input file MenuAdd. txt. The input file contains two lines. The first line specifies the main menu node after which the new main menu node will be inserted. The second line specifies the new main menu node and its submenu nodes. Display the menu after adding the new main menu item. The main menu items will be traversed in backwards direction while the submenu items will be traversed in forward direction. Example of adding a new menu item For example, the file content below specifies that the new main menu item called Layout (with submenu items Margins, Orientation and Size) will be inserted into the existing menu after the main menu item View. Layout;Margins;Orientation;Size Ask the user to enter the name of a main menu item and remove it from the menu. Traverse the linked lists data structures (in the same direction as of question b)) and display the menu after the remove operation was performed.

Explanation / Answer

package menu;

import java.io.*;
import java.lang.*;
import java.util.*;

class Node<E>
{
    E elem;
    Node<E> next, prev,child;
}

class LinkedList<E>
{
    private Node<E> head = null;
    private Node<E> tail = null;
    private Node<E> temp = null;   
    private int counter = 0;
    public LinkedList(){}
    public int size()
    {
        return counter;
    }
    public void print()
    {
        for (Node n = head; n != null; n = n.next)
        {
            System.out.print(n.elem.toString() + " ");
            for(Node y = n.child; y!=null; y = y.next)
            {
                System.out.print(y.elem.toString() + " ");
            }
            System.out.println("");
    }
    System.out.println("");
    }
   
    public Node indexOf(E elem)
    {
        temp = head; //start at the beginning of the list
    int i; //create a counter field that isn't local to a loop
   
    //while we haven't found the elem we are looking for, keep looking
    for(i=0; !(temp.elem).equals(elem) && temp != null; i++)
    temp = temp.next;
    if(i == size()) return null; //if the elem wasn't found, return null
    return temp;   //otherwise, return the node
    }
   
    public Node add(E elem)
    {
        //if we don't have any elems in our LinkedList
    if (head == null)
        {
            head = new Node<E>();
        tail = head;
        head.elem = elem;
            head.next = null;
            head.prev = null;
            counter++ ;
            return head;
    }
        else
        {
            temp = tail;
            tail.next = new Node(); //add a new node to the end of the list
        tail = tail.next; //set the tail pointer to that node
        tail.elem = elem; //set elem to be stored to the end node
            tail.prev = temp;
            tail.next = null;
            counter++ ;
            return tail;
    }
     }
   
    public void addsub(Node x,E elem)
    {
        //if we don't have any elems in our sub LinkedList
        Node c = x.child;
    if (c == null)
        {
            c = new Node<E>();
            c.elem = elem;
            c.prev = null;
            c.next = null;
            x.child = c;
    }
        else
        {
            while(c.next != null)
                c = c.next;
            temp = c;
            c.next = new Node(); //add a new node to the end of the list
            c = c.next;
        c.elem = elem; //set elem to be stored to the end node
            c.prev = temp;
            c.next = null;
    }
     }
   
    public Node remove(String lin)
    {
        int i;
   
        temp = head; //start at the beginning of the list
    Node temp1=null,temp2=null;
    //iterate to the position before the line
        for(i=0; !(temp.next.elem).equals(lin) && temp != null; i++)
    temp = temp.next;
       
        temp1 = temp.next ;
        temp2 = temp1.next;
        temp.next = temp2;
        temp2.prev = temp;
        counter--;
    return temp1;
    }
   
}

public class Menu
{
   
    public static void main(String[] args)
    {
        LinkedList<String> ll = new LinkedList<String>();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String fileName = null;
  
        System.out.println("Enter name of file to build link list");
        try
        {
            fileName = br.readLine();
        }
        catch (IOException ioe)
        {
            System.out.println("IO error trying to read your name!");
            System.exit(1);
        }

        System.out.println("File name is, " + fileName);
       
        String line;
        int len,i;
        Node x;
        try
        {
            BufferedReader rd = new BufferedReader(new FileReader(fileName));
            while((line = rd.readLine()) !=null)
            {
                Scanner scanner = new Scanner(line);
                String[] tmp = line.split( ";" );
                len = tmp.length;
                x = ll.add(tmp[0]);
                for(i=1;i<len;i++)
                {
                    ll.addsub(x, tmp[i]);
                }
            }
        }
        catch(Exception e)
        {
            System.out.println("Error reading file");
        }
       
        System.out.println("Printing Menu ");
        ll.print();
       
        System.out.println("Enter name of file to add item in link list");
        try
        {
            fileName = br.readLine();
        }
        catch (IOException ioe)
        {
            System.out.println("IO error trying to read your name!");
            System.exit(1);
        }

        System.out.println("File name is, " + fileName);
        String s;
        try
        {
            BufferedReader ld = new BufferedReader(new FileReader(fileName));
            int cnt = 0;
            Node r=null,u = null;
            while((s = ld.readLine()) !=null)
            {
                Scanner scanner = new Scanner(s);
                if(cnt == 0)
                {
                    r = ll.indexOf(s);
               
                     if(r != null)
                        u = r.next;
               
                     else
                        System.out.println("Node not found");
                }
                else if(cnt > 0)
                {
                    String[] tm = s.split( ";" );
                    int le = tm.length;
                    Node n = new Node();
                    n.elem = tm[0];
                    n.prev = r;
                    n.next = u;
                    r.next = n;
                    u.prev = n;
                    for(i=1;i<le;i++)
                    {
                        ll.addsub(n, tm[i]);
                    }
                }
                cnt++;
            }
        }
        catch(Exception e)
        {
            System.out.println("Error reading file");
        }
       
        System.out.println("Updated Menu is");
        ll.print();
       
        String lin= "";
        System.out.println("Enter name of Menu Item to be Deleted");
        BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
        try
        {
            lin = b.readLine();
        }
        catch(Exception e)
        {
            System.out.println("Error reading line");
        }
        System.out.println(lin);
       
        System.out.println("Menu after removing " + ll.remove(lin).elem);
        ll.print();
    }
}


Output::

run:
Enter name of file to build link list
Menu.txt
File name is, Menu.txt
Printing Menu

File New Open Save SaveAs Close
View Color Gray Zoom Ruler Grid
Process Select Move Rotate Scale Delete Group
About Release Version Contact Description

Enter name of file to add item in link list
MenuAdd.txt
File name is, MenuAdd.txt
Updated Menu is
File New Open Save SaveAs Close
View Color Gray Zoom Ruler Grid
Layout Margins Orientation Size
Process Select Move Rotate Scale Delete Group
About Release Version Contact Description

Enter name of Menu Item to be Deleted
Process
Process
Menu after removing Process
File New Open Save SaveAs Close
View Color Gray Zoom Ruler Grid
Layout Margins Orientation Size
About Release Version Contact Description

BUILD SUCCESSFUL (total time: 13 seconds)