链表的查找

xujiaxiang 2008-11-05 12:08:58

package ShuJuJieGou.Ch05;

/**
*
* 双端链表
*/
class Link4 {

public long dData;
public Link4 next;

public Link4(long d) {
dData = d;
}

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

class FirstLastList {

private Link4 first;
private Link4 last;

public FirstLastList() {
first = null;
last = null;
}

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

public void insertFirst(long dd) {
Link4 newlink = new Link4(dd);

if (isEmpty()) {
last = newlink;
}
newlink.next = first;
first = newlink;
}

//first 和 last 是同一个对象,每次插入后,last都指向最后一个值
public void insertlast(long dd) {
Link4 newlink = new Link4(dd);
if (isEmpty()) {
first = newlink;
} else {
last.next = newlink;
}
last = newlink;
}

public long deleteFirst() {
long temp = first.dData;
if (first.next == null) {
last = null;
}
first = first.next;
return temp;
}

//查找方法
public Link4 find(long d) {
Link4 temp = first;
while (first.dData != d) {
if (temp == null) {
return null;
}else{
temp = temp.next;

}
}
return temp;
}

//删除
public Link4 delete(long d) {
Link4 persous = first;
Link4 current = first;
while (current.dData != d) {

if (current == null) {
return null;
} else {
persous = current;
current = current.next;
}
}
if (current == first) {
first = first.next;
} else {
persous.next = current.next;
}
return current;
}

public void displayList() {
System.out.print("List(first-->last):");
Link4 current = first;
while (current != null) {
current.displayLink();
current = current.next;
}
System.out.println("");
}
}

public class Test4 {

public static void main(String[] args) {
FirstLastList thelist = new FirstLastList();

thelist.insertFirst(22);
thelist.insertFirst(44);
thelist.insertFirst(66);
thelist.insertFirst(88);

thelist.insertlast(11);
thelist.insertlast(33);
thelist.insertlast(55);

thelist.displayList();

thelist.deleteFirst();
thelist.deleteFirst();
thelist.displayList();


//测试删除
thelist.delete(11);
thelist.displayList();

//测试查找 错误在这里,我在一个单链表使用这个查找功能正常使用,而在双端链表却不行
Link4 temp=thelist.find(22);
temp.displayLink();
}
}

...全文
112 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
shuaiAWP 2008-11-05
  • 打赏
  • 举报
回复
查找方法跟main函数的错误差不多,都需要判断temp是否为null
在main函数最后要判断一下temp要是为null的话就不能引用temp.dData,否则会引发异常
shuaiAWP 2008-11-05
  • 打赏
  • 举报
回复

class Link4 {

public long dData;
public Link4 next;

public Link4(long d) {
dData = d;
}

public void displayLink() {

System.out.print(dData + " ");
}
}

class FirstLastList {

private Link4 first;
private Link4 last;

public FirstLastList() {
first = null;
last = null;
}

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

public void insertFirst(long dd) {
Link4 newlink = new Link4(dd);

if (isEmpty()) {
last = newlink;
}
newlink.next = first;
first = newlink;
}

//first 和 last 是同一个对象,每次插入后,last都指向最后一个值
public void insertlast(long dd) {
Link4 newlink = new Link4(dd);
if (isEmpty()) {
first = newlink;
} else {
last.next = newlink;
}
last = newlink;
}

public long deleteFirst() {
long temp = first.dData;
if (first.next == null) {
last = null;
}
first = first.next;
return temp;
}

//查找方法
public Link4 find(long d) {
Link4 temp = first;
while (temp.dData != d) {
if (temp == null) {
return null;
}else{
temp = temp.next;
if(temp == null) {//这样改就OK了
return null;
}

}
}
return temp;
}

//删除
public Link4 delete(long d) {
Link4 persous = first;
Link4 current = first;
while (current.dData != d) {

if (current == null) {
return null;
} else {
persous = current;
current = current.next;
}
}
if (current == first) {
first = first.next;
} else {
persous.next = current.next;
}
return current;
}

public void displayList() {
System.out.print("List(first-->last):");
Link4 current = first;

while (current != null) {
current.displayLink();
current = current.next;
}
System.out.println("");
}
}

public class Test4 {

public static void main(String[] args) {
FirstLastList thelist = new FirstLastList();

thelist.insertFirst(22);
thelist.insertFirst(44);
thelist.insertFirst(66);
thelist.insertFirst(88);

thelist.insertlast(11);
thelist.insertlast(33);
thelist.insertlast(55);

thelist.displayList();

thelist.deleteFirst();
thelist.deleteFirst();
thelist.displayList();


//测试删除
thelist.delete(11);
thelist.displayList();

//测试查找 错误在这里,我在一个单链表使用这个查找功能正常使用,而在双端链表却不行
Link4 temp=thelist.find(66);
if(temp == null) {
System.out.println("没有这个数!");
}else{
temp.displayLink();
}
}
}


KOOK_OKKO 2008-11-05
  • 打赏
  • 举报
回复

public Link4 find(long d) {
Link4 temp = first;
while (first.dData != d) {
if (temp == null) {
return null;
}else{
temp = temp.next;//这里temp.next是不是为null

}
}
return temp;
}


62,623

社区成员

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

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