Java LinkedList on displaying Data - java

I have created an inventory management app where the user will be able to add, view and delete data from the inventory. When the user type A, the program will prompt user to enter data. If the user type D, the program will display the data in the inventory. I use LinkedList to achieve this. Im quite new to this. I was able to prompt user to enter data but was not able to display it. Theres a red line on the code. Where did I went wrong? I am sorry if the way I ask is wrong. Do correct me.
Main.java
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = 0;
LinkedList myList = new LinkedList();
while(t != 1)
{
System.out.print("I, A, D");
char input = sc.next().charAt(0);
if(input == 'A')
{
System.out.print("Please enter item id: ");
int id = sc.nextInt();
sc.nextLine();
System.out.print("Please enter item name: ");
String name = sc.nextLine();
System.out.print("Please enter item type: ");
String type = sc.nextLine();
System.out.print("Please enter item price: ");
double price = sc.nextDouble();
sc.nextLine();
Item I1 = new Item(id, name, type, price);
myList.addItemToFront(I1);
}
else if(input == 'D')
{
myList.DisplayItem(); //at this point, theres red line which i dont know why?
}
}
LinkedList.java
class LinkedList {
private Node head; // first node in the linked list
private int count;
public int getCount() {
return count;
}
public Node getHead() {
return head;
}
public LinkedList() {
head = null; // creates an empty linked list
count = 0;
}
public void addFront(int n) {
Node newNode = new Node(n);
newNode.setLink(head);
head = newNode;
count++;
}
public void deleteFront() {
if (count > 0) {
head = head.getLink();
count--;
}
}
public void addItemToFront(Item I1)
{
Node itemNode = new Node(I1);
itemNode.setLink(head);
head = itemNode;
}
public void DisplayItem(Node head)
{
if(head == null)
{
return;
}
Node current = head;
while (current != null)
{
System.out.println(current.data.toString());
current = current.getLink();
}
System.out.println(current);
}
public int length(Node head)
{
if(head == null)
{
return 0;
}
int count = 0;
Node current = head;
while(current != null)
{
count++;
current = current.getLink();
}
return count;
}
Node.java
public class Node {
Object data;
private Node link;
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getLink() {
return link;
}
public void setLink(Node link) {
this.link = link;
}
public Node(Object data) {
this.data = data;
this.link = null;
}
}
Sample output (How the program should run example)
I, A, D
A
Please enter item id: 001
Please enter item name: Wooden Chair
Please enter item type: Furniture
Please enter item price: 50.30
I, A, D
D
001, Wooden Chair, Furniture, 50.30

public void DisplayItem() {
if (head == null) {
return;
}
Node current = head;
while (current != null) {
System.out.println(current.getNodeItem().toString());
current = current.getLink();
}
}
Use this method

As I see your delete method in LinkedList.java class is expecting the Node head to be passed to it before it can iterate. You need to pass the head node to your display method then. I would expect it to be a Compiler error in your Main.java if you have put the right code above.

The display method in your linked list class accepts an argument of type node which is the head. you need to pass that argument from the main method, then it will work.
Instead of : myList.DisplayItem();
Do this : myList.DisplayItem(head);
Now, if we llok at your LinkedList class, you have made the 'head' as a private variable, either make it public, so that you can access it from the Main class or you can remove the argument from the DisplayItem(Node head) method because any ways you are using the head as a global variable and then iterating over it in the DisplayItem method.
So, better remove the argument from the method definition, it will work.

Related

Change all occurrences of an Int in a linked list using IntNode Java

For my college work I need to make an IntList that includes the function
public void replace(int oldValue, int newValue);
The function itself needs to be able to replace all occurrences of an integer in the linked list. The list has is a singly linked list therefore has no previous function. Here is the rest of my code for understanding, any help is greatly appreciated.
import java.util.Scanner;
public class IntListTest {
private static Scanner scan;
private static final IntList list = new IntList();
//----------------------------------------------------------------
// Creates a list, then repeatedly prints the menu and does what
// the user asks until they quit.
//----------------------------------------------------------------
public static void main(String[] args)
{
scan = new Scanner(System.in);
printMenu();
int choice = scan.nextInt();
while (choice != 0)
{
dispatch(choice);
printMenu();
choice = scan.nextInt();
}
}
//----------------------------------------
// Does what the menu item calls for.
//----------------------------------------
public static void dispatch(int choice)
{
int newVal;
String info;
switch (choice) {
case 0 -> System.out.println("Bye!");
case 1 -> { //add to front
System.out.println("Enter integer to add to front");
newVal = scan.nextInt();
list.addToFront(newVal);
}
case 2 -> { //add to end
System.out.println("Enter integer to add to end");
newVal = scan.nextInt();
list.addToEnd(newVal);
}
case 3 -> {//print
list.print();
}
case 4 -> {
int oldValue, newValue;
System.out.println("Enter the old value: ");
oldValue = scan.nextInt();
System.out.println("Enter the new value: ");
newValue = scan.nextInt();
list.replace(oldValue, newValue);
}
default -> System.out.println("Sorry, invalid choice");
}
}
//-----------------------------------------
// Prints the user's choices
//-----------------------------------------
public static void printMenu()
{
System.out.println("\n Menu ");
System.out.println(" ====");
System.out.println("0: Quit");
System.out.println("1: Add an integer to the front of the list");
System.out.println("2: Add an integer to the end of the list");
System.out.println("3: Print the list");
System.out.println("4: Replace all occurrences of a value in the list with a new one.");
System.out.print("\nEnter your choice: ");
}
}
public class IntList {
private IntNode front;
public IntList(){
front = null;
}
public void addToFront(int val)
{
front = new IntNode(val,front);
}
//-----------------------------------------
// Adds given integer to end of list.
//-----------------------------------------
public void addToEnd(int val)
{
IntNode new_node = new IntNode(val,null);
if (front == null) {
front = new_node;
}
else {
IntNode temp = front;
while (temp.next != null)
temp = temp.next;
temp.next = new_node;
}
}
//------------------------------------------------
// Prints the list elements from first to last.
//------------------------------------------------
public void print()
{
System.out.println("--------------------");
System.out.print("List elements: ");
IntNode temp = front;
while (temp != null)
{
System.out.print(temp.val + " ");
temp = temp.next;
}
System.out.println("\n-----------------------\n");
}
//-----------------------------------------
// Replaces an exact element with a different one
//-----------------------------------------
public void replace(int oldValue, int newValue){
}
}
public class IntNode {
public int val; //value stored in node
public IntNode next; //link to next node in list
//------------------------------------------------------------------
// Constructor; sets up the node given a value and IntNode reference
//------------------------------------------------------------------
public IntNode(int val, IntNode next)
{
this.val = val;
this.next = next;
}
}
If you're asking for an implementation of the replace method, then it should look like this:
public void replace(int oldValue, int newValue) {
IntNode temp = front;
while (temp != null) {
temp.val = temp.val == oldValue ? newValue : temp.val;
temp = temp.next;
}
}
It's not that different from what you've already written in your print method. You basically use an auxiliary node to iterate your list and instead of printing its value you check whether the value must be replaced with newValue or not. In case the replacement shouldn't be happening then its current value is simple re-assigned.

LinkedList with if statements?

So i got this one task to do, about LinkedList, you can take a look on my Main file, also got to mention that my "//The conditions" part is wrong and I just put something as an idea, but that's actually not really working
import java.util.*;
public class Main {
public static void main(String[] args){
ArrayList nokiaAL = new ArrayList();
LinkedList phoneAL = new LinkedList();
//input
Smartphone a = new Smartphone("Nokia","Nokia 7 Plus",1300,260101);
Smartphone b = new Smartphone("Samsung","Galaxy S8",900,220100);
Smartphone c = new Smartphone("Xiaomi","Mi 10",1500,150031);
Smartphone d = new Smartphone("Nokia","3310",250,101001);
Smartphone e = new Smartphone("Samsung","Galaxy Y",400,774101);
Smartphone f = new Smartphone("Apple","iPhone 7",1100,316300);
phoneAL.insertAtFront(f);
phoneAL.insertAtFront(e);
phoneAL.insertAtFront(d);
phoneAL.insertAtFront(c);
phoneAL.insertAtFront(b);
phoneAL.insertAtFront(a);
//process
Object r = (Object) phoneAL.getFirst();
while (r != null) {
System.out.print(" "+r);
r = (Object) phoneAL.getNext();
}
//The conditions
//If nokia + the price $1200+, it will save all the info about nokia
//If brand samsung + model Galaxy Y, It will count the total of the phone
Object obj;
int countSamsung = 0;
for(int i=0;i<phoneAL.size();i++){
obj = phoneAL.get(i);
Smartphone obj2 = (Smartphone) obj;
if(obj2.getBrand().equalsIgnoreCase("Nokia")){
nokiaAL.add(obj2);
}
if(obj2.getBrand().equalsIgnoreCase("Samsung")){
if(obj2.getModel().equalsIgnoreCase("Galaxy Y")){
countSamsung++;
}
}
}
//output
System.out.println("\n");
System.out.println("Details about Nokia phone more than RM1200:"+nokiaAL.toString());
System.out.println("Quantity of Samsung model Galaxy Y: " + countSamsung);
}
}
I know how to print all the details in the LinkedList, the main point here is, you can't add or change anything of other .java files, you can only edit the Main.java file, is it even possible? here's my Smartphone and LinkedList code.
public class Smartphone {
String brand;//e.g: Nokia, Samsung
String model;//e.g: Lumia, Galaxy Y, Note S
double price;
int warranty;//warranty (in year)
Smartphone() {
}
public Smartphone(String a, String b, double c, int d){
this.brand=a;
this.model=b;
this.price=c;
this.warranty=d;
}
public String getBrand(){
return brand;
}
public String getModel(){
return model;
}
public double getPrice(){
return price;
}
public int getWarranty(){
return warranty;
}
public String toString(){
return "\n\nBrand: "+brand +"\nModel: "+ model +"\nPrice: $"+ price +"\nWarranty: "+ warranty;
}
}
public class LinkedList
{
private Node first;
private Node last;
private Node current;
public LinkedList()
{
first = null;
last = null;
current = null;
}
public boolean isEmpty(){
return (first == null); }
public void insertAtFront(Object insertItem){
Node newNode = new Node(insertItem);
if (isEmpty()){
first = newNode;
last = newNode;
}else{
newNode.next = first;
first = newNode;
}
}
public void insertAtBack(Object insertItem){
Node newNode = new Node(insertItem);
if(isEmpty()){
first = newNode;
last = newNode;
}else{
last.next = newNode;
last = newNode;
}
}
public Object removeFromFront(){
Object removeItem = null;
if(isEmpty()){
return removeItem;
}
removeItem = first.data;
if(first == last){
first = null;
last = null;
}else
first = first.next;
return removeItem;
}
public Object removeFromBack(){
Object removeItem = null;
if(isEmpty())
{
return removeItem;
}
removeItem = last.data;
if (first == last)
{
first = null;
last = null;
}else{
current = first;
while(current.next != last)
current = current.next;
last = current;
last.next = null;
}
return removeItem;
}
public Object getFirst(){
if(isEmpty())
return null;
else
{
current = first;
return current.data;
}
}
public Object getNext(){
if(current == last)
return null;
else
{
current = current.next;
return current.data;
}
}
}
As I said before, I can print all the details of the phones, but how to really use it as conditions, like If-else statement? for example, if(obj.getBrand().equalsIgnoreCase("Nokia")){} , I can achieve this with ArrayList but since this is LinkedList task, So I'm still figuring this out without even know if its possible or not. I hope someone would understand this and able to help. TQ
here's my node code for the LinkedList
public class Node {
Object data;
Node next;
Node(Object obj){
data=obj;
}
}
You should iterate using while and validating if the list has ended.
Diferently from an ArrayList, that you can directly acess the vector positions, in a linked list you should walk from node to node. Also, in your example you only implement a getNext() method and not a get(i).
Example:
Object aux = linkedList.getFirst();
while(aux != null) {
// your business logic here
aux = linkedList.getNext();
}
As you dont make the use of generics in your implementation, to acess your object data, you will need to use cast or make use of generics in your implementation.
Cast way:
while(aux != null) {
phoneObject = (Smartphone) aux;
// your business logic here
if(phoneObject.getBrand().equalsIgnoreCase("Nokia")){
System.out.println("Phone brand == Nokia");
}
aux = linkedList.getNext();
}
In the generic approach, you will also need to change the LinkedList implementation and Node implementation.
LinkedList:
public class LinkedList<T>
{
private Node<T> first;
private Node<T> last;
private Node<T> current;
public T getFirst(){
if(isEmpty())
return null;
else
{
current = first;
return current.data;
}
}
public T getNext(){
if(current == last)
return null;
else
{
current = current.next;
return current.data;
}
}
// add other methods here
}
Node:
public class Node<T> {
T data;
Node<T> next;
// add other methods here
}
Main:
LinkedList<Smartphone> linkedList = new LinkedList<Smartphone>();
// add objects
Smartphone aux = linkedList.getFirst();
while(aux != null) {
// no need to cast, because of generics use
if(aux.getBrand().equalsIgnoreCase("Nokia")){
System.out.println("Phone brand == Nokia");
}
// your business logic here
aux = linkedList.getNext();
}
Your getNext() method, returns null if your list has ended, so our stop criteria is aux == null. Our loop will execute while aux is not null, execute all your business logic (if clauses or what ever validation you want to do) and in the end of the loop, you will set the next object to aux variable.
You should add a generic parameter to your LinkedList.
class LinkedList<T> {
private Node<T> first;
private Node<T> last;
private Node<T> current;
....
}
class Node<T> {
T data;
Node<T> next;
Node(T obj) {
data = obj;
}
}
Then you can only add objects of that type to your list.
LinkedList<Smartphone> phoneList = new LinkedList<>();
But of course, if you can you really should not implement LinkedList by yourself but use the existing one! That's far more easier and safer to use.
List<Smartphone> nokiaList = new ArrayList<>();
List<Smartphone> phoneList = new LinkedList<>();
//input
phoneList.add(new Smartphone("Nokia", "Nokia 7 Plus", 1300, 260101));
phoneList.add(new Smartphone("Samsung", "Galaxy S8", 900, 220100));
phoneList.add(new Smartphone("Xiaomi", "Mi 10", 1500, 150031));
phoneList.add(new Smartphone("Nokia", "3310", 250, 101001));
phoneList.add(new Smartphone("Samsung", "Galaxy Y", 400, 774101));
phoneList.add(new Smartphone("Apple", "iPhone 7", 1100, 316300));
//The conditions
//If nokia + the price $1200+, it will save all the info about nokia
//If brand samsung + model Galaxy Y, It will count the total of the phone
Object obj;
int countSamsung = 0;
for (int i = 0; i < phoneList.size(); i++) {
Smartphone phone = phoneList.get(i);
if (phone.getBrand().equalsIgnoreCase("Nokia")) {
nokiaList.add(phone);
}
if (phone.getBrand().equalsIgnoreCase("Samsung")) {
if (phone.getModel().equalsIgnoreCase("Galaxy Y")) {
countSamsung++;
}
}
}

LinkedList isn't taking input

I seem to be having some problems with inserting values into a linked list. I am trying to create a program for the Josephus problem and I am suppose to take 3 numbers from the user. The first number is how many "people" there are, say its 4 you would have a list of 1,2,3,4. This is where I am stuck. Every time I enter in the 3 ints my program returns saying the List is empty and I can't figure out why. If anyone could help explain it would be greatly appreciated, thanks!
Main
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
long[] numbers = new long[3];
LinkedList circle = new LinkedList();
System.out.println("Please enter 3 numbers");
for (int i = 0; i < 3; i++)
{
numbers[i] = input.nextLong();
}
for (int i = 0; i < numbers[0]; i++)
{
circle.insertLink();
circle.move();
}
circle.getCurrent();
}
Link
class Link
{
public long dData;
public Link next;
public Link(long dd)
{
dData = dd;
}
public Link(int d, Link n)
{
this(d);
next = n;
}
public void displayLink()
{
System.out.print(dData + " ");
}
}
Linked List
class LinkedList
{
private Link current;
private int id;
public LinkedList()
{
current = null;
id = 1;
}
public void move()
{
current = current.next;
}
public boolean isEmpty()
{
if(current == null)
System.out.println("The List is empty");
return current == null;
}
public Link getCurrent()
{
return current;
}
public void setCurrent(int id)
{
while(current.dData != id)
move();
}
public Link getNext()
{
return current.next;
}
public void insertLink()
{
if(!isEmpty())
{
Link newlink = new Link(id++, current.next);
current.next = newlink;
}
else
{
Link newlink = new Link(id++);
newlink.next = newlink;
current = newlink;
}
}
public Link deleteLink()
{
Link temp = current.next;
if(current != current.next)
current.next = current.next.next;
else
current = null;
return temp;
}
}
when first time you call insertLink() methos you always will get The List is empty print. because at initialization the current variable of your linked list class is null. just remove System.out.println("The List is empty"); from your code.
public boolean isEmpty()
{
return current == null;
}
override toString() method to see your list
You are taking three inputs from user and iterating with first element of numbers array, i think it should be
for (int i = 0; i < numbers.length; i++)
{
circle.insertLink();
circle.move();
}
And at the first time when you insert the link, isEmpty() method checks your current variable is null or not if it is then it print the "The list is empty". so just remove that line.
System.out.println("The List is empty");

How to implement Search Function using Singly Linked List in Java?

Hello fellow Programmers!
I would like to ask how to implement the Search Function in Singly Linked List using Java (Data Structures).
PROBLEM: The Search function of my program can only access the head and tail of the elements.
Question: How can I access all the elements?
Sample Code:
import java.util.*;
public class MyLinkedList<T> {
private Node<T> head;
private Node<T> tail;
public void add(T element){
Node<T> nd = new Node<T>();
nd.setValue(element);
if(head == null){
head = nd;
tail = nd;
}
else {
tail.setNextRef(nd);
tail = nd;
}}
public void delete(){
if(head == null){
System.out.println("List is empty...");
}
else{
Node<T> tmp = head;
head = tmp.getNextRef();
if(head == null){
tail = null;
}
System.out.println("Deleted: "+tmp.getValue());
System.out.println("First Employee Name Deleted!");
}
}
public void search(){
Node<T> tmp = head;
if(head==null){
System.out.println("List is empty...");
}
else{
String b=""+tmp.getValue();
Scanner input=new Scanner(System.in);
System.out.print("Enter an employee name to search: ");
String search = input.nextLine();
if(b.equals(search)){
System.out.println("Found! "+tmp.getValue());
}
else if (b!=search){
System.out.println("Not Found!");
}
tmp = tmp.getNextRef();
} }
public void show(){
Node<T> tmp = head;
if(head==null){
System.out.println("List is empty...");
}
else{
while(true){
if(tmp == null){
break;
}
System.out.println(tmp.getValue());
tmp = tmp.getNextRef();
}}
}
public static void main(String a[]){
MyLinkedList<String> sl = new MyLinkedList<String>();
Scanner input = new Scanner(System.in);
sl.add("Garcia, Bianca Axel, 001");
sl.add("Temprosa, Camille Ann, 002");
sl.add("Villanueva, Von Justin, 003");
sl.add("Rivera, Reygie, 004");
sl.add("Dapapac, Ronnelle, 005");
sl.add("Bati, Aubrey, 006");
sl.add("Fiestada, Diana Rose, 007");
sl.add("Dobalada, Jojo, 008");
sl.add("Del Mundo, Maria Ethel, 009");
sl.add("Alejandro, Rachelle, 010");
System.out.print("Welcome To Singly Linked List Management for Employee Names!\n(Created by: Alex del Rosario || Source Code: Mr. John Carlo Son)\n");
while(true){
System.out.println("*------------------------*");
System.out.println("*PLEASE SELECT A FUNCTION*");
System.out.println("*1.)Add Employee Name *");
System.out.println("*2.)Delete Employee Name *");
System.out.println("*3.)Show Employee List *");
System.out.println("*4.)Search an Employee *");
System.out.println("*5.)Exit the program *");
System.out.println("*------------------------*");
System.out.print("Enter a number: ");
int select = input.nextInt();
if(select==1){
System.out.println("**Enter an employee name with ID number**");
System.out.println("Use this format:(Last Name),(First Name),(ID number)");
System.out.println("(ex. del Rosario, Alex, 0001)");
System.out.print("Input here: ");
input.nextLine();
String employee=input.nextLine();
sl.add(employee);
System.out.println("Employee: "+employee+" successfully added!");
}
else if(select==2){
sl.delete();
}
else if(select==3){
System.out.println("Employee List:");
sl.show();
}
else if(select==4){
sl.search();
}
else if(select==5){
System.out.println("*** THANK YOU FOR USING THIS PROGRAM! :) ***");
System.exit(0);
}
else{
System.out.println("INVALID INPUT!");
}
}}
}
class Node <T> implements Comparable<T> {
private T value;
private Node<T> nextRef;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public Node<T> getNextRef() {
return nextRef;
}
public void setNextRef(Node<T> ref) {
this.nextRef = ref;
}
#Override
public int compareTo(T arg) {
if(arg == this.value){
return 0;
} else {
return 1;
}
}
}
More a tipp than an answer. Ask yourself what you want to do. Do you want to use the linked list provided by Java or write your own code for a linked list?
With the provided list iterating is easy.
private void search(String searchElement) {
for (int i = 0; i < list.size(); i++) {
String listElement = list.get(i);
if (searchElement.equals(listElement)) {
System.out.println("Found element " + searchElement + " at position " + i);
return;
}
}
System.out.println(searchElement + " does not appear in the list");
}
When writing your own list you have to be more careful how you use the list. For example your implementation of delete() will not work.
public void delete(){
if(head == null){
System.out.println(List is empty...);
}
else{
NodeAlex tmp = head;
head = tmp.getNextRef();
if(head == null){ // you need to check if head == tail or head.getNextRef() == null
tail = null; // here you have to set tmp.setNextRef(null);
// and additionally define tmp as tail.
}
System.out.println(Deleted +tmp.getValue());
System.out.println(First Employee Name Deleted!);
}
}
Edit: To access the list you have two possibilities. At first you have to create a new instance, i.e., (here for Strings, you might want to use other types/objects)
List<String> list = new LinkedList<String>();
Then you basically have two opportunities on how to access the list in the methods.
(a) Your method is in the same class where you initialized the list, however, list has to be a global field.
private List<String> list;
private static void main(String[] args) {
list = new LinkedList<String>();
(...)
}
private static void search(String searchElement) {
// has access on list
}
(b) You simply pass your list to the method you need and if the method modifies the list you need to return the list again.
(main class, main method)
search(list, "Test");
list = delete(list, "Test");
(the class where the methods are located)
public void search(List<String> list, String searchElement) { ... }
public List<String> delete(List<String> list, String elementToDelete) {
// delete element
return list;
}

Traversing a custom linked list

I'm writing a program to simulate memory fragmentation. The input file tells what segments need to be input at what time.
A sample file is:
N
C 200
P 1 2 3
P 2 3 4
P 2 3 1
R
E
where C is the memory size, P is the segment in the order (size, start time, and life time), and R (should) print out a report showing what segments, and any holes are in memory and where.
One of the rules of this assignment is to create a linked list of the events, where insertions and deletions of the segments are created as events, and I need to traverse the event list.
UPDATE: I have something different, but I know for sure it's not inserting my Events into the Event List. I don't understand why. Does anyone see where my logic is off?
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class TestEventList{
public static void main(String[] args){
//read file
File file = new File("b.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
//send it to interpret file method:
interpretFile(line);
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} //end try-catch
}
public static void interpretFile(String command) {
EventList evtList = new EventList();
Scanner sc = new Scanner(command);
char initialCommand = command.charAt(0);
if (initialCommand == 'N') {
System.out.println("Name");
} else {
}//end else
//file error
char commandCode = command.charAt(0);
String skip = sc.next(); //skips next character to get to integers
switch (commandCode) {
case 'C':/*create Memory! which means, create Event!
Form: Event(int startTime, Segment memSegment)*/
int size = sc.nextInt();
Segment defaultMemoryNode = new Segment(size, 100, false );
/*create event node*/
Event insertDefaultNode = new Event(0, defaultMemoryNode);
/*insert this event*/
evtList.insertEvent(insertDefaultNode);
break;
case 'P':
int segmentSize = sc.nextInt();
int segmentStart = sc.nextInt();
int segmentLife = sc.nextInt();
int segmentExpiration = segmentLife + segmentStart;
Segment memorySegment = new Segment(segmentSize, segmentExpiration, true );
Event addSegment = new Event(segmentStart, memorySegment);
evtList.insertEvent(addSegment);
memorySegment.occupied = false;
Event removeSegment = new Event(segmentExpiration, memorySegment);
evtList.insertEvent(removeSegment);
break;
case 'R':
evtList.traverseEventList();
break;
case 'E':
System.exit(0);
}//end switch
}//end interpretfile method
} //end class T.E.L.
/*This class has-a Linked List, has-a memoryNode, has-a Segment*/
class MemoryList{
private Node memoryNode = new Node();
private Segment memorySegment = new Segment();
private LinkedList memoryList = new LinkedList();
Node head;
Node current;
public MemoryList(){
super();
}
/*define blocks and holes*/
public void insertBlock(Segment memorySegment) {
current = head;
if (current == null) {
memoryList.Add(memorySegment);
System.out.println(memorySegment.size);
}
else {
System.out.println("Checking for room");
System.out.println(current.getSize());
int invalidFit=0;
if(current.getStatus() == false && current.getSize()>=memorySegment.size){
System.out.println("Verified space");
int freeSpace = current.getSize() - memorySegment.size;
memoryList.Add(memorySegment);
createHole(freeSpace);
current = current.next;
} //end if
else {
current = current.next;
} //end else
}//end else
} //end insert block
public void removeBlock(Segment expiredSegment){
current = head;
//search for segment
while(current.next != null){
if(current.getTimetoLeave() == expiredSegment.timeToLeave
&& current.getSize() == expiredSegment.size){
memoryList.Remove(expiredSegment);
int freespace = expiredSegment.size;
createHole(freespace);
}
else{
current = current.next;
}
}//end while
}
private void createHole(int space) {
Node hole = new Node(space, 100, false);
memoryList.Add(hole);
//test if there are two holes together. if so, mergeHoles.
}
*Merge 2 Consecutive Holes*/
private void mergeHoles(Node a, Node b) {
//getPrev(a); //find previous of node a
//use the size through the end of a's prev to
//get start of prev.next (a)+
//make a point to b.next?
} //end mergeHoles
public void traverseMemoryList(){
current = head;
if(current == null){
System.out.println("Memoryless");
}
else{
while(current.next != null){
if(memoryNode.getStatus() == false){
System.out.println("Hole");
current = current.next;
}
}
System.out.println("Segment of size " + current.getSize());
current = current.next;
}
}
} //end MemoryList
class MemoryNode extends Node{
public MemoryNode(){
super();
}
}
class Segment{
int size;
int timeToLeave;
boolean occupied;
/*constructor*/
public Segment(){
}
public Segment(int newSize, int newTime, boolean isOccupied){
this.size = newSize;
this.timeToLeave = newTime;
this.occupied = isOccupied;
}
}
class Node {
private int size;
private int timeToDepart;
boolean occupied; // True if segment, false if hole
Node next;
public Object data; //data in a node
public Node() {
}
public Node(int segmentSize, int timeToLeave, boolean type) {
this.size = segmentSize;
this.timeToDepart = timeToLeave;
this.occupied = type;
}
public int getSize() {
return size;
}
public void setSize(int segmentSize) {
size = segmentSize;
}
public int getTimetoLeave() {
return timeToDepart;
}
public void setTimetoLeave(int timeToLeave) {
timeToDepart = timeToLeave;
}
public void setStatus(boolean type) {
occupied = type;
}
public boolean getStatus() {
return occupied;
}
} //end Node
/* class LL has-a Node*/
class LinkedList{
private Node listNode= new Node();
Node current;
Node head;
Node prev;
int size;
/*Constructors:*/
public LinkedList() {
super();
}
public LinkedList(int j, int k, boolean l) {
super(); //essentially the same as a node
}
/*LL proprietary methods*/
/*test if the list is empty, to avoid NullPointerException*/
public boolean isEmpty() {
return head == null;
}
//insert method:
public void Add(Object data1) {
listNode.data = data1;
/*special case: list is empty*/
if (isEmpty()) {
listNode.next = head;
head = listNode;
head.data = listNode.data;
}
else{
current = head;
while(current.next != null)
{
current.data = data1;
current.next = null;
head = current;
}
current.data = data1;
current.next = head; //newNode now points to head
head = current; //now newNode is the head
}
}
public void Remove(Object delData) {
/*pointers*/
//special case: if head is the removed node;
if (current.data == delData) {
head = current.next;
} else {
prev = head; //it's not the head, keep moving.
current = current.next;
while (current.next != null) { //reached end of list
if (current.data == delData) { //if
prev.next = current.next; //just skip the current node
} else {
prev = current; //now prev is that node
current = current.next; //current is the next node
}
} //end while
//what if current.next = null (it's at the end)?
if (current.next == null && current.data == delData) {
prev.next = null;
}
}//end else
}
public void traverse(){
if(head== null){
System.out.println("no elements to show");
}
else{
current = head;
while(current.next != null){
current = current.next;
}
}}
}// end LL class
/*class EventList has-an Event, is-a LinkedList*/
class EventList{
private Event event = new Event();
private LinkedList evtList = new LinkedList();
private MemoryList memList = new MemoryList();
Node current;
Node head;
int time; //set to the most recent time
/*constructor*/
public EventList(){
super();
}
public void actionOfEvent(Event event1){
Segment p = event.getMemorySegment();
if(p.occupied == true){
insertSegment(event1);
}
else
removeSegment(event1);
}
//a linked list to control creation of events
public void insertEvent(Event event) {
current = head;
if(current == null){
evtList.Add(event);
System.out.println("Added 1st event " + event.startTime);
}
else{
while(current.next != null){
if(event.startTime <= event.getTime()){
//if the event start was before the current time...
evtList.Add(event);
current = current.next;
}
else{
current = current.next;
}
}//end while
evtList.Add(event);
System.out.println("Added 2nd event");
}
}//end insertEvent
public void traverseEventList(){
current = head;
if(current == null){
System.out.println("At time " + event.getTime());
System.out.println("uneventful");
}
else{
while (current.next != null){
Segment segment1 = event.getMemorySegment();
if(segment1.occupied = true){
memList.insertBlock(segment1);
System.out.println(segment1.size + " inserted");
}
else{
memList.removeBlock(segment1);
System.out.println(segment1.size + " removed from memory.");
}
}
}
}
public void insertSegment(Event addEvent){
addEvent.getMemorySegment();
memList.insertBlock(addEvent.getMemorySegment());
}
public void removeSegment(Event expEvent){
}
} //end eventList
/*class Event is-a Node*/
class Event{
int startTime;
Segment memoryNode;
int time;
public Event(){
super();
}
//pretty much the same as Node.
public Event(int newStartTime, Segment newMemNode){
super();
this.startTime = newStartTime;
this.memoryNode = newMemNode;
}
public void setTime(int newStartTime){
time = newStartTime;
}
public int getTime(){
return time;
}
public void setMemorySegment(Segment newMemNode){
memoryNode = newMemNode;
}
public Segment getMemorySegment(){
return memoryNode;
}
}//end class Event
class Report{
int currentTime= 0;
//this creates and prints the segments/holes in the list at curTime
}
I ran your code and it seems that you never call:
setMemoryNode();
This is causing NullPointerExceptions.
Also:
Some of the multiple event instances are being caused by these lines:
EventSequenceList expiredNode = new EventSequenceList(newMemNode,
1, expir, 1, true);
insertEvent(expiredNode);
I will edit this as I see more.
Just a few (other) remarks
Design
You use a lot of inheritance. Is that really necessary? Later on, for production code, you should consider using composition instead of inheritance and code against interfaces. That will remove a lot of ugly dependencies and improve maintainability. Now you have
EventSequenceList is-a MemoryList is-a LinkedList is-a Node
Just from the names, I have some doubt, that a LinkedList really is-a Node. I expect a Node in trees or graphs and even there it's usually a has-a relationship.
Naming
Sometimes you break with Java naming conventions: method names should not start with a capital letter (like Add). Sometimes you use one-letter-variable names (like in most of your constructors).
Sometimes a methodname does not tell us, what the method is really doing (like iterpretFile which actually does not interpret a file but only a single command that may have been read from a file)
The more I look at the assignment, the more I get the feeling, that you'll get stuck with your design sooner or later (more sooner than later). From what I read, what is required:
One event model class. A Class, that represents an insertion or deletion event.
One memory model class. A Class, that represents the entire memory
One segment model class. A Class that represents a segment. A memory class has a list or an array of segments
One linked list that holds all events. This custom linked list may be capable of inserting an event at the right place
One reporting class. A class that can create and print a report.
One input file parser. It will use the input to
create a memory class (with an appropriate number of segments)
create insertion and deletion events from the P lines
insert the events in the linked list
Absolutely no inheritance is needed.
Edit - in response to your last comments
A memory has-an array of cells. The cells are indexed, starting with 0. They are not linked, so I actually don't see any reason to use a LinkedList here. A memory model could look like:
public class Memory {
private int[] cells;
public Memory(int size) { cells = new int[size]; }
public void store(int index, int value) {
if (index < 0 || index >= size) throw new IllegalArgumentException("..");
cells[index] = value;
}
public int read(int index) {
if (index < 0 || index >= size) throw new IllegalArgumentException("..");
return cells[index];
}
}
A segment could be seen as a subclass of Memory. In real life, a segment is requested from a memory manager and the manager allocates a region, if possible. Segments are totally independant, no link between them, no use for a LinkedList here. A quick draft:
public class MemoryManager {
private Memory managedMemory;
public MemoryManager(Memory memory) { this.memory = memory; }
public Segment getSegment(int size) {
int startAddress = allocateSegment(int size);
if (startAddress != -1) {
return new Segment(this, startAddress, size);
}
return null;
}
}
public class Segment extends Memory {
private MemoryManager memoryManager;
private int startAddress; // usually - a handle, so that the memoryManager can
// relocate the segment - we keep it simple
public Segment(MemoryManager memoryManager, int startAdress, int size) {
super(size);
this.memoryManager = memoryManager;
this.startAddress = startAddress;
}
Now back to the events.
One of the rules of this assignment is to create a linked list of the events [eventList = new EventList<Event>()] , where insertions and deletions of the segments are created as events [new Event(EventType.INSERT, int time, Segment segment)); new Event(EventType.DELETE, int time, Segment segment);] , and I need to traverse the event list [for(Event event:eventList)].
That's the task. implement an Event class, implement an EventList class, implement a small enum EventType. The challenge is to implement an insert method in EventClass that inserts two events for one P line at the right places (timestamps).

Categories