用java实现一个链表类_用java实现的一个链表

#ifndef _CIRCULAR_LINKLIST_H_#define _CIRCULAR_LINKLIST_H_#pragma once/************************************************************************//* 线性表链式存储结构实现 --单向循环链表 *//********************

随手写的一个用java实现的链表。package JavaHash;/** * 这个类定义了链表中应有的属性和获取方法 * @author zyn * */public class Node {

private Node left;

private Node right;

private Object obj;

private int flag;

public int getFlag() {

return flag; } public void setFlag(int flag) {

this.flag = flag; } public Node(Object obj){

this.obj=obj;

} public Node getLeft() {

return left; } public void setLeft(Node left) {

this.left = left; } public Node getRight() {

return right; } public void setRight(Node right) {

this.right = right; } public Object getObj() {

return obj; } public void setObj(Object obj) {

this.obj = obj; }

}package JavaHash;/** * 建一个双向链表 * @author zyn * 这个链表有一个致命性漏洞,当存入的数据是整形是,你搜索和删除只能用确定链表中位置的方式 * 因此此链表十分适合向其中加入不是整型的数据。 */public class JavaLinkList { private Node first;//建立头结点 private Node last; private Node move=null; /**

* 初始化链表

*/

public void creatLinkList(){

first=new Node("head");

last=new Node("last");

first.setRight(last);

first.setLeft(null);

last.setRight(null);

last.setLeft(first);

last.setFlag(1);

first.setFlag(0);

move=first;

}

/**

* 当建一个 JavaLinkList的对象时自动调用初始化方法

*/

public JavaLinkList(){

creatLinkList();

}

/**

* 检查链表中是否有相同的数据

* @param obj 数据

* @return

*/

public boolean check(Object obj){

boolean b=true;

int i=last.getFlag();

for(int t=0;t<=i;t++){

Node no=getLink(i);

if(no.getObj().equals(obj)){

b=false;

}

no=null;

}

return b;

}

/**

* 向链表中加入元素

* @param obj 要加入的元素

* @return 加入元素在链表中的位置

*/

public int add(Object obj){

if(check(obj)){

Node no=new Node(obj);

move.setRight(no);

no.setLeft(move);

no.setRight(last);

last.setLeft(no);

no.setFlag(move.getFlag()+1);

last.setFlag(move.getFlag()+2);

move=no;

no=null;

}

return move.getFlag();

}

/**

* 删除链表中对应标记的元素

* @param i

*/

public boolean delete(int i){

Node no=getLink(i);

Node aim=no;

boolean b=true;

for(int j=no.getFlag()+1;j<=last.getFlag();j++){

Node no2=getLink(j);

no2.setFlag(no2.getFlag()-1);

no2=null;

}

if(no.getLeft()!=null&&no.getRight()!=null){

no.getLeft(#ifndef _STATICK_LINCKLIST_H_#define _STATICK_LINCKLIST_H_#pragma once/************************************************************************//*

线性表链式存储结构实现 --静态链表

*//***************************).setRight(no.getRight());

no.getRight().setLeft(no.getLeft());

no.setLeft(null);

no.setRight(null);

no=null;

}else{

b=false;

no=null;

}

return b;

}

/**

* 根据所存元素删除链表节点

* @param obj 存的元素

*

*/

public boolean delete(Object obj){

Node no=getLink(obj);

Node aim=no;

boolean b=true;

int t=no.getFlag();

for(int i=t+1;i<=last.getFlag();i++){

Node no2=getLink(i);

no2.setFlag(no.getFlag()-1);

no2=null;

}

if(no.getLeft()!=null&&no.getRight()!=null){

no.getLeft().setRight(no.getRight());

no.getRight().setLeft(no.getLeft());

no.setLeft(null);

no.setRight(null);

no=null;

}else{

b=false;

}

return b;

}

/**

* 寻找链表中的元素

* @param i 链表位置标记

* @return 返回要寻找的链表对象

*/

public Node getLink(int i){

Node aim=new Node("mubiao");

if(i>0){

Node find=first.getRight();

if(find.getFlag()==i){

aim=find;

}else{

for(int j=2;j<=last.getFlag();j++){

Node no=find.getRight();

if(no.getFlag()==i){

aim=no;

break;

}else{

find=no;

}

no=null;

}

}

}else{

aim=first;

}

return aim;

}

/**

* 根据所村内容找

* @param obj 存的内容

* @return 返回节点对象

*/

public Node getLink(Object obj){

Node aim=null;

for(int i=0;i<=last.getFlag();i++){

Node no=getLink(i);

if(no.getObj()==obj){

aim=getLink(i);

}

no=null;

}

return aim;

}

/**

* 向链表中的指定位置加入新的节点。

* @param i节点位置

* @param obj 要加入的数据

*/

public boolean insert(int i,Object obj){

boolean b=true;

Node no=getLink(i);

if(no.getLeft()!=null){

for(int j=i+1;j<=last.getFlag();j++){

Node no2=getLink(j);

no2.setFlag(no2.getFlag()+1);

no2=null;

}

no.setFlag(no.getFlag()+1);

Node fresh=new Node(obj);

fresh.setLeft(no.getLeft());

fresh.setRight(no);

no.getLeft().setRight(fresh);

no.setLeft(fresh);

fresh.setFlag(i);

no=null;

fresh=null;

}else{

b=false;

no=null;

}

return b;

}

/**

*

* @return 得到链表中有多少元素

*/

public int length(){

return last.getFlag()+1;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值