您的位置:首页 > 其它

手写链表(二)-使用内部类实现添加、查询、定位、插入等功能

2014-02-01 23:08 501 查看
要求是这样的:

我们在示例(示例即本人博客手写链表(一))中给出的链表类是递归定义的。这样做的好处是链表的某个局部也是一个链表,这与链表在逻辑上的概念具有一致性。

但出于效率的考虑,经常需要引入一些辅助变量来加快操作的速度,这时,如果能给链表类型增加一个外壳就很方便后续的处理。

基本思路:

class MyList {

private Node head;

class Node {

int data;

Node next;

}

....

}

这种方案中,MyList不是递归定义的,而内部包含的Node类代表了链表的真实结构。MyList类是一个外壳类。它通过持有head 指针来记录一个链表的头在哪里。

请试着完善这种方案。

使用内部类实现添加、查询、定位、插入等功能

public class MyList<T> {
class Node{
public Node next;
public T data;
public Node(T data, Node next){
this.data=data;
this.next=next;
}
}

private Node head;
private Node tail;
private int size=0;

public MyList(){

}

public MyList(T headValue){
this.head=new Node(headValue, null);
this.tail=this.head;
size++;
}
public int length(){
return size;
}
public boolean empty(){
return size==0;
}

public void add(T element){
if(size==0){
this.head=new Node(element, null);
this.tail=this.head;
}
else{
Node next=new Node(element, null);
tail.next=next;
tail=next;
}
size++;
}
//头增加方式
public void addToHead(T element){
if(size==0){
this.head.data=element;
this.head.next=null;
this.tail=this.head;
}
else{
Node header=new Node(element, head);
head=header;
}
size++;
}
//插入
public void insert(T element , int index){
if(index<0 | index>size-1) throw new IndexOutOfBoundsException("越界");
else if(index==0) addToHead(element);
else if(size==0) add(element);
else{
Node previous=head;
for(int i=0;i<=index & previous!=null;i++,previous=previous.next){
if(i==index-1){
previous.next=new Node(element,previous.next);
size++;
break;
}
}
}
}
//搜索
public T search(int index){
if(index<0 | index>size-1) throw new IndexOutOfBoundsException("越界");
else{
Node current=head;
for(int i=0;i<=index & current!=null;i++,current=current.next){
if(i==index) return (T)current.data;
}
return null;
}
}
//元素定位
public int locate(T element){
Node current=head;
for(int i=0;i<size & current!=null;i++,current=current.next){
if(current.data.equals(element)) return i;
}
return -1;
}
//删除
public T delete(int index){
if(index<0 | index>size-1) throw new IndexOutOfBoundsException("越界");
else if(index==0) {
T value=head.data;
head=head.next;
size--;
return value;
}
else{
Node current=head;
for(int i=0;i<size & current!=null;i++,current=current.next){
if(i==index-1){
T value=current.next.data;
Node delNode=current.next;
current.next=delNode.next;
delNode.next=null;
size--;
return value;
}
}
return null;
}
}
public void remove(){
delete(size-1);
}
public void clear(){
head=null;
tail=null;
size=0;
}

public String toString(){
StringBuilder strB=new StringBuilder("[");
if(size==0) return "[]";
else{
for(Node current=head; current!=null;current=current.next){
strB.append(current.data.toString()+", ");
}
return strB.toString().substring(0,strB.length()-2)+"]";
}
}
//展示
public void show(){
System.out.println(this.toString());
}
}


测试主函数:

public class Test {
public static void main(String[] args) {
MyList<Object> a = new MyList<Object>();
a.add(10);
a.add(20);
a.add(30);
a.insert(15, 1);
a.addToHead(5);
a.show();
a.delete(1);//删除第二个元素
a.show();
System.out.println(a.search(2));//查询第三个元素
System.out.println(a.locate(20));//元素定位
}
}


结果:

[5, 10, 15, 20, 30]
[5, 15, 20, 30]
20
2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐