链表总结
发布日期:2021-09-29 21:09:49
浏览次数:2
分类:技术文章
本文共 11555 字,大约阅读时间需要 38 分钟。
直接上代码,有不合理之处希望大伙们提点修改意见:
1、单链表总结
包含了单链表的删除,增加,顺序增加,修改,查找倒数第k个元素,递归思想创建链表,递归思想反转链表,非递归思想反转链表,逆序打印链表;
package com.housy.linklist;import java.awt.HeadlessException;import java.lang.Thread.State;import java.util.Arrays;import java.util.List;import java.util.Stack;public class SingleLinkedListDemo { public static void main(String[] args) { HeroNode heroNode1 = new HeroNode(1, "李四", "哈哈1"); HeroNode heroNode2 = new HeroNode(2, "张三", "哈哈2"); HeroNode heroNode3 = new HeroNode(3, "王五", "哈哈3"); HeroNode heroNode4 = new HeroNode(4, "赵六", "哈哈4"); SingleLinkedList singleLinkedList = new SingleLinkedList(); //-------------------------testing the list to add---------------------------------- singleLinkedList.orderAdd(heroNode1); singleLinkedList.orderAdd(heroNode3); singleLinkedList.orderAdd(heroNode4); singleLinkedList.orderAdd(heroNode2);// singleLinkedList.list(); //-------------------------testing the list to modify---------------------------------- HeroNode heroNode = new HeroNode(3, "王五", "修改哈哈哈3"); singleLinkedList.modify(heroNode); singleLinkedList.list(); //-------------------------testing the list to delete----------------------------------// singleLinkedList.delete(heroNode1);// singleLinkedList.list();// System.out.println(SingleLinkedList.size(singleLinkedList.getHead())); //-------------------------used for testing list the reciprocal of the first k elements----------------------------------// System.out.println(singleLinkedList.countBackwardsByK(3)); -------------------------recursive thought to create linked list---------------------------------- // singleLinkedList.createLinkedList(Arrays.asList());// singleLinkedList.list();// System.out.println("------------------------");// singleLinkedList.createLinkedList(Arrays.asList(1));// singleLinkedList.list();// System.out.println("------------------------");// singleLinkedList.createLinkedList(Arrays.asList(1,2,3,4));// singleLinkedList.list(); //-------------------------recursive thought to reverse linked list---------------------------------- // System.out.println(singleLinkedList.createLinkedList(Arrays.asList()));// singleLinkedList.reverseLinkedList(singleLinkedList.createLinkedList(Arrays.asList()));// System.out.println("-------------------null-----");// singleLinkedList.list();// System.out.println("------------------------");// singleLinkedList.reverseLinkedList(singleLinkedList.createLinkedList(Arrays.asList(1)));// singleLinkedList.list();// System.out.println("------------------------");// singleLinkedList.reverseLinkedList(singleLinkedList.createLinkedList(Arrays.asList(1,2,3,4)));// singleLinkedList.list(); //-------------------------non-recursive thought to reverse linked list---------------------------------- // System.out.println(singleLinkedList.createLinkedList(Arrays.asList()));// singleLinkedList.reverseLinkedListByNonrecursive(singleLinkedList.createLinkedList(Arrays.asList()));// System.out.println("-------------------null-----");// singleLinkedList.list();// System.out.println("------------------------");// singleLinkedList.reverseLinkedListByNonrecursive(singleLinkedList.createLinkedList(Arrays.asList(1)));// singleLinkedList.list();// System.out.println("------------------------");// singleLinkedList.reverseLinkedListByNonrecursive(singleLinkedList.createLinkedList(Arrays.asList(1,2,3,4)));// singleLinkedList.list();// System.out.println("------------------------");// singleLinkedList.reverseLinkedListByNonrecursive(singleLinkedList.createLargeLinkedList(100000));// singleLinkedList.list();// singleLinkedList.createLargeLinkedList(10);// singleLinkedList.list(); //-----------------------------reverse print test-------------------------------- singleLinkedList.reversePrint(); }}class SingleLinkedList { /** * 判空 * 遍历 * * 先一般情况后特殊 */ private HeroNode head = new HeroNode(0, null, null);// private int i = 0; public HeroNode getHead() { return head; } //------------------------reverse print------------------------- public void reversePrint() { if (head.next == null) { return; } Stackstack = new Stack<>(); HeroNode temp = head.next; while(temp != null) { stack.push(temp); temp = temp.next; } while(!stack.isEmpty()) { System.out.println(stack.pop()); } } //--------------------recursive thought------------------------- public HeroNode createLinkedList(List no) { if (no.isEmpty()) { return null; } HeroNode firstNode = new HeroNode(no.get(0), null, null); if (no.get(0) == 1) { head.next = firstNode; } //System.out.println(no.get(0) + "运行第" + i ++ +"次 " + firstNode); firstNode.next = createLinkedList(no.subList(1, no.size())); return firstNode; } public HeroNode reverseLinkedList(HeroNode heroNode) { System.out.println(heroNode); if (heroNode == null || heroNode.next == null ) { head.next = heroNode; System.out.println(heroNode);// System.out.println("head.next = " + head.next); return heroNode; } HeroNode newHead = reverseLinkedList(heroNode.next); heroNode.next.next = heroNode; heroNode.next = null; return newHead; } //--------------------non-recursive thought------------------------- public HeroNode reverseLinkedListByNonrecursive(HeroNode heroNode) { HeroNode newHead = null; HeroNode curHead = heroNode; while(curHead != null) { //curHead == null HeroNode next = curHead.next; curHead.next = newHead; newHead = curHead; curHead = next; } head.next = newHead; return newHead; } public HeroNode createLargeLinkedList(int size) { HeroNode prev = null; HeroNode first = null; for(int i = 1; i <= size; i++) { HeroNode heroNode = new HeroNode(i, null, null); if (prev != null) { prev.next = heroNode; } else { first = heroNode; head.next = first; } prev = heroNode;// System.out.println(prev); } return first; } public void add(HeroNode heroNode) { HeroNode temp = head; while(temp.next != null) { temp = temp.next; } temp.next = heroNode; } public void list() { HeroNode temp = head.next;// System.out.println("head.next = " + head.next); if (head.next == null) { return; } while(temp != null) { System.out.println(temp); temp = temp.next; } } public void orderAdd(HeroNode heroNode) { HeroNode temp = head; boolean flag = false; while(temp.next != null) { if (temp.next.no > heroNode.no) { break; } else if (temp.next.no == heroNode.no) { flag = true; } temp = temp.next; } if (flag) { System.out.println("编号存在"); } else { heroNode.next = temp.next; temp.next = heroNode; } } public void modify(HeroNode heroNode) { if (head.next == null) { return; } HeroNode temp = head.next; boolean flag = false; while(temp != null) { if (temp.no == heroNode.no) { flag = true; break; } temp = temp.next; } if (flag) { temp.name = heroNode.name; temp.nickname = heroNode.nickname; } else { System.out.println("未找到!"); } } public void delete(HeroNode heroNode) { if (head.next == null) { return; } HeroNode temp = head.next; boolean flag = false; while(temp != null) { if (temp.no == heroNode.no) {// System.out.println(heroNode.next ); head.next = heroNode.next; break; } if (temp.next.no == heroNode.no) { flag = true; break; } temp = temp.next; }// System.out.println(flag + " " + temp); if (flag) { temp.next = heroNode.next; heroNode.next = null; } } public static int size(HeroNode head) { int length = 0; if (head.next == null) { return 0; } HeroNode temp = head.next; while(temp != null) { length++; temp = temp.next; } return length; } public HeroNode countBackwardsByK(int k) { int count = size(getHead()) - k; HeroNode temp = head.next; while(count != 0) { temp = temp.next; count--; } return temp; }}class HeroNode { public int no; public String name; public String nickname; public HeroNode next; public HeroNode(int no, String name, String nickname) { this.no = no; this.name = name; this.nickname = nickname; this.next = null; } @Override public String toString() { return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname + "]"; } }
在利用栈逆向打印链表的时候有这么一句代码:Stack<HeroNode> stack = new Stack<>();
有关泛型:后面尖括号里面是可以为null也可以为HeroNode,这是由于在声明stack的时候,就已经知道了这是一个存放HeroNode类型的栈,所以加不加都是可以的,在早期java(jdk1.5之前)中,是没有尖括号的,没有泛型这个概念,意思是Stack<Object>,而加上就是HeroNode的栈了。
2、双向链表
package com.housy.linklist;public class DoubleLinkedListDemo { public static void main(String[] args) { HeroNode2 heroNode1 = new HeroNode2(1, "Bob", "foolish"); HeroNode2 heroNode2 = new HeroNode2(2, "Alice", "beautiful"); HeroNode2 heroNode3 = new HeroNode2(3, "Housy", "cutey"); HeroNode2 heroNode4 = new HeroNode2(4, "Jack", "intelligent"); DoubleLinkedList doubleLinkedList = new DoubleLinkedList(); doubleLinkedList.add(heroNode1); doubleLinkedList.add(heroNode2); doubleLinkedList.add(heroNode3); doubleLinkedList.add(heroNode4); doubleLinkedList.list(); System.out.println("---------------------delete 4-----------------"); doubleLinkedList.delete(4); doubleLinkedList.list(); System.out.println("---------------------delete 2-----------------"); doubleLinkedList.delete(2); doubleLinkedList.list(); }}class DoubleLinkedList { private HeroNode2 head = new HeroNode2(0, null, null); public HeroNode2 getHead() { return head; } public void add(HeroNode2 heroNode) { HeroNode2 temp = head; while(temp.next != null) { temp = temp.next; } temp.next = heroNode; heroNode.pre = temp; } public void delete(int no) { if (head.next == null) { System.out.println("链表为null"); return; } HeroNode2 temp = head.next; boolean flag = false; while(temp != null) { if (temp.no == no) { flag = true; break; } temp = temp.next; } if (flag) { temp.pre.next = temp.next; if (temp.next != null) { temp.next.pre = temp.pre; } } } public void list() { HeroNode2 temp = head.next;// System.out.println("head.next = " + head.next); if (head.next == null) { System.out.println("---------------null------------"); return; } while(temp != null) { System.out.println(temp); temp = temp.next; } }}class HeroNode2 { public int no; public String name; public String nickname; public HeroNode2 next; public HeroNode2 pre; public HeroNode2(int no, String name, String nickname) { super(); this.no = no; this.name = name; this.nickname = nickname; this.next = null; this.pre = null; } @Override public String toString() { return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname + "]"; } }
转载地址:https://blog.csdn.net/hou_shiyu/article/details/105979979 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月11日 07时34分34秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【Leetcode刷题篇】leetcode316 去除重复字母
2019-04-26
【Leetcode刷题篇】leetcode1081 不同字符的最小子序列
2019-04-26
【面试篇】Java网络编程与IO流体系
2019-04-26
【大话Mysql面试】-Mysql的索引为什么要使用B+树,而不是B树,红黑树等之类?
2019-04-26
【大话Mysql面试】-如何通俗易懂的了解Mysql的索引最左前缀匹配原则
2019-04-26
【大话Mysql面试】-MYSQL的两种存储引擎MyISAM与InnoDB的区别是什么?
2019-04-26
理解String.intern()和String类常量池疑难解析例子
2019-04-26
python flask打造前后端分离的口罩检测
2019-04-26
【大话Mysql面试】-MySQL基础知识
2019-04-26
【大话Mysql面试】-MySQL数据类型有哪些
2019-04-26
【大话Mysql面试】-MySQL数据引擎
2019-04-26
【大话Mysql面试】-常见SQL语句书写
2019-04-26
【大话Mysql面试】-SQL语句优化
2019-04-26
【大话Mysql面试】-Mysql事务以及隔离级别
2019-04-26
【大话Mysql面试】-Mysql索引
2019-04-26
【大话Mysql面试】-Mysql锁
2019-04-26
【大话Mysql面试】-Mysql常见面试题目
2019-04-26
08 【多线程高并发】Java线程间通信的方式
2019-04-26