package chain; public class Node { // 0 数据结构部分 private Node next; private int data; // 1 构造函数 public Node(int data) { this.data = data; } // 2 显示 public void display(){ System.out.println(data+ " "); } public int display1(){ return data; } /** * @return the next */ public Node getNext() { return next; } /** * @param next the next to set */ public void setNext(Node next) { this.next = next; } /** * @return the data */ public int getData() { return data; } /** * @param data the data to set */ public void setData(int data) { this.data = data; } }
package chain; /** * 单链表 * @author zm * */ public class LinkList { // 0 数据结构 private Node head; private Node tail; private int size; // 1构造函数 public LinkList(){ this.head = null; this.tail = null; this.size = 0; } public LinkList(int data){ this.head = new Node(data); this.tail = null; this.size = 1; } // 2方法 // 2.1 插入 public void add(int data){ Node node = new Node(data); if(this.head == null){ this.head = node; this.tail = node; }else{ this.tail.setNext(node); this.tail = node; } this.size ++; } // 2.2清空所有 public void clear(){ this.head = null; this.tail = null; System.gc(); } // 2.3删除最后节点 public int deleteLast(){ // 0 得到倒数第二个节点 // 1 删除最后一个节点 // 2 将倒数第二个节点作为最后一个节点 Node point = this.head; while(point.getNext() != this.tail) { point = point.getNext(); } tail = point; tail.setNext(null); size--; return 0; } // 2.4 根据角标删除元素(仅删除头、尾节点之外的元素) public boolean delete(int location){ if(location >= this.size -1 || location <= 0){ System.out.println("out of range"); return false; } boolean result = false; int count = 0; Node node = this.head; Node priveos = null; while(node.getNext() != this.tail){ // 找到要删除角标的上一个位置 if(count == location-1){ System.out.println("find the data, the previos location is " + (location-1)); System.out.println("the previos data is " + node.display1()); priveos = node; result = true; size--; break; } node = node.getNext(); count++; } if(result){ Node dealNode = priveos.getNext(); Node dealNextNode = dealNode.getNext(); priveos.setNext(dealNextNode); dealNode.setNext(null); } return result; } // 2.5 判定是否包含元素 public boolean exists(int data){ boolean flag = false; Node point = head; while(point.getNext() != null) {// 先判断非尾元素节点 if(point.getData() == data){ flag = true; break; } point = point.getNext(); } if(!flag){ // 最后判断尾巴节点 if(tail.getData() == data) { flag = true; } } return flag; } // 2.6 显示所有元素 public void display(){ Node node = head; while(node.getNext() != null){// 先显示非尾元素节点 System.out.println(node.getData()); node = node.getNext(); } System.out.println(tail.getData());// 最后显示尾元素节点 } // 2.7 显示头结点 public void deleteHead(){ if(this.size ==1){ head = null; tail = null; size = 0; }else{ Node node = head.getNext(); head.setNext(null); head = node; } } public int size() { return this.size; } public static void main(String[] args) { LinkList list = new LinkList(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); //System.out.println(list.size()); //list.display(); //list.deleteLast(); //list.display(); //list.delete(3); // list.deleteHead(); list.display(); System.out.println(list.exists(4)); } }
相关推荐
java单链表操作,封装性高,方便查看,考试、学习都可使用
算法-单链表遍历及反转(java)(csdn)————程序
单链表的定义 单链表是一种线性数据结构,它包含一系列的节点,每个节点都含有一个值和指向下一个节点的引用。 单链表的特性 单链表具有动态性,可以灵活地插入和删除元素,但访问元素时需要从头节点开始顺序查找。 ...
通过Java实现单链表的操作,包括单链表定义、遍历、置空、判空、插入、删除、反转、中间结点、指定顺序排序、前插、后插、判断单链表是否存在环、环的长度、环的起始结点
Java实现单链表的基本操作
基于JAVA创建单链表,并实现了在队列末尾增加、删除元素,在队列中插入、删除元素,打印链表。
使用java实现单链表。重点介绍插入与建表
单链表的插入,删除,排序!集合的并、交和差运算 编制一个能演示执行集合的并、交和差...vc mfc编的 集合运算 交并补差 包含...采用有序单链表表示集合
使用java语言编译数据结构的循环单链表,有三个类,分别是接口类,循环单链表的继承接口类的实现类,和循环单链表的测试类。
java单链表
数据结构中使用Java实现单链表的创建、读取等基本操作
Java的循环单链表及其测试程序,用Java实现循环单链表,大家可以参考参考。
单链表常见的几个面试题 新浪,腾讯内部面试题目 题目分析 代码实现 测试用例 一,面试题目 统计链表中有效节点的个数(不含头结点)【新浪】 获取链表中倒数第K个元素【新浪】 单向链表的逆转【腾讯】 二,题目分析...
文章代码实现了用Java编程语言来实现了数据结构中的单链表结构,提供了操作链表的基本方法以及排序方法
Java实现单链表的增删改查以及选择、冒泡、反转排序等功能的实现!
JAVA单链表(多项式)直接插入排序 JAVA单链表(多项式)直接插入排序
链表去重 基于java的单链表增、删、改、查、去重操作
使用java实现单链表,数据结构的基础知识........................................................................................