ECET 370 ECET370 ECET/370 ENTIRE COURSE HELP – DEVRY UNIVERSITY
$14.99$19.00
ECET 370 ECET370 ECET/370 ENTIRE COURSE HELP – DEVRY UNIVERSITY
ECET 370 Week 2 Lab Simple LinkedList Class
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.
Description
ECET 370 ECET370 ECET/370 ENTIRE COURSE HELP – DEVRY UNIVERSITY
ECET 370 Week 2 Lab Simple LinkedList Class
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | /****************************** * 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 ( "\nList 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; } } |
ECET 370 ECET370 ECET/370 ENTIRE COURSE HELP – DEVRY UNIVERSITY
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…
ECET 370 ECET370 ECET/370 ENTIRE COURSE HELP – DEVRY UNIVERSITY
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.