拿java写了一个有点像指针的单链表

public class LinkList {
 private Node firstNode;
 private Integer position;

public LinkList() {
  super();
 }

public LinkList(Node firstNode) {
  super();
  this.firstNode = firstNode;
 }

public Node getFirstNode() {
  return firstNode;
 }

public void setFirstNode(Node firstNode) {
  this.firstNode = firstNode;
 }

public Integer getPosition() {
  return position;
 }

public void setPosition(Integer position) {
  this.position = position;
 }

/**
  * 无位置参数:在链表的末尾添加
  *
  * @param node
  * @return
  */
 public LinkList add(Node node) {
  // 从头结点开始,依次找下去知道为空为止
  Node currentNode = this.firstNode;
  if (firstNode.getNextNode() == null) {
   node.setPriorNode(firstNode);
   firstNode.setNextNode(node);
   return this;
  }
  while (currentNode.getNextNode() != null) {
   currentNode = currentNode.getNextNode();
  }
  currentNode.setNextNode(node);
  currentNode.getPriorNode().setNextNode(currentNode);
  node.setPriorNode(currentNode);
  return this;
 }

/**
  * 在指定位置添加节点
  *
  * @param node
  * @param position
  * @return
  */
 public boolean add(Node node, int position) {
  int count = 0;
  Node currentNode = this.firstNode;
  if (position == 1) {
   if (firstNode.getNextNode() == null) {
    node.setPriorNode(firstNode);
    firstNode.setNextNode(node);
    return true;
   }
   node.setNextNode(firstNode.getNextNode());
   node.setPriorNode(firstNode);
   firstNode.setNextNode(node);
   return true;
  }
  while (count < position) {
   if (currentNode.getNextNode() == null) {
    if (count == position - 1) {
     add(node);
     return true;
    }
    return false;
   }
   currentNode = currentNode.getNextNode();
   count++;
  }
  currentNode.getPriorNode().setNextNode(node);
  node.setPriorNode(currentNode.getPriorNode());
  node.setNextNode(currentNode);
  currentNode.setPriorNode(node);
  node.setPriorNode(currentNode);
  return true;
 }

/**
  * 移除指定位置的node
  *
  * @param position
  * @return
  */
 public boolean remove(int position) {
  Node canditate = find(position);
  if (canditate != null && canditate.getNextNode() != null) {
   canditate.getPriorNode().setNextNode(canditate.getNextNode());
   canditate.getNextNode().setPriorNode(canditate.getPriorNode());
  } else if (canditate != null && canditate.getNextNode() == null) {
   canditate.getPriorNode().setNextNode(null);
  } else {
   return false;
  }

return true;
 }

/**
  * 根据指定位置查找node
  *
  * @param position
  * @return
  */
 public Node find(int position) {
  int count = 0;
  Node node = this.firstNode;
  while (count < position) {
   if (node.getNextNode() != null) {
    node = node.getNextNode();
   }
   if (node.getNextNode() == null && count < position - 1) {
    return null;
   }
   count++;
  }
  return node;
 }

/**
  * 根据位置更新node信息
  *
  * @param newvalue
  * @param position
  * @return
  */
 public boolean update(String newvalue, int position) {
  Node canditate = find(position);
  if (canditate != null) {
   canditate.setNodevalue(newvalue);
  } else {
   return false;
  }

return true;
 }
}

package projectlinklist;

/**
 *实体类
 *nodevalue为节点的值
 *nextNode为下一个节点
 */
public class Node {
  private Node priorNode;
  public Node getPriorNode() {
   return priorNode;
  }
  public void setPriorNode(Node priorNode) {
   this.priorNode = priorNode;
  }
  private String nodevalue;
  private Node nextNode;
  public String getNodevalue() {
   return nodevalue;
  }
  public Node(String nodevalue) {
   super();
   this.nodevalue = nodevalue;
  }
  public Node() {
   super();
  }
  public void setNodevalue(String nodevalue) {
   this.nodevalue = nodevalue;
  }
  public Node getNextNode() {
   return nextNode;
  }
  public void setNextNode(Node nextNode) {
   this.nextNode = nextNode;
  }
}

时间: 2024-07-30 13:45:55

拿java写了一个有点像指针的单链表的相关文章

java写的一个简单学生管理系统[改进]

用Java写的一个简单学生管理系统 import java.util.*; public class student_cj {  public static void main(String[] args){      Scanner in=new Scanner(System.in);   System.out.print("请输入学生人数:");   int num=in.nextInt();//学生人数   String[] str=new String[num];//结合一行数

【LeetCode-面试算法经典-Java实现】【138-Copy List with Random Pointer(拷贝有随机指针的单链表)】

[138-Copy List with Random Pointer(拷贝有随机指针的单链表)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list.

【Java】 大话数据结构(2) 线性表之单链表

本文根据<大话数据结构>一书,实现了Java版的单链表. 书中的线性表抽象数据类型定义如下(第45页): 实现程序: package LinkList; /** * 说明: * 1.<大话数据结构>中没有线性表的长度,但提到可以存储于头节点的数据域中. * 本程序的线性表长度存放于count变量中,线性表长度可以使程序比较方便. * 2.程序中,第i个位置代表第i个结点,头结点属于第0个结点 * 3.因为链表为泛型,整表创建采用整型(随机整数做元素),所以有出现一些类型转换 * 4

【LeetCode-面试算法经典-Java实现】【061-Rotate List(旋转单链表)】

[061-Rotate List(旋转单链表)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 题目大

【LeetCode-面试算法经典-Java实现】【086-Partition List(将单链表进行分区)】

[086-Partition List(将单链表进行分区)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the

用JAVA写查询一个字符串中是否包含另外一个字符串以及出现的次数

package JAVA; import java.awt.List;import java.util.ArrayList;/** *  * @author 梁小鱼 * */public class MyTest { public static void main(String[] args) {  //查找字符串在目标字符串是否存在  Boolean isExit = IsExit("f","abfsdfsdkjl;fas;dlfsldf;asdfsdfaszdf"

Java写的一个计算器模拟小程序

下个周六又要参加自考实践上机考试了,时间过的好快,天冷了人也变懒惰了,有时候什么也不想干,今晚刚好有时间就抽空把JAVA的试题拿出来再复习复习,看书比较困乏索性就敲敲代码吧,说实话我对JAVA不是很熟,如果不是因为考试要考,我也没时间接触它,毕竟做运维的,我更喜欢shell,PYTHON之类的.算了,还是把刚敲的代码放这里保存下,省的以后又找不到了.刚入门也就这样了. 题目: 编写一个计算器模拟程序.界面采用4行3列布局,界面设有3个文字标签(运算数1.运算数2.计算结果).3个文本框和3个加.

JAVA实现具有迭代器的线性表(单链表)

一,迭代器的基本知识: 1,为什么要用迭代器?(迭代:即对每一个元素进行一次“问候”) 比如说,我们定义了一个ADT(抽象数据类型),作为ADT的一种实现,如单链表.而单链表的基本操作中,大部分需要用到依次遍历单链表中的每一个元素.一般而言,我们就是用for循环来实现遍历,这样,当你新增一个对单链表的操作并需要使用遍历时,你就得重新写一个for循环而实现遍历.那么,为什么不将迭代(遍历)作为一种基本的ADT操作(基本的ADT操作如:新增一个元素.删除一个元素)呢?于是,迭代器就出场了. 2,鉴于

Java数据结构-线性表之单链表LinkedList

线性表的链式存储结构,也称之为链式表,链表:链表的存储单元可以连续也可以不连续. 链表中的节点包含数据域和指针域,数据域为存储数据元素信息的域,指针域为存储直接后继位置(一般称为指针)的域. 注意一个头结点和头指针的区别: 头指针: 指向链表的第一个节点的指针,若链表有头结点,则是指向头结点的指针: 头指针具有标识作用,所以常用头指针作为链表的名字: 不论链表是否为空,头指针都不为空: 是链表的必要元素. 头结点: 头结点是为了操作的统一和方便而设立的,放在第一个元素节点的前面,其数据域一般无意