原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://dba10g.blog.51cto.com/764602/229133
package link;
public class LinkList {
private Link first;
public void LinkList(){
first = null;
}
public boolean isEmpty(){
return first == null;
}
public void insertFirst( int id, double dd){
Link newLink = new Link(id,dd);
newLink.next = first;
first = newLink;
/*
* first = newLink; 这样会陷入死循环
* newLink.next = first;
*/
}
public Link deleteFirst(){
Link temp = first;
first = first.next;
return temp;
}
public void displayList(){
Link current = first;
while(current != null){
current.displayLink();
current =current.next;
}
}
public Link find2( int key){
Link current = first;
while(current.next != null){
if(current.iData == key)
return current;
else
current = current.next;
}
if(current.iData == key){
return current;
}
return null;
}
public Link find( int key){
Link current = first;
while(current.iData != key){
if(current.next == null)
return null;
else{
current = current.next;
}
}
return current;
}
public Link delete( int key){
Link current = first;
Link previous = first;
while(current.iData != key){
//最后,没有找到匹配的数据,返回
if(current.next == null){
return null;
}
else{
//依次对链接点进行遍历,并记录前一个链接点
previous = current;
current =current.next;
}
}
if(current == first){
first = first.next;
}
else{
previous.next =current.next;
}
return current;
}
public static void main(String args[]){
LinkList list = new LinkList();
list.insertFirst(11, 1.88);
list.insertFirst(33, 3.88);
list.insertFirst(44, 4.88);
list.insertFirst(55, 5.88);
list.insertFirst(66, 6.88);
list.insertFirst(77, 7.88);
list.delete(6).displayLink();
//list.find(66).displayLink();
//list.displayList();
}
}
public class LinkList {
private Link first;
public void LinkList(){
first = null;
}
public boolean isEmpty(){
return first == null;
}
public void insertFirst( int id, double dd){
Link newLink = new Link(id,dd);
newLink.next = first;
first = newLink;
/*
* first = newLink; 这样会陷入死循环
* newLink.next = first;
*/
}
public Link deleteFirst(){
Link temp = first;
first = first.next;
return temp;
}
public void displayList(){
Link current = first;
while(current != null){
current.displayLink();
current =current.next;
}
}
public Link find2( int key){
Link current = first;
while(current.next != null){
if(current.iData == key)
return current;
else
current = current.next;
}
if(current.iData == key){
return current;
}
return null;
}
public Link find( int key){
Link current = first;
while(current.iData != key){
if(current.next == null)
return null;
else{
current = current.next;
}
}
return current;
}
public Link delete( int key){
Link current = first;
Link previous = first;
while(current.iData != key){
//最后,没有找到匹配的数据,返回
if(current.next == null){
return null;
}
else{
//依次对链接点进行遍历,并记录前一个链接点
previous = current;
current =current.next;
}
}
if(current == first){
first = first.next;
}
else{
previous.next =current.next;
}
return current;
}
public static void main(String args[]){
LinkList list = new LinkList();
list.insertFirst(11, 1.88);
list.insertFirst(33, 3.88);
list.insertFirst(44, 4.88);
list.insertFirst(55, 5.88);
list.insertFirst(66, 6.88);
list.insertFirst(77, 7.88);
list.delete(6).displayLink();
//list.find(66).displayLink();
//list.displayList();
}
}
本文出自 “简单” 博客,请务必保留此出处http://dba10g.blog.51cto.com/764602/229133