关于JAVA实现数据结构问题,有请高手回答!!

nzfghtk 2008-03-22 01:01:26

class Node1 {
int a;
Node1 next;
}
public class Mylist {
Node1 head=null;
public static void main(String args[])
{
Mylist b=new Mylist();
int i=1;
while(i<=15)
{b.insert(i);i++;}
b.showlist();
}
Node1 gettop()
{
return head;
}

void insert(int obj)
{
Node1 temp=null;
if(this.head==null)
{ head=new Node1();
temp=new Node1();
temp.a=obj;
temp.next=null;
head.next=temp;

}
else
{
temp=new Node1();
temp.a=obj;
temp.next=this.head.next;
this.head.next=temp;
}
}
void showlist()
{
Node1 ptr=null;
ptr=this.head;
if(ptr==null)
{System.out.println("NULL");}
while(ptr!=null)
{ ptr=ptr.next;
System.out.print(ptr.a+" ");
}
}
}



以上是一个链式堆栈的代码,运行能得到预期的结果,但是结果出现之后ECLIPSE会转到DEBUG模式,并且报空指针错误.这我就不明白了,如果我用了未实例化的指针,那怎么又会运行出指针所指向的正确结果呢?运行结果如下:
java.lang.NullPointerException
at Mylist.showlist(Mylist.java:48)
at Mylist.main(Mylist.java:14)
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Exception in thread "main"
那些数字是把链表堆栈中的数据倒序打印出来所得到的,和预期相符合,就是这几个异常不知道怎么回事;
...全文
239 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
nzfghtk 2008-03-25
  • 打赏
  • 举报
回复
没想到有这么多问题啊,谢谢DAN1980!!
icekingson 2008-03-25
  • 打赏
  • 举报
回复
我的那个方法的确将节点删掉了啊 我看过了
Dan1980 2008-03-24
  • 打赏
  • 举报
回复
名字取反了,删除怎么叫push?应该叫pop吧?

其他问题不大,怎么会“运行时和没有加这个方法一样”呢?删除以后应该是从14开始打印(15已经删掉了)

还有,整体来说你的代码很不严密,导致程序不健壮,就是C++也不能写成这样吧?

比如你的代码:
public void push() {
if (this.gettop().next != null) { // 如果this.gettop()返回null,会抛出异常
Node1 ptr = this.gettop().next;
this.gettop().next = ptr.next;
// 上面两句其实可以合并成一句,写成:
// this.gettop().next = this.gettop().next.next就行了,并且这样写原来的节点不再存在被引用,内存能及时回收,
// 你这里动用了变量ptr,过后又没有执行ptr = null,致使节点虽然从表上脱落下来了,却被另一个变量引用,当然,这里方法很快就结束了,问题不大,但应该养成严密的编程习惯
}

我会改进成下面这样:

public void pop() {
if (head == null || head.next == null) return; // 如果表为空,直接返回
head.next = head.next.next; // 让表头直接指向第一个节点的后继节点,第一个节点自动脱落
}

这样,无论从健壮性和代码清晰性上都有很大改观。
nzfghtk 2008-03-24
  • 打赏
  • 举报
回复
人都哪里去了??
tangme 2008-03-23
  • 打赏
  • 举报
回复
我刚开了一个qq群,大家加进来一起探讨一下java
58517889
nzfghtk 2008-03-22
  • 打赏
  • 举报
回复
JAVA确实没有指针,只有地址引用,这种只不过是C++学多了的人的一种习惯
nzfghtk 2008-03-22
  • 打赏
  • 举报
回复
请大家帮帮我...
qlykssl 2008-03-22
  • 打赏
  • 举报
回复
java不是不用指针了么 怎么这个代码有指针呢? 不太明白
nzfghtk 2008-03-22
  • 打赏
  • 举报
回复
这个不需要申请空间了吧,申请了不还是和以前一样没有删除掉接点....
nzfghtk 2008-03-22
  • 打赏
  • 举报
回复
谢谢Dan1980 ,我以前是学C++的,才开始学习JAVA,上面的代码也是根据C++的思路写的,希望能加你为好友,多多向高手学习
icekingson 2008-03-22
  • 打赏
  • 举报
回复
class Node1
{
int a;
Node1 next;
}

public class Mylist
{
Node1 head=null;
public static void main(String args[])
{
Mylist b=new Mylist();
int i=1;
while(i<=15)
{
b.insert(i);

i++;
}
b.push();
b.showlist();
}

Node1 gettop()
{
return head;
}

void insert(int obj)
{
Node1 temp=null;
if(this.head==null)
{
head=new Node1();
temp=new Node1();
temp.a=obj;
temp.next=null;
head.next=temp;

}
else
{
temp=new Node1();
temp.a=obj;
temp.next=this.head.next;
this.head.next=temp;
}
}
void showlist()
{
Node1 ptr=null;
ptr=this.head;

if(ptr==null)
{
System.out.println("NULL");
}
while(ptr.next != null)
{
ptr=ptr.next;
System.out.print(ptr.a+" ");
}
}

public void push()
{
if(this.gettop().next != null)
{
Node1 ptr = new Node1();
ptr = this.gettop().next;
this.gettop().next = ptr.next;
}
}

}


你想要的是这个效果么 这个节点没有申请空间哦 我也是刚学java 大家一起奋斗java哈
nzfghtk 2008-03-22
  • 打赏
  • 举报
回复
class Node1 {
int a;
Node1 next;
}
public class Mylist {
Node1 head=null;
public static void main(String args[])
{
Mylist b=new Mylist();
int i=1;
while(i <=15)
{b.insert(i);i++;}
b.push();
b.showlist();
}
Node1 gettop()
{
return head;
}

void insert(int obj)
{
Node1 temp=null;
if(this.head==null)
{ head=new Node1();
temp=new Node1();
temp.a=obj;
temp.next=null;
head.next=temp;

}
else
{
temp=new Node1();
temp.a=obj;
temp.next=this.head.next;
this.head.next=temp;
}
}
void showlist()
{
Node1 ptr=null;
ptr=this.head;
if(ptr==null)
{System.out.println("NULL");}
while(ptr!=null)
{ ptr=ptr.next;
System.out.print(ptr.a+" ");
}
}
public void push() {
if (this.gettop().next != null) {
Node1 ptr = this.gettop().next;
this.gettop().next = ptr.next;
}
}


Dan1980你还在吗>我加了一个删除结点的方法,但是运行时和没有加这个方法一样,帮我看看是怎么回事好吗?
Dan1980 2008-03-22
  • 打赏
  • 举报
回复
遍历的代码写错了,while(ptr!=null)应该是while(ptr.next!=null),你只是保证当前指针不是null,而你要打印的是下一个指针指向的Node的值。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧