1.定义接口(内含内部节点类)
package com.jimmy;
public interface ListInterface<T>{
publicboolean add(Object entry);
publicboolean add(int pos,Object entry);
publicboolean remove(int pos);
publicvoid clear();
publicboolean repalce(int pos,Object entry);
public Object getEntry(int pos);
publicboolean contains(Object entry);
publicint getLength();
publicboolean isEmpty();
publicvoid display();
publicclass MNode<T> {
private Tdata;
public T getData() {
returndata;
}
publicvoid setData(T data) {
this.data = data;
}
public MNode<T>next;
public MNode(){
this.data=null;
next=null;
}
public MNode(T data){
this.data=data;
next=null;
}
public MNode(T data,MNode<T> next){
this.data=data;
this.next=next;
}
}
}
2.具体实现类(链表实现)
//first为哨兵,要初始化,不参与保存数据
//getEntry(pos),从一开始找
package com.jimmy.impl;
import com.jimmy.ListInterface;
public class MyLinkedList<T>implements ListInterface<T> {
private MNode<T>first;
privateintlength;
public MyLinkedList(){
clear();
first=new MNode<T>(); //first为哨兵,要初始化
}
//默认放到last后头
publicboolean add(Object entry) {
MNode<T> newNode=new MNode(entry);
if(isEmpty()){
first.next=newNode;
}
else{
MNode<T> lastNode=getNodeAt(length);
lastNode.next=newNode;//放到last后头
}
length++;
returntrue;
}
publicboolean add(int pos, Object entry) {
}
publicboolean remove(int pos) {
MNode pre = null;
MNode<T> cur=first;//first为哨兵
if(pos>=1&&pos<=length){
for(int i=0;i<pos;i++){ //从一开始
pre=cur;
cur=cur.next;
}
}
pre.next=cur.next;
cur=null;
length--;
returntrue;
}
publicvoid clear() {
first=null;
MNode<T> pre = null;
MNode<T> cur=first;//first为哨兵
for(int i=0;i<length;i++){//从一开始
pre=cur;
cur=cur.next;
pre=null;
}
length=0;
}
publicboolean repalce(int pos, Object entry) {
MNode<T> pre = null;
MNode<T> cur=first;//first为哨兵
for(int i=0;i<pos;i++){ //从一开始
pre=cur;
cur=cur.next;
}
MNode<T> tmp=new MNode(entry);
pre.next=tmp;
tmp.next=cur.next;
cur=null;
returntrue;
}
public Object getEntry(int pos) {
MNode<T> cur=first;//first为哨兵
if(pos>=1&&pos<=length){
for(int i=0;i<pos;i++){ //从一开始
cur=cur.next;
}
}
return cur.getData();
}
publicboolean contains(Object entry) {
MNode<T> cur=first;//first为哨兵
for(int i=0;i<length;i++){//从一开始
cur=cur.next;
if(cur.getData().equals(entry));
{
System.out.print("list contains "+entry);
returntrue;
}
}
returnfalse;
}
publicint getLength() {
returnlength;
}
publicboolean isEmpty() {
if(length==0)
{assert(first==null);
returntrue;}
else
returnfalse;
}
publicvoid display() {
MNode<T> cur=first;//first为哨兵
for(int i=0;i<length;i++){//从一开始
cur=cur.next;
//if(cur!=null)
System.out.print(cur.getData()+",");
}
System.out.println();
}
//外部输入pos,pos从1开始
public MNode<T> getNodeAt(int pos)
{
MNode<T> cur=first;//first为哨兵
if(pos>=1&&pos<=length){
for(int i=0;i<pos;i++){
cur=cur.next;
}
}
return cur;
}
publicstaticvoid main(String as[]){
MyLinkedList<Integer> ll=new MyLinkedList<Integer>();
ll.add(1);
//List l=new LinkedList();
ll.add(5);
ll.add(3);
ll.display(); //1,5,3
ll.repalce(2,new Integer(9)); //1,9,3
ll.display();
Integer a=(Integer)ll.getEntry(2); //注意这里getEntry()查找二的显示9,即符合中文习惯,第一个为1
System.out.println("a="+a);
System.out.println("length="+ll.length);
System.out.println(ll.contains(new Integer(9)));
}
}
测试显示:
1,5,3,
1,9,3,
a=9
length=3
list contains 9true