双节点 ADT - 节点的打印节点



第一:这是家庭作业!我真的不希望有人为我编码,但我需要帮助找到我搞砸的地方。

第二:问题:我们正在与ADT合作,并创建一个DoubleLinkedNode ADT(专门持有双精度(。我看了我的讲座(在线学生(,阅读了阅读材料,并在这里和其他网站上进行了在线研究,试图解决这个问题。我似乎能够添加节点并在以前的分配中删除它们/更改元素/索引(节点(位置/克隆节点......但是每次我尝试实现一种方法来逐个节点并输出时,我的方法最终都会抓住我的节点的尾巴并无限打印它......或我循环方法的次数(IE manyNodes(。

这是我当前的代码:

public class DoubleNode {
private double data;
private DoubleNode link;
public DoubleNode(double initialData, DoubleNode initialLink)
{
data = initialData;
link = initialLink;
}
public void addNodeAfter(double item)
{
link = new DoubleNode(item, link);
}
public double getData( )
{
return data;
}
public DoubleNode getLink( )
{
return link;
}
public static DoubleNode listCopy(DoubleNode source)
{
DoubleNode copyHead;
DoubleNode copyTail;
if (source == null)
return null;
copyHead = new DoubleNode(source.data, null);
copyTail = copyHead;
while (source.link != null)
{
source = source.link;
copyTail.addNodeAfter(source.data);
copyTail = copyTail.link;
}
return copyHead;
}
public static DoubleNode[ ] listCopyWithTail(DoubleNode source)
{
DoubleNode copyHead;
DoubleNode copyTail;
DoubleNode[ ] answer = new DoubleNode[2];
if (source == null)
return answer;
copyHead = new DoubleNode(source.data, null);
copyTail = copyHead;
while (source.link != null)
{
source = source.link;
copyTail.addNodeAfter(source.data);
copyTail = copyTail.link;
}
answer[0] = copyHead;
answer[1] = copyTail;
return answer;
}
public static int listLength(DoubleNode head)
{
DoubleNode cursor;
int answer;
answer = 0;
for (cursor = head; cursor != null; cursor = cursor.link)
answer++;
return answer;
}
public static DoubleNode[ ] listPart(DoubleNode start, DoubleNode end)
{
DoubleNode copyHead;
DoubleNode copyTail;
DoubleNode cursor;
DoubleNode[ ] answer = new DoubleNode[2];
copyHead = new DoubleNode(start.data, null);
copyTail = copyHead;
cursor = start;
while (cursor != end)
{
cursor = cursor.link;
if (cursor == null)
throw new IllegalArgumentException
("nn***LAST NODE NOT ON LIST***nn");
copyTail.addNodeAfter(cursor.data);
copyTail = copyTail.link;
}
// Return the head and tail references
answer[0] = copyHead;
answer[1] = copyTail;
return answer;
}
public static DoubleNode listPosition(DoubleNode head, int position)
{
DoubleNode cursor;
int i;
if (position <= 0)
throw new IllegalArgumentException("nn***POSITION DOES NOT EXIST***nn");
cursor = head;
for (i = 1; (i < position) && (cursor != null); i++)
cursor = cursor.link;
//love you
return cursor;
}
public static DoubleNode listSearch(DoubleNode head, double target)
{
DoubleNode cursor;
for (cursor = head; cursor != null; cursor = cursor.link)
if (target == cursor.data)
return cursor;
return null;
}
public void removeNodeAfter( )
{
link = link.link;
}
public void setData(double newData)
{
data = newData;
}
public void setLink(DoubleNode newLink)
{
link = newLink;
}
public String toString() {
String str;
str = "n" + data;
return str;
}
}
public class DoubleLinkedSeq implements Cloneable {
private static DoubleNode head, tail, currentElement, cursor, precursor, cursorTemp;
private static int manyNodes;
public DoubleLinkedSeq(double element) {
head = null;
tail = null;
currentElement = null;
manyNodes = 0;
}
public DoubleLinkedSeq() {
cursor = tail;
precursor = head;
}
public static void addFirst(double element) {
if (head == null) {
DoubleNode node = new DoubleNode(element, null);
head = node;
tail = node;
cursor = head;
precursor = head;
}
else {
DoubleNode node = new DoubleNode(element, head);
precursor = head;
cursor = precursor.getLink();
}
manyNodes++;
}
public static void addAfter(double element) {
if (isCurrent()) {
cursor.addNodeAfter(element);
precursor = cursor;
cursor = cursor.getLink();
}
else {
if(tail == null) {
tail = new DoubleNode(element, null);
precursor = tail;
cursor = tail;
tail.addNodeAfter(element);
cursor = tail.getLink();
}
else {
precursor = tail;
tail = tail.getLink();
cursor = tail;
cursor.addNodeAfter(element);
}
}
manyNodes++;
}
public static void addBefore(double element) {
if (isCurrent()) {
if (cursor == head) {
precursor = new DoubleNode(element, cursor);
head = precursor;
cursor = precursor.getLink();
}
else {
precursor = new DoubleNode(element, cursor);
cursor = precursor.getLink();
}
}
else {
if (head == null) {
head = new DoubleNode(element, null);
cursor = head;
precursor = head;
cursor = precursor.getLink();
tail = cursor;
}
else {
precursor = new DoubleNode(element, cursor);
head = precursor;
tail = cursor;
}
}
manyNodes++;
}
public static void addAll(DoubleLinkedSeq addend) {
if(addend == null) {
throw new IllegalArgumentException("nn***LIST IS EMPTY***nn");
}
if(addend.size() > 0) {
tail.setLink(addend.head);
tail = addend.tail;
manyNodes += addend.size();
}
}
public static void advance() {
if(!isCurrent()) {
return;
}
precursor = cursor;
cursor = cursor.getLink();
}
public Object clone() {
DoubleLinkedSeq answer;
try {
answer = (DoubleLinkedSeq) super.clone();
}
catch(CloneNotSupportedException e) {
throw new RuntimeException("nn***CLASS DOES NOT IMPLEMENT CLONEABLE***nn");
}
answer.head = DoubleNode.listCopy(head);
return answer;
}
public static DoubleLinkedSeq catenation(DoubleLinkedSeq s1, DoubleLinkedSeq s2) {
DoubleLinkedSeq s3 = new DoubleLinkedSeq(s1.manyNodes + s2.manyNodes);
System.arraycopy(s1.currentElement, 0, s3.currentElement, 0, s1.manyNodes);
System.arraycopy(s2.currentElement, 0, s3.currentElement, s1.manyNodes, s2.manyNodes);
s3.manyNodes = (s1.manyNodes + s2.manyNodes);
return s3;
}
public double getCurrent( )
{
if(!isCurrent()) {
throw new IllegalStateException("nn***CURRENT ELEMENT IS NULL***nn");
}
return cursor.getData();
}
public static boolean isCurrent( )
{
if (cursor == null) {
return false;
}
else {
return true;
}
}
public static void removeCurrent( )
{
if(!isCurrent()) {
throw new IllegalStateException("nn***CURRENT ELEMENT IS NULL***nn");
}
else if(manyNodes == 0) {
throw new IllegalStateException("nn***LIST IS EMPTY***nn");
}
else if(manyNodes == 1) {
head = null;
tail = null;
cursor = null;
}
else if(cursor == head) {
head = head.getLink();
cursor = head;
}
else if(cursor == tail) {
tail = null;
cursor = null;
}
else {
DoubleNode pre = head;
while(pre.getLink() != cursor) {
pre = pre.getLink();
}
if(cursor == tail) {
pre.setLink(null);
tail = pre;
cursor = null;
}
else {
pre.setLink(cursor.getLink());
cursor = pre;
}
}
manyNodes--;
}
public static int size( )
{
return manyNodes;
}
public static void start( )
{
if(head == null) {
cursor = null;
}
cursor = head;
}
/**
* Return for Output
* @return
*/
public String toString() {
String str="-> ";
cursorTemp = head;
while (cursorTemp != null) {
str = str + cursorTemp.toString();
cursorTemp = cursorTemp.getLink();
}
return str;
}
}//END CLASS
public class DoubleLinkedSeqTest {
private static final SecureRandom generator = new SecureRandom();
private static int index, menuSelect, i, size;
private static String tmp;
private static double dataManip;
private static DoubleLinkedSeq list = new DoubleLinkedSeq();
public static void main(String[] args) {
DoubleLinkedSeq.start();
populateSequence();
}
private static void populateSequence() {
size = generator.nextInt(15);
for (i = 0; i < size; i++) {
dataManip = ((generator.nextDouble() * 12.3152) - (generator.nextDouble() * 7.9221));
list.addFirst(dataManip);
}
for(i = 0; i < size; i++) {
System.out.printf("%s %n", list.toString());
}
} //END populateSequence METHOD
}

已修复 - 感谢您的帮助。每个建议都让我到达那里。

损坏的代码段是:公共静态空添加第一

固定代码:

public static void addFirst(double element) {
if (head == null) {
DoubleNode node = new DoubleNode(element, null);
head = tail = node;
precursor = cursor = head;
} else {
DoubleNode node = new DoubleNode(element, head);
head = precursor = node;
cursor = precursor.getLink();
}
manyNodes++;
System.out.println("Node Count: " + manyNodes);
}

最新更新