这是一个小练习,在代码中描述了类得作用,求指点有什么不足的地方,啥地方需要优化?package com.wwy.thirdTest;import org.omg.CosNaming.NamingContextPackage.NotEmpty;/**
 * 一个单项链表使用一个头结点,无尾节点,保留头结点的引用
 * a、返回链表的大小
 * b、打印链表的方法
 * c、测试值x是否含于链表的方法
 * d、如果值x没有,则添加到该链表的方法
 * e、如果值x含于链表则将x从该链表中删除的方法
 * @author wWX161568
 *
 */
public class Test3_11 
{

private transient int size = 0;
private Node head = new Node();

public Test3_11()
{
head.next = head;
}

public Test3_11(String data)
{
head.data = data;
head.next = head;
} /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
/*Test3_11 t = new Test3_11();
 t.create(3);
System.out.println(head.next.next.next.data);
System.out.println(t.size());*/ }

//创建节点为n的链表
public Node create(int n)
{
Node temp = head;
for(int i = 0;i < n-1;i++)
{
temp.next = new Node(i+1 + "");
temp = temp.next;
}
size += n;
return head;
}

/**
 * 打印链表
 */
public void print()
{
Node temp = head;
//记录打印的个数,并控制结束循环
int i = 0;
do
{
System.out.print(temp.data);
if(i < size - 1)
{
System.out.print(" --> ");
}
//遍历给下一个节点
temp = temp.next;
i++;
}while(i < size);
System.out.println();
}

/**
 * 判断是否包含值o
 * @param o 要判断的值
 * @return 链表中存在值o则返回true,否则返回false
 */
public boolean contains(String o)
{
if(o.equals(null) || o ==null)
{
try {
throw new NotEmpty();
} catch (NotEmpty e) {
e.printStackTrace();
}
return false;
}
else
{
Node temp = head;
int i = 0;
do
{
if(o.equals(temp.data))
return true;
i++;
temp = temp.next;
}while(i < size);
return false;
}
}

/**
 * 判断链表中是否含有值为newVal的节点,有就删除,没有就添加到尾节点
 * @param newVal
 */
public void containsAddRemove(String newVal)
{
boolean flag = this.contains(newVal);
if(flag)
{
//不存在,调用add方法添加新节点
this.add(newVal);
}
else
{
//存在,调用remove方法删除值为newVal的节点
this.remove(newVal);
}
}

/**
 * 删除值为newVal的节点
 * @param newVal
 */
public  void remove(String newVal) {
Node temp = head;

//循环查找值为newVal的节点
for(int i = 0;i < size;i++)
{
String data = temp.data;
if(newVal.equals(data))
   break;
temp = temp.next;
}
System.out.println(temp.data);
//找到值为newVal的节点temp,继续查找该节点的前一个节点
Node courrent = temp;

//判断是不是尾节点
if(temp.next == null)
{
courrent = this.getTailPrevious();
//断开链接,
courrent.next = null;
size--;
}
else
{
//不是尾节点,查找temp(要删除的节点)的前一个节点
//System.out.println(432);
//怎么查???????????、
Node prev = head;
//找到的prev就是要删除节点的前一个节点
for(int i = 0; i < size;i++)
{
if(prev == temp)
{
prev = this.getNode(i - 1);
break;
}
prev = prev.next;
}

courrent = prev;
//断开链接,也就是把找到的前一个节点的后一个节点指向为要删除节点的下一个节点
courrent.next = temp.next;
//并断开要删除节点与后一个节点的链接
temp.next = null;
size--;
}
}

private Node getTailPrevious()
{
Node temp = head;
int i = 0;
//循环找到当前链表的尾节点
do
{
   Node current = temp;
   temp = temp.next;
   i++;
   if(current == null)
   {
   break;
   }
}while(i < size-2);
return temp;
} /**
 * 在尾节点插入值为newVal的节点
 * @param newVal
 */
public  void add(String newVal) {
Node temp = this.getTailPrevious().next;
//创建值为newVal的节点
Node insert = new Node(newVal);
//将新创建的节点链接到尾部
temp.next = insert;
//链表长度加1
size++;
} /**
 * 查找指定位置的节点
 * @param index
 * @return
 */
public Node getNode(int index){
//从头结点开始遍历
Node temp = head;

for(int i = 0;i < size;i++)
{
if(i == index){
return temp;
}
temp = temp.next;
}
if(temp == null)
throw new NullPointerException();
return null;
}

/**
 * 返回链表的大小
 * @return
 */
public int size()
{
return size;
} /**
 * 节点类
 * @author wWX161568
 *
 */
public static class Node
{
Node next;
String data;

public Node()
{
}
public Node(String data)
{
this.data = data;
}
}}下面是一个测试类
package com.wwy.thirdTest;public class Main 
{ /**
 * @param args
 */
public static void main(String[] args) 
{
/*Test3_3MyLinkedList<Integer> t = new Test3_3MyLinkedList<Integer>();
t.add(1);
t.add(2);
t.add(3);LinkedList
System.out.println(t.contains(0));*/

Test3_11 t =  new Test3_11("head");
t.create(3);
System.out.println(t.size());
t.print();

boolean con = t.contains("2");
System.out.println(con);

t.add("3");
t.print();
System.out.println(t.size());

        System.out.println("index " + t.getNode(1).data);

t.remove("2");
t.print();
System.out.println(t.size());
}}
优化链表

解决方案 »

  1.   

    1.加个变量保存尾节点 getTailPrevious() 不要了
    2.public Test3_11(String data) 
        { 
            head.data = data; 
            head.next = head; 
    size=1;//少了这个
        } 
    3.看print() ,这是头节点保存数据的链表,但是 public Node create(int n)中
    头节点没有数据的,数据是从head.next开始的
    4.public boolean contains(String o) 这里,整个错了
    add的方法可以添加null的数据,if(o.equals(null) || o ==null)判断错了
    下面的do while 改成 while,do while,size=0也会执行一次
    if(o.equals(temp.data)) 改成
    if(o==temp.data||(o!=null&&o.equals(temp.data)))
    o==null的情况 || o!=null
    5.public  void remove(String newVal) {
    if(newVal.equals(data)) //也是有null的情况
    if(newVal==data||(newVAl!=nullnewVal.equals(data)))
    6.下一句,循环外加个变量boolean
    天知道是找到相同值
       break;
    还是for结束后都没有找到,或者size==0
    7.//判断是不是尾节点
             if(temp.next == null)
    这里,我想问下,这是不是循环链表,
    下面的,除了add方法,别的都错了