java.编写一个链表。出现空指针异常。求助。附代码
publicclassdd{publicstaticvoidmain(String[]args){OperaNodeop=newOperaNode();op.addToH...
public class dd {
public static void main(String[] args) {
OperaNode op = new OperaNode();
op.addToHead(1);
op.addToHead(2);
op.addToHead(3);
op.addToHead(5);
op.printAll();
op.getMax();
}
}
class Node {
public int data;
public Node next;
public Node()
{
}
public Node(int i) {
data=i;
next=null;
}
public Node(int i, Node n) {
data = i;
next = n;
}
}
//链表类
class OperaNode {
public int max;
public Node head, tail;
public OperaNode() {
head = tail = null;
}
// 判断链表是否为空
public boolean isEntry() {
if (head == null) {
return true;
} else
{
return false;
}
}
// 在链表的头部添加数据
public void addToHead(int l) {
head = new Node(l, head);
//if (tail == null) {
//tail = head;
//}
}
public void printAll() {
Node temp;
for (temp = head; temp != null ; temp = temp.next) {
System.out.println(temp.data);
}
}
public void getMax()
{
Node temp =null;
for (temp = head; temp != null ; temp = temp.next){
if(head.data>head.next.data){
//temp.data=head.data;
}
//head = head.next;
}
System.out.println(temp.data);
}
}
下面是错误提示。。。。。。。
Exception in thread "main" java.lang.NullPointerException
at quickstart.javabean.OperaNode.getMax(dd.java:77)
at quickstart.javabean.dd.main(dd.java:12)
求大侠们帮帮忙啊 展开
public static void main(String[] args) {
OperaNode op = new OperaNode();
op.addToHead(1);
op.addToHead(2);
op.addToHead(3);
op.addToHead(5);
op.printAll();
op.getMax();
}
}
class Node {
public int data;
public Node next;
public Node()
{
}
public Node(int i) {
data=i;
next=null;
}
public Node(int i, Node n) {
data = i;
next = n;
}
}
//链表类
class OperaNode {
public int max;
public Node head, tail;
public OperaNode() {
head = tail = null;
}
// 判断链表是否为空
public boolean isEntry() {
if (head == null) {
return true;
} else
{
return false;
}
}
// 在链表的头部添加数据
public void addToHead(int l) {
head = new Node(l, head);
//if (tail == null) {
//tail = head;
//}
}
public void printAll() {
Node temp;
for (temp = head; temp != null ; temp = temp.next) {
System.out.println(temp.data);
}
}
public void getMax()
{
Node temp =null;
for (temp = head; temp != null ; temp = temp.next){
if(head.data>head.next.data){
//temp.data=head.data;
}
//head = head.next;
}
System.out.println(temp.data);
}
}
下面是错误提示。。。。。。。
Exception in thread "main" java.lang.NullPointerException
at quickstart.javabean.OperaNode.getMax(dd.java:77)
at quickstart.javabean.dd.main(dd.java:12)
求大侠们帮帮忙啊 展开
3个回答
展开全部
我看了你的程序,帮你改完了,主要改了getMax函数,你看看吧。
完整的程序如下:
public class dd {
public static void main(String[] args) {
OperaNode op = new OperaNode();
op.addToHead(1);
op.addToHead(2);
op.addToHead(3);
op.addToHead(5);
op.printAll();
op.getMax();
}
}
class Node {
public int data;
public Node next;
public Node(){}
public Node(int i) {
data=i;
next=null;
}
public Node(int i, Node n) {
data = i;
next = n;
}
}
//链表类
class OperaNode {
public int max;
public Node head, tail;
public OperaNode() {
head = tail = null;
}
// 判断链表是否为空
public boolean isEntry() {
if (head == null) {
return true;
} else {
return false;
}
}
// 在链表的头部添加数据
public void addToHead(int l) {
head = new Node(l, head);
if (tail == null) {
tail = head;
}
}
public void printAll() {
Node temp;
for (temp = head; temp != null ; temp = temp.next) {
System.out.println(temp.data);
}
}
public void getMax(){
Node temp =null;
Node tmp =head;
for (temp = head; temp != null ; temp = temp.next){
if(tmp.data<temp.data){
tmp.data=temp.data;
}
//head = head.next;
}
System.out.println(tmp.data);
}
}
运行结果:
5
3
2
1
5
完整的程序如下:
public class dd {
public static void main(String[] args) {
OperaNode op = new OperaNode();
op.addToHead(1);
op.addToHead(2);
op.addToHead(3);
op.addToHead(5);
op.printAll();
op.getMax();
}
}
class Node {
public int data;
public Node next;
public Node(){}
public Node(int i) {
data=i;
next=null;
}
public Node(int i, Node n) {
data = i;
next = n;
}
}
//链表类
class OperaNode {
public int max;
public Node head, tail;
public OperaNode() {
head = tail = null;
}
// 判断链表是否为空
public boolean isEntry() {
if (head == null) {
return true;
} else {
return false;
}
}
// 在链表的头部添加数据
public void addToHead(int l) {
head = new Node(l, head);
if (tail == null) {
tail = head;
}
}
public void printAll() {
Node temp;
for (temp = head; temp != null ; temp = temp.next) {
System.out.println(temp.data);
}
}
public void getMax(){
Node temp =null;
Node tmp =head;
for (temp = head; temp != null ; temp = temp.next){
if(tmp.data<temp.data){
tmp.data=temp.data;
}
//head = head.next;
}
System.out.println(tmp.data);
}
}
运行结果:
5
3
2
1
5
追问
万分感谢
追答
如果你对我的回答感到满意,请采纳我的回答。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询