java链表list_java自定义List链表
第一步:定義一個List接口,規定一些基本操作
package my.stack;
public class Node {
private T data;
private Node next;
public Node(){
data = null;
next = null;
}
public Node(T data){
this.data = data;
this.next = null;
}
public Node(T data, Node next){
this.data = data;
this.next = next;
}
public void setData(T data){
this.data = data;
}
public T getData(){
return this.data;
}
public void setNext(Node next){
this.next = next;
}
public Node getNext(){
return this.next;
}
}
第二步:實現該接口定義的函數
1)由于是鏈表,首先要定義一個節點類(可以作為內部類實現)
package my.list;
public class Node {
private T data;
private Node next;
public Node(){
data = null;
next = null;
}
public Node(T data){
this.data = data;
this.next = null;
}
public Node(T data, Node next){
this.data = data;
this.next = next;
}
public void setData(T data){
this.data = data;
}
public T getData(){
return this.data;
}
public void setNext(Node next){
this.next = next;
}
public Node getNext(){
return this.next;
}
}
2)實現接口
package my.list;
public class MyLinkedList implements MyList {
private Node head;
private Node tail;
private int size;
public MyLinkedList(){
this.head = null;
this.tail = null;
this.size = 0;
}
public MyLinkedList(T data){
this.head = new Node(data);
this.tail = null;
this.size = 0;
}
@Override
//添加元素
public void add(T element) {
Node node = new Node(element);
if(this.head == null){
this.head = node;
this.tail = node;
}else{
this.tail.setNext(node);
this.tail = node;
}
this.size++;
}
@Override
//清空鏈表
public void clear() {
this.head = null;
this.tail = null;
System.gc();
}
@Override
//刪除鏈表最后一個節點
public int delete() {
Node point = head;
while(point.getNext() != this.tail){
point = point.getNext();
}
tail.setNext(null);
tail = point;
size--;
return 0;
}
@Override
public boolean delete(int location) {
if(location > size-1){
System.out.println("out of range");
}else{
Node point = head;
int count = 1;
while(point.getNext() != this.tail){
point = point.getNext();
count++;
if(count == location){
System.out.println("finl this element:"+point.getData());
}
break;
//Node temp = point.getNext();
//point.setData(temp.getData());
//point.setNext(temp.getNext());
//temp = null;
}
Node temp = point.getNext();
point.setData(temp.getData());
point.setNext(temp.getNext());
temp = null;
return true;
}
return false;
}
@Override
//查找鏈表中是否包含某元素
public boolean find(T element) {
// TODO Auto-generated method stub
Node point = head;
while(point.getNext() != null){
if(point.getData().equals(element)){
return true;
}
point = point.getNext();
}
return false;
}
@Override
public int size() {
return this.size;
}
}
第三部:編寫客戶端進行測試
此處暫未實現使用location位置進行查詢,鏈表隨即查詢效率較低
package my.list;
public class MyLinkedListClient {
public static void main(String[] args){
//System.out.println("hello world");
//Node node = new Node("hello");
//MyLinkedList> list = new MyLinkedList>();
MyLinkedList list = new MyLinkedList();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
//list.delete();
System.out.println(list.size());
//System.out.println(list.delete(2));
//list.clear();
//list.clear();
}
}
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的java链表list_java自定义List链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab编程实现levinson算法
- 下一篇: php怎么循环输出二维数组,PHP中遍历