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.
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)