Please tell me what is this code. is it a singly linked list or a doubly linked
Question:
Please tell me what is this code.
is it a singly linked list or a doubly linked list?
Please change the coding format to a different style. There must be different ways of doing the code.
You may not use a List, ArrayList, Vector, or similar type built-in to the chosen programming language. You must create the linked list and its necessary components.
Please make this code unique. I want this code as a doubly linked list. Please help. Please use comments so I understand what each line of code does!
// Storm.java public class Storm { public String name; public int maxWind; public String monthFormed; public int category; } //end of Storm.java
//HurricaneLinkedList.java
public class HurricaneLinkedList {
// inner node class for linked list class HNode { Storm data; HNode next; HNode prev; } private HNode head; private HNode tail; public HurricaneLinkedList() { head= null; tail = null; }
public void push_front(Storm h) { HNode curr = new HNode(); curr.data = h; curr.prev = null; curr.next = head;
if(head != null) head.prev = curr; else tail = curr; head = curr;
} public void push_back(Storm h) { HNode curr = new HNode(); curr.data = h; curr.prev = tail; curr.next = null;
if(tail != null) tail.next = curr; else head = curr;
tail = curr; } public void insert(Storm h, int i) { HNode node = new HNode(); node.data = h; node.prev = null; node.next = null;
if((i == 0) || (head == null)) push_front(h); else { HNode curr = head; int index = 0;
while((curr.next != null) && (index < i-1)) { index++; curr = curr.next; }
node.prev = curr; node.next = curr.next; if(curr.next != null) curr.next.prev = node; else tail = node; curr.next = node; } }
public void erase(int i) { if(head != null) { if(i < 0) System.out.println("Invalid index"); else { HNode curr = head; int index = 0; while((curr.next != null) && (index < i) ) { index++; curr = curr.next; }
if((index < i) || (curr == null)) System.out.println("Invalid index"); else { if(curr.prev == null) { head = head.next; if(head != null) head.prev = null; }else curr.prev.next = curr.next; if(curr.next == null) { tail = curr.prev; if(tail != null) tail.next = null; }else curr.next.prev = curr.prev; } } }else System.out.println("List is empty"); } public void printForward() { if(head == null) { System.out.println("Empty list"); }else { System.out.println(); HNode node = head;
while(node != null) { if(node.data.category == 0) { System.out.print("Tropical Storm "); }else System.out.print("Hurricane "); System.out.println(node.data.name+" - Wind Speed: "+node.data.maxWind+" MPH; Month Formed : "+node.data.monthFormed+"; Category : "+node.data.category); node = node.next; } } System.out.println(); }
public void printReverse() { if(head == null) { System.out.println("Empty list"); }else { System.out.println(); HNode node = tail;
while(node != null) { if(node.data.category == 0) { System.out.print("Tropical Storm "); }else System.out.print("Hurricane "); System.out.println(node.data.name+" - Wind Speed: "+node.data.maxWind+" MPH; Month Formed : "+node.data.monthFormed+"; Category : "+node.data.category); node = node.prev; } } System.out.println(); } }
//end of HurricaneLinkedList.java
//Driver.java public class Driver {
public static void main(String[] args) {
HurricaneLinkedList list = new HurricaneLinkedList(); // creaate a list // add storms Storm storm = new Storm(); storm.name = "Alex"; storm.maxWind = 120; storm.category = 3; storm.monthFormed = "July";
list.push_front(storm);
storm = new Storm(); storm.name = "Bonnie"; storm.maxWind = 65; storm.category = 0; storm.monthFormed = "August";
list.push_back(storm); storm = new Storm(); storm.name = "Charley"; storm.maxWind = 150; storm.category = 4; storm.monthFormed = "August";
list.push_front(storm); storm = new Storm(); storm.name = "Ivan"; storm.category = 5; storm.maxWind = 165; storm.monthFormed = "September";
list.insert(storm,1); storm = new Storm(); storm.name = "Matthew"; storm.category = 0; storm.maxWind = 45; storm.monthFormed = "October";
list.insert(storm,2); // display the list System.out.println("Storms Forward: "); list.printForward(); System.out.println("Storms Reverse: "); list.printReverse();
// remove the element at index 2 list.erase(2); // display the list System.out.println("Storms Forward: "); list.printForward(); System.out.println("Storms Reverse: "); list.printReverse(); } }
//end of Driver.jav
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill