手写链表(二)-使用内部类实现添加、查询、定位、插入等功能
2014-02-01 23:08
501 查看
要求是这样的:
我们在示例(示例即本人博客手写链表(一))中给出的链表类是递归定义的。这样做的好处是链表的某个局部也是一个链表,这与链表在逻辑上的概念具有一致性。
但出于效率的考虑,经常需要引入一些辅助变量来加快操作的速度,这时,如果能给链表类型增加一个外壳就很方便后续的处理。
基本思路:
class MyList {
private Node head;
class Node {
int data;
Node next;
}
....
}
这种方案中,MyList不是递归定义的,而内部包含的Node类代表了链表的真实结构。MyList类是一个外壳类。它通过持有head 指针来记录一个链表的头在哪里。
请试着完善这种方案。
使用内部类实现添加、查询、定位、插入等功能
测试主函数:
结果:
我们在示例(示例即本人博客手写链表(一))中给出的链表类是递归定义的。这样做的好处是链表的某个局部也是一个链表,这与链表在逻辑上的概念具有一致性。
但出于效率的考虑,经常需要引入一些辅助变量来加快操作的速度,这时,如果能给链表类型增加一个外壳就很方便后续的处理。
基本思路:
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
相关文章推荐
- 安卓开发使用百度地图sdk实现定位,添加marker,城市切换等功能
- jsp操作MySQL实现查询/插入/删除功能示例
- 使用JQuery实现全选、反选、添加和删除功能
- Vue中使用Froala Editor时,外部方法调用html.insert()插入内容到光标最后停留位置功能实现
- PHP+MySQL使用mysql_num_rows实现模糊查询图书信息功能
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 使用ADF-BC 实现查询功能之三:如何在页面装载时自动执行查询?
- 用线性表编写通讯录管理系统 实现添加,查询,修改,删除功能
- Java使用Arrays、ArrayList、LinkedList、Vector实现插入查询性能分析
- 使用Oracle函数实现按汉字拼音首字母查询功能
- Android开发,MapBox地图详细介绍、使用、部分功能实现(初始化、标记、定位、styleurl)
- 使用jQuery实现实时模糊查询功能
- 数据结构双向循环链表的C语言实现(插入,查询,删除)
- Vue中使用Froala Editor时,外部方法调用html.insert()插入内容到光标最后停留位置功能实现
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 使用ADF-BC 实现查询功能之一:All Queriable Attributes
- 使用alterMIME实现添加message footer功能
- 移动web端使用腾讯地图实现定位功能
- drupal7 view添加查询条件又一方法效率不知道怎么样,但至少功能能实现!
- OpenLayers学习笔记6——使用jQuery UI实现查询并标注(功能实现篇)