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"
那些数字是把链表堆栈中的数据倒序打印出来所得到的,和预期相符合,就是这几个异常不知道怎么回事;
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你还在吗>我加了一个删除结点的方法,但是运行时和没有加这个方法一样,帮我看看是怎么回事好吗?
{
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哈
58517889
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; // 让表头直接指向第一个节点的后继节点,第一个节点自动脱落
}
这样,无论从健壮性和代码清晰性上都有很大改观。