Java初学者CSDN第4问!

nisus_lee 2005-10-28 04:39:06
class Link
{
public long dData;
public Link next;

public Link(long dd)
{
dData=dd;
}

public void displayLink()
{
System.out.print(dData+" ");
}
}

class LinkList
{
private Link first;

public LinkList()
{
first=null;
}

public boolean isEmpty()
{
return(first==null);
}

public void insertFirst(long dd)
{
Link newLink=new Link(dd);
newLink.next=first;
first=newLink;
}

public long deleteFirst()
{
Link temp=first;
first=first.next;
return temp.dData;
}

public void displayList()
{
Link current=first;
while(current!=null)
{
current.displayLink();
current=current.next;
}
System.out.println("");
}
}

class LinkStack
{
private LinkList theList;

public void push(long j)
{
theList.insertFirst(j);
}

public long pop()
{
return theList.deleteFirst();
}

public boolean isEmpty()
{
return (theList.isEmpty());
}

public void displayStack()
{
System.out.print("Stack(top-->bottom):");
theList.displayList();
}
}

class LinkStackApp
{
public static void main(String[] args)
{
LinkStack theStack=new LinkStack();

theStack.push(20);
theStack.push(40);

theStack.displayStack();

theStack.push(60);
theStack.push(80);

theStack.displayStack();

theStack.pop();
theStack.pop();

theStack.displayStack();
}
}

编译通过,可是无法运行,显视的是:
Exception in thread "main" java.lang.NullPointerExcep
at LinkStack.push(LinkStackApp.java:66)
at LinkStackApp.main(LinkStackApp.java:92)
望各位指点下 !
...全文
134 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
superslash 2005-10-28
  • 打赏
  • 举报
回复
wo dao
nisus_lee 2005-10-28
  • 打赏
  • 举报
回复
问题解决 ..谢谢 ……
yanxiazhiqiu 2005-10-28
  • 打赏
  • 举报
回复
你就是问题终结者 ,求倒!
zhaoguoqingluntan 2005-10-28
  • 打赏
  • 举报
回复
up

ianok 2005-10-28
  • 打赏
  • 举报
回复
up
classjava 2005-10-28
  • 打赏
  • 举报
回复
class LinkStack
{
private LinkList theList;//这个改成private LinkList theList=new LinkList();

public void push(long j)
{
theList.insertFirst(j);
}

public long pop()
{
return theList.deleteFirst();
}

public boolean isEmpty()
{
return (theList.isEmpty());
}

public void displayStack()
{
System.out.print("Stack(top-->bottom):");
theList.displayList();
}
}

62,614

社区成员

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

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