链表的实现(Java语言描述)

代码如下:

public interface ListInterface<T> {
	public T getElem(int i);
	public boolean insertElem(int i,T t);
	public T deleteElem(int i);
	

}


 

public class Node<T> {
	public T data;
	public Node<T> next;
	public T getData(){
		return data;
	}
	public void setData(T data){
		this.data = data;
	}
	public Node<T> getNext(){
		return next;
	}
	public void setNext(Node<T> next){
		this.next = next;
	}

}


 

import java.io.*;
import java.util.*;

public class Linklist<T> implements ListInterface<T> {
	public Node<T> head;
	public int length;
	public Linklist(T[] at){//头插法建立单链表。
		T t;
		this.length = 11;
		Node<T> p;
		head = new Node<T>();
		//head.next = null;
		//Scanner scanner = new Scanner(System.in);
		//T s = scanner.nextLine();
		int i=0;
		while(i<at.length){
			t = at[i];
			p = new Node<T>();
			p.data = t;
			p.next = head.next;
			head.next = p;//头插法!
			i++;
		}
	}
	public Linklist(Node<T> head){
		this.head = head;
	}
	public T getElem(int i){
		int j = 0;
		Node<T> n = head;
		while(n != null){
			if(j == i){
				return n.getData();
			}
			n = n.getNext();
			j++;
		}
		return null;
	}
	public boolean insertElem(int i,T t){
		if(i<0 || i>length){
			System.out.println("插入位置不合法!");
			return false;
		}else{
			if(head==null && i==1){
				head = new Node<T>();
				head.setData(t);
				length++;
				return true;
			}
			else if(head!=null && i==1){
				Node<T> tempNode = new Node<T>();
				tempNode.setData(t);
				tempNode.setNext(head);
				head = tempNode;
				length++;
				return true;
			}else{
				Node<T> n = this.head;
				int j = 1;
				while(n!=null && j<i-1){
					n = n.getNext();
					j++;
				}
				Node<T> tempNode = new Node<T>();
				tempNode.setData(t);
				tempNode.setNext(n.getNext());
				n.setNext(tempNode);
				length++;
			}
			return true;
			
		}
	}
	public T deleteElem(int i){
		if(head==null || i<1 || i>length){
			System.out.println("删除位置不合法!");
			return null;
		}
		T old;
		if(head!=null && i==1){
			old = head.getData();
			head = head.getNext();
		}else{
			Node<T> n = this.head;
			int j = 1;
			while(n!=null && j<i-1){
				n = n.getNext();
				j++;
			}
			old = n.getNext().getData();
			n.setNext(n.getNext().getNext());
		}
		length--;
		return old;
	}
	public Node<T> getHead(){
		return this.head;
	}
	public int getLength(){
		return this.length;
	}
	public void setHead(Node<T> head){
		this.head = head;
	}
	public void setLength(int length){
		this.length = length;
	}
	public void display(){
		Node<T> p;
		int i = 0;
		p = head.next;
		while(p!=null){
			System.out.print(p.data + " ");
			p = p.next;
		}
	} 
}


 

import java.util.*;
import java.io.*;

public class LinkedlistTest {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Integer[] arr = new Integer[10];
		Scanner scan = new Scanner(System.in);
		for(int i=0; i<10; i++){
			arr[i] = scan.nextInt();
		} 
		Linklist<Integer> list = new Linklist<Integer>(arr);
		list.display();
		System.out.println();
		System.out.println(list.getElem(5));
		System.out.println(list.insertElem(5, 78));
		list.display();
		System.out.println();
		System.out.println("要删除的元素是:"+list.deleteElem(5));
		list.display();
		
		
        		

	}

}



 

原文地址:https://www.cnblogs.com/wxisme/p/4363786.html