标签:Node head JAVA cur 单向 System next 链表 null
【JAVA】实现一个无头单向非循环链表
链表
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。
链表的结构多样,这里列出8种链表结构:
单向带头循环、单向不带头循环、单向带头非循环、单向不带头非循环
双向带头循环、双向不带头循环、双向带头非循环、双向不带头非循环
虽然有这么多的链表的结构,但是重点掌握两种:
无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。
无头双向链表:在Java的集合框架库中LinkedList底层实现就是无头双向循环链表。
无头单向非循环链表实现
MyLinkedList.java
class Node{
public int val;
public Node next;
public Node(){
}
public Node(int val) {
this.val = val;
}
}
//无头单向非循环链表实现
public class MyLinkedList {
public Node head;//表示当前链表的头,默认null
//构建链表
public void createdLinked() {
this.head = new Node(12);
Node node2 = new Node(22);
Node node3 = new Node(32);
Node node4 = new Node(42);
this.head.next = node2;
node2.next = node3;
node3.next = node4;
}
//得到单链表的长度
public int size() {
Node cur = this.head;
int count = 0;
while(cur != null) {
count++;
cur = cur.next;
}
return count;
}
//清空链表
public void clear() {
this.head = null;
}
//打印链表
public void display() {
Node cur = this.head;
while(cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
public void display1(Node node){
while(node != null) {
System.out.print(node.val + " ");
node = node.next;
}
System.out.println();
}
//找链表最后一个节点
public Node findLastNode() {
if(this.head == null) {
System.out.println("head == null");
return null;
}
Node cur = this.head;
while (cur.next != null) {
cur = cur.next;
}
return cur;
}
//找链表倒数第二个节点
public Node findLastTwoNode() {
if(this.head == null) {
System.out.println("head == null");
return null;
}
if(this.head.next == null) {
System.out.println("Only One Node");
return null;
}
Node cur = this.head;
while(cur.next.next != null) {
cur = cur.next;
}
return cur;
}
//找到第n个节点
public Node findN(int n){
if(this.head == null) {
System.out.println("head == null");
return null;
}
if(n <= 0) {
System.out.println("太小");
return null;
}
if(n > size()){
System.out.println("太大");
return null;
}
int count = 0;
Node cur = this.head;
while(count != n) {
count++;
cur = cur.next;
}
return cur;
}
//头插法
public void addFirst(int data) {
Node node = new Node(data);
if(this.head == null) {
this.head = node;
}else{
node.next = this.head;
this.head = node;
}
}
//尾插法
public void addLast(int data) {
Node node = new Node(data);
if(this.head == null) {
this.head = node;
}else{
Node cur = this.head;
while (cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
}
//找到index-1位置的节点的引用
public Node moveIndex(int index) {
Node cur = this.head;
int count = 0;
while(count != index-1) {
cur = cur.next;
count++;
}
return cur;
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data) {
if(index < 0 || index > size()) {
System.out.println("illegal");
return;
}
if(index == 0){
addFirst(data);
return;
}
if(index == size()) {
addLast(data);
return;
}
Node cur = moveIndex(index);//cur保存index - 1位置的节点的引用
Node node = new Node(data);
node.next = cur.next;
cur.next = node;
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key) {
Node cur = this.head;
while(cur != null) {
if(cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
//查找key的前驱,如果有返回前驱节点的引用,没有返回null
public Node searchPrev(int key) {
Node cur = this.head;
while (cur.next != null) {
if(cur.next.val == key){
return cur;
}
cur = cur.next;
}
return null;
}
//删除第一次出现关键字为key的节点
public void remove(int key) {
if(this.head == null) {
System.out.println("head == null");
}
if(this.head.val == key){
this.head = this.head.next;
}
Node prev = searchPrev(key);
if(prev == null) {
System.out.println("error");
}else{
Node del = prev.next;
prev.next = del.next;
}
}
//删除所有值为key的节点
public void removeAllkey(int key) {
if(this.head == null){
System.out.println("head == null");
}
Node prev = this.head;
Node cur = prev.next;
while(cur != null) {
if(cur.val == key) {
prev.next = cur.next;
}else{
prev = cur;
}
cur = cur.next;
}
if(this.head.val == key) {
this.head = this.head.next;
}
}
//反转链表
public Node reverseList() {
Node cur = this.head;
Node prev = null;
Node newHead = null;
while (cur != null) {
Node curNext = cur.next;
if(curNext == null) {
newHead = cur;
}
cur.next = prev;
prev = cur;
cur = curNext;
}
return newHead;
}
//返回链表的中间节点,如果有两个中间节点则返回第二个
public Node middleNode1(){
int len = size() / 2;
Node cur = this.head;
int count = 0;
while (count != len) {
cur = cur.next;
count++;
}
return cur;
}
public Node middleNode() {
Node fast = this.head;
Node slow = this.head;
while (fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
}
Main.java
public class Main {
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.createdLinked();//12 22 32 42
myLinkedList.addLast(100);//12 22 32 42 100
myLinkedList.addFirst(1);//1 12 22 32 42 100
myLinkedList.addIndex(0,110);//110 1 12 22 32 42 100
myLinkedList.display();//110 1 12 22 32 42 100
System.out.println("=================");
System.out.println("链表长度为:"+myLinkedList.size());//7
System.out.println("=================");
System.out.println("链表中是否有元素12:"+myLinkedList.contains(12));//true
System.out.println("=================");
int n = 4;
Node ret = myLinkedList.findN(n);
System.out.println("链表第"+n+"个节点是:"+ret.val);
System.out.println("=================");
System.out.println("421是否在链表中"+myLinkedList.contains(421));
System.out.println("==================");
ret = myLinkedList.findLastTwoNode();
System.out.println(ret.val);
System.out.println("虽然发生了异常,但是我还是想打印这句话");
System.out.println("==================");
myLinkedList.remove(1);
myLinkedList.display();//110 12 22 32 42 100
System.out.println("==================");
Node ret1 = myLinkedList.reverseList();
myLinkedList.display1(ret1);//100 42 32 22 12 110
myLinkedList.clear();
}
}
标签:Node,head,JAVA,cur,单向,System,next,链表,null 来源: https://blog.csdn.net/CYQAQ/article/details/112055621
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。