java数据结构之List

 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();

}

//外部输入pospos从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

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值