第一步:定义一个List接口,规定一些基本操作
- package my.stack;
- public class Node<T> {
- private T data;
- private Node<T> next;
- public Node(){
- data = null;
- next = null;
- }
- public Node(T data){
- this.data = data;
- this.next = null;
- }
- public Node(T data, Node<T> 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<T> next){
- this.next = next;
- }
- public Node<T> getNext(){
- return this.next;
- }
- }
第二步:实现该接口定义的函数
1)由于是链表,首先要定义一个节点类(可以作为内部类实现)
- package my.list;
- public class Node<T> {
- private T data;
- private Node<T> next;
- public Node(){
- data = null;
- next = null;
- }
- public Node(T data){
- this.data = data;
- this.next = null;
- }
- public Node(T data, Node<T> 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<T> next){
- this.next = next;
- }
- public Node<T> getNext(){
- return this.next;
- }
- }
2)实现接口
- package my.list;
- public class MyLinkedList<T> implements MyList<T> {
- private Node<T> head;
- private Node<T> tail;
- private int size;
- public MyLinkedList(){
- this.head = null;
- this.tail = null;
- this.size = 0;
- }
- public MyLinkedList(T data){
- this.head = new Node<T>(data);
- this.tail = null;
- this.size = 0;
- }
- @Override
- //添加元素
- public void add(T element) {
- Node<T> node = new Node<T>(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<T> 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<T> 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<T> temp = point.getNext();
- // point.setData(temp.getData());
- // point.setNext(temp.getNext());
- // temp = null;
- }
- Node<T> 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<T> 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<String> node = new Node<String>("hello");
- // MyLinkedList<Node<String>> list = new MyLinkedList<Node<String>>();
- MyLinkedList<String> list = new MyLinkedList<String>();
- 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();
- }