gpt4 book ai didi

java - 遍历自定义链表

转载 作者:行者123 更新时间:2023-11-30 09:47:54 26 4
gpt4 key购买 nike

我正在编写一个程序来模拟内存碎片。输入文件告诉什么段需要在什么时候输入。

示例文件是:

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

}

最佳答案

我运行了你的代码,你似乎从来没有调用过:

setMemoryNode();

这会导致 NullPointerExceptions。

还有:

某些多个事件实例是由这些行引起的:

EventSequenceList expiredNode = new EventSequenceList(newMemNode,
1, expir, 1, true);
insertEvent(expiredNode);

我会在看到更多内容后对其进行编辑。

关于java - 遍历自定义链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6501788/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com