Placeholder

ECET 370 Week 2 Lab Simple LinkedList Class

$19.00

Description

Exercise 1: Review of Linked Lists
Create a project using the classes in “A Simple LinkedList Class.
(Links to an external site.)
Links to an external site.
” Compile it, run it, and review the code that is given carefully. This code tests the LinkedList class provided in the lecture.
/****************************** * Week 2 lab – exercise 1: * * a simple LinkedList class * *******************************/ /** * Class implementing a linked list. */ public class LinkedList { private Node first; //dummy header node /** * Initializes the list to empty creating a dummy header node. */ public LinkedList() { first = new Node(); } /** * Determines whether the list is empty * * @return true if the list is empty, false otherwise */ public boolean isEmpty() { return (first.getNext() == null); } /** * Prints the list elements. */ public void display() { Node current = first.getNext(); while (current != null) { System.out.print(current.getInfo() + ” “); current = current.getNext(); } System.out.println(); } /** * Adds the element x to the beginning of the list. * * @param x element to be added to the list */ public void add(int x) { Node p = new Node(); p.setInfo(x); p.setNext(first.getNext()); first.setNext(p); } /** * Deletes an item from the list. Only the first occurrence of the item in * the list will be removed. * * @param x element to be removed. */ public void remove(int x) { Node old = first.getNext(), p = first; //Finding the reference to the node before the one to be deleted boolean found = false; while (old != null && !found) { if (old.getInfo() == x) { found = true; } else { p = old; old = p.getNext(); } } //if x is in the list, remove it. if (found) { p.setNext(old.getNext()); } } } /****************************** * Week 2 lab – exercise 1: * * a simple LinkedList class * *******************************/ public class Main { public static void main(String args[]) { LinkedList intList = new LinkedList(); System.out.print(“List of numbers before list creation: “); for (int i =0; i < 10; i++) { int info = (int)(Math.random()*10); System.out.print(info + " "); intList.add(info); } System.out.print(" List of numbers after list creation: "); intList.display(); } } /****************************** * Week 2 lab - exercise 1: * * a simple LinkedList class * *******************************/ /** * Linked list node. */ public class Node { private int info; //element stored in this node private Node next; //link to next node /** * Initializes this node setting info to 0 and next to null */ public Node() { info = 0; next = null; } /** * Sets the value for this node * * @param i the desired value for this node */ public void setInfo(int i) { info = i; } /** * Sets the link to the next node * * @param l node reference */ public void setNext(Node l) { next = l; } /** * Returns the value in this node * * @return the value in this node */ public int getInfo() { return info; } /** * Returns the link to the next node * * @return link to the next node */ public Node getNext() { return next; } } Exercise 2: Implementing a Doubly Linked List Modify the class LinkedList in Exercise 1 to make it a doubly linked list. Name your class DoublyLinkedList. Each node in your list must contain two references, one to the next node and the other to previous node. In addition to the methods used in Exercise 1, add a method addEnd to add an integer at the end of the list, and a method displayInReverse to print the list backwards: void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main class to test your DoublyLinkedList class. A sample console output that demonstrates double linkage is shown below. See that regardless of which end the list was added onto, the contents of the list print out correctly when going either forward or backward thru the list… Exercise 3: Implementing a Bag Class Create a class bag that uses a linked list to store the bag items. The item type must be a Java int type, that is, the bag will store integers. The class should have the methods listed below. Create a main class to test your bag class. This main class should fill a bag with ten random integers, each ranging in value between 0 and 9. LinkedListBag(): default constructor boolean isEmpty(): determines whether the bag is empty void display(): prints the bag elements int getLength(): returns the number of items in the bag void clear(): removes all of the items from the bag void add(int item): adds an item to the bag void removeOne(int item): removes an item from the bag; only the first occurrence of the item should be removed. int count(int item): counts the number of occurrences of an item in the bag. SCREENSHOTS SOLUTION PAYMENT The solution includes a Eclipse project. Attachments [Move over files to preview content of those files] ECET_370_Week_2_Lab.zip (26.18 KB) Eclipse Project ECET370Week2Lab .classpath .project .settings org.eclipse.jdt.core.prefs bin exercise1 LinkedList.class Main.class Node.class exercise2 DoubleLinkedList.class Main.class Node.class exercise3 LinkedListBag.class Main.class Node.class src exercise1 LinkedList.java Main.java Node.java exercise2 DoubleLinkedList.java Main.java Node.java exercise3 LinkedListBag.java Main.java Node.java Source Code exercise1 LinkedList.java Main.java Node.java exercise2 DoubleLinkedList.java Main.java Node.java exercise3 LinkedListBag.java Main.java Node.java Preview LinkedList.java xxxxxx xxxxxxx xxxxxxx() { xxxxxx (xxxxx.xxxxxxx() == xxxx); } xxxxxx xxxx xxxxxxx() { Node current = first.getNext();while (current != null) { System.out.print(current.getInfo() + " "); current = current.getNext(); }System.out.println(); }public void add(int x) { Node p = new Node(); x.xxxxxxx(x); x.xxxxxxx(xxxxx.xxxxxxx()); xxxxx.xxxxxxx(x); } Preview Main.java xxxxxxx xxxxxxxx1; xxxxxx xxxxx xxxx { xxxxxx xxxxxx xxxx xxxx(xxxxxx xxxx[]) { LinkedList intList = new LinkedList();System.out.print("List of numbers before list creation: "); for (int i = 0; i < 10; i++) { int info = (int) (Math.random() * 10); System.out.print(info + " "); xxxxxxx.xxx(xxxx); } xxxxxx.xxx.xxxxx("xxxxx xx xxxxxxx xxxxx xxxx xxxxxxxx: "); xxxxxxx.xxxxxxx(); Preview Node.java xxxxxxx xxxxxxxx1; xxxxxx xxxxx xxxx { xxxxxxx xxx xxxx; //xxxxxxx xxxxxx xx xxxx xxxx private Node next; //link to next nodepublic Node() { info = 0; next = null; } xxxxxx xxxx xxxxxxx(xxx x) { xxxx = x; } xxxxxx xxxx xxxxxxx(xxxx x) { xxxx = x; Preview DoubleLinkedList.java xxxxxx xxxxxxx xxxxxxx() { xxxxxx (xxxxx.xxxxxxx() == xxxx); } xxxxxx xxxx xxxxxxxxxxxxxxxx() { Node current = first.getNext();while (current.getNext() != null) { current = current.getNext(); } while (current.getPrev() != null) { System.out.print(current.getInfo() + " "); current = current.getPrev(); } System.out.println(); } xxxxxx xxxx xxxxxx(xxx x) { xxxx x = xxx xxxx(); x.xxxxxxx(x); x.xxxxxxx(xxxx); xxxx xxxxxxx = xxxxx; xxxxx (xxxxxxx.xxxxxxx() != xxxx) { Preview Main.java xxxxxxx xxxxxxxx2; xxxxxx xxxxx xxxx { xxxxxx xxxxxx xxxx xxxx(xxxxxx xxxx[]) { DoubleLinkedList intList = new DoubleLinkedList();System.out.print("List of numbers before list creation: "); for (int i = 0; i < 10; i++) { int info = (int) (Math.random() * 10); System.out.print(info + " "); xxxxxxx.xxxxxx(xxxx); } xxxxxx.xxx.xxxxx("xxxxx xx xxxxxxx xx xxxxxxx: "); xxxxxxx.xxxxxxxxxxxxxxxx(); Preview Node.java xxxxxxx xxxxxxxx2; xxxxxx xxxxx xxxx { xxxxxxx xxx xxxx; //xxxxxxx xxxxxx xx xxxx xxxx private Node next; //link to next node private Node prev; //link to previous nodepublic Node() { info = 0; next = null; prev = null; } xxxxxx xxxx xxxxxxx(xxx x) { xxxx = x; } Preview LinkedListBag.java xxxxxx xxxxxxx xxxxxxx() { xxxxxx (xxxxx.xxxxxxx() == xxxx); } xxxxxx xxxx xxxxxxx() { Node current = first.getNext();while (current != null) { System.out.print(current.getInfo() + " "); current = current.getNext(); }System.out.println(); }public void add(int x) { Node p = new Node(); x.xxxxxxx(x); x.xxxxxxx(xxxxx.xxxxxxx()); xxxxx.xxxxxxx(x); } Preview Main.java xxxxxxx xxxxxxxx3; xxxxxx xxxxx xxxx { xxxxxx xxxxxx xxxx xxxx(xxxxxx xxxx[]) { LinkedListBag intList = new LinkedListBag(); System.out.print("List of numbers before list creation: "); for (int i = 0; i < 10; i++) { int info = (int) (Math.random() * 10); System.out.print(info + " ");intList.add(info); } xxxxxx.xxx.xxxxx("xxxxx xx xxxxxxx xxxxx xxxx xxxxxxxx: "); xxxxxxx.xxxxxxx(); xxxxxx.xxx.xxxxxxx("xxxxxx xx xxxxxxxx: " + xxxxxxx.xxxxxxxxx()); xxxxxx.xxx.xxxxxxx("xxxxxx xx xxxxxxxxxxx xx 1 xx xxx xxx: " + xxxxxxx.xxxxx(1)); xxxxxx.xxx.xxxxxxx("xxxxxx 1 xxxx xxxxxxxxxxxxx"); Preview Node.java xxxxxxx xxxxxxxx3; xxxxxx xxxxx xxxx { xxxxxxx xxx xxxx; //xxxxxxx xxxxxx xx xxxx xxxx private Node next; //link to next nodepublic Node() { info = 0; next = null; } xxxxxx xxxx xxxxxxx(xxx x) { xxxx = x; } xxxxxx xxxx xxxxxxx(xxxx x) { xxxx = x; Preview LinkedList.java xxxxxx xxxxxxx xxxxxxx() { xxxxxx (xxxxx.xxxxxxx() == xxxx); } xxxxxx xxxx xxxxxxx() { Node current = first.getNext();while (current != null) { System.out.print(current.getInfo() + " "); current = current.getNext(); }System.out.println(); }public void add(int x) { Node p = new Node(); x.xxxxxxx(x); x.xxxxxxx(xxxxx.xxxxxxx()); xxxxx.xxxxxxx(x); } Preview Main.java xxxxxxx xxxxxxxx1; xxxxxx xxxxx xxxx { xxxxxx xxxxxx xxxx xxxx(xxxxxx xxxx[]) { LinkedList intList = new LinkedList();System.out.print("List of numbers before list creation: "); for (int i = 0; i < 10; i++) { int info = (int) (Math.random() * 10); System.out.print(info + " "); xxxxxxx.xxx(xxxx); } xxxxxx.xxx.xxxxx("xxxxx xx xxxxxxx xxxxx xxxx xxxxxxxx: "); xxxxxxx.xxxxxxx(); Preview Node.java xxxxxxx xxxxxxxx1; xxxxxx xxxxx xxxx { xxxxxxx xxx xxxx; //xxxxxxx xxxxxx xx xxxx xxxx private Node next; //link to next nodepublic Node() { info = 0; next = null; } xxxxxx xxxx xxxxxxx(xxx x) { xxxx = x; } xxxxxx xxxx xxxxxxx(xxxx x) { xxxx = x; Preview DoubleLinkedList.java xxxxxx xxxxxxx xxxxxxx() { xxxxxx (xxxxx.xxxxxxx() == xxxx); } xxxxxx xxxx xxxxxxxxxxxxxxxx() { Node current = first.getNext();while (current.getNext() != null) { current = current.getNext(); } while (current.getPrev() != null) { System.out.print(current.getInfo() + " "); current = current.getPrev(); } System.out.println(); } xxxxxx xxxx xxxxxx(xxx x) { xxxx x = xxx xxxx(); x.xxxxxxx(x); x.xxxxxxx(xxxx); xxxx xxxxxxx = xxxxx; xxxxx (xxxxxxx.xxxxxxx() != xxxx) { Preview Main.java xxxxxxx xxxxxxxx2; xxxxxx xxxxx xxxx { xxxxxx xxxxxx xxxx xxxx(xxxxxx xxxx[]) { DoubleLinkedList intList = new DoubleLinkedList();System.out.print("List of numbers before list creation: "); for (int i = 0; i < 10; i++) { int info = (int) (Math.random() * 10); System.out.print(info + " "); xxxxxxx.xxxxxx(xxxx); } xxxxxx.xxx.xxxxx("xxxxx xx xxxxxxx xx xxxxxxx: "); xxxxxxx.xxxxxxxxxxxxxxxx(); Preview Node.java xxxxxxx xxxxxxxx2; xxxxxx xxxxx xxxx { xxxxxxx xxx xxxx; //xxxxxxx xxxxxx xx xxxx xxxx private Node next; //link to next node private Node prev; //link to previous nodepublic Node() { info = 0; next = null; prev = null; } xxxxxx xxxx xxxxxxx(xxx x) { xxxx = x; } Preview LinkedListBag.java xxxxxx xxxxxxx xxxxxxx() { xxxxxx (xxxxx.xxxxxxx() == xxxx); } xxxxxx xxxx xxxxxxx() { Node current = first.getNext();while (current != null) { System.out.print(current.getInfo() + " "); current = current.getNext(); }System.out.println(); }public void add(int x) { Node p = new Node(); x.xxxxxxx(x); x.xxxxxxx(xxxxx.xxxxxxx()); xxxxx.xxxxxxx(x); } Preview Main.java xxxxxxx xxxxxxxx3; xxxxxx xxxxx xxxx { xxxxxx xxxxxx xxxx xxxx(xxxxxx xxxx[]) { LinkedListBag intList = new LinkedListBag(); System.out.print("List of numbers before list creation: "); for (int i = 0; i < 10; i++) { int info = (int) (Math.random() * 10); System.out.print(info + " ");intList.add(info); } xxxxxx.xxx.xxxxx("xxxxx xx xxxxxxx xxxxx xxxx xxxxxxxx: "); xxxxxxx.xxxxxxx(); xxxxxx.xxx.xxxxxxx("xxxxxx xx xxxxxxxx: " + xxxxxxx.xxxxxxxxx()); xxxxxx.xxx.xxxxxxx("xxxxxx xx xxxxxxxxxxx xx 1 xx xxx xxx: " + xxxxxxx.xxxxx(1)); xxxxxx.xxx.xxxxxxx("xxxxxx 1 xxxx xxxxxxxxxxxxx"); Preview Node.java xxxxxxx xxxxxxxx3; xxxxxx xxxxx xxxx { xxxxxxx xxx xxxx; //xxxxxxx xxxxxx xx xxxx xxxx private Node next; //link to next nodepublic Node() { info = 0; next = null; } xxxxxx xxxx xxxxxxx(xxx x) { xxxx = x; } xxxxxx xxxx xxxxxxx(xxxx x) { xxxx = x; Price: $19 Buy Now Checkout Added to cart Add to Cart Checkout Added to cart

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.