想自己实现一个vector
其中remove()如何实现?public class MyVector { Node head;

class Node {
String data;
Node next;

public Node(String data) {
this.data = data;
}
}


public static void main(String[] args) {

MyVector mv = new MyVector();

mv.add("1");
mv.add("2");
mv.add("3");
mv.add("8");

for(int i =0;i<mv.size();i++) {
System.out.println((String)mv.get(i));
}
mv.remove(1);

for(int i =0;i<mv.size();i++) {
System.out.println((String)mv.get(i));
} }

public void remove(int index) {

.....................
.....................
}

public void add(String s ) {
Node node = new Node(s);

Node tail = getTail();

if(tail==null) {
head = node;
}else {
tail.next = node;
node.next = null;
}
}

public Node getTail() {

if(head==null) return null;

Node p = head;
while(true) {
if(p.next==null) break;
p = p.next;

return p; 
}

public int size() {
int n = 0;
Node node = head;
while(true) {
if(node==null) break;
node = node.next;
n++;
}
return n;
}

public Object get(int n) {
Node node = head;
for(int i=0;i<n;i++) {
node = node.next;
}
return node.data;
}}

解决方案 »

  1.   

    答:参考代码:public void remove(int index) 
    { if(head==null || index<0 ) return; //合法数据检查Node f=null,w=head;
    for(int i=1;i<=index && w!=null ;i++) //找index对应的结点
    {
      f=w; 
      w=w.next; 
    }if(f==null)//要删除第一个结点
    {
     head=head.next;
    }
    else  if(w!=null)//要删除w结点,前驱是f.若w==null,表示index太大,没有对应的结点.
          {
           f.next=w.next;
          }
      

  2.   

      lao xiong zen me cong lai bu jie tie????