定义抽象节点类Node:

 1 package cn.wzbrilliant.datastructure;
 2
 3 /**
 4  * 节点
 5  * @author ice
 6  *
 7  */
 8 public abstract class Node {
 9     private Node next;
10
11     public Node(){
12         next=null;
13     }
14
15     public void setNext(Node nextNode){
16         next=nextNode;
17     }
18
19     public Node getNext(){
20         return next;
21     }
22 }

链表类,实现了插入首尾节点、指定位置节点,删除节点、指定位置节点,链表的逆序以及判空操作:

  1 package cn.wzbrilliant.datastructure;
  2
  3 /**
  4  * 链表
  5  * @author ice
  6  *
  7  */
  8 public class Link {
  9
 10     protected Node head;
 11     protected Node tail;
 12     protected int size;
 13
 14     public Link() {
 15         this.head = null;
 16         this.tail = null;
 17         this.size = 0;
 18     }
 19
 20     public void addAtFirst(Node node){
 21         node.setNext(head);
 22         head=node;
 23         if(size==0)
 24             tail=node;
 25         size++;
 26     }
 27
 28     public void addAtLast(Node node){
 29         if(size==0){
 30             head=tail=node;
 31         }else{
 32             tail.setNext(node);
 33             tail=node;
 34         }
 35         node.setNext(null);
 36         size++;
 37     }
 38
 39     public void removeFirst(){
 40         if(size==0)
 41             throw new RuntimeException("link size is 0...");
 42         head=head.getNext();
 43         if(size==1){
 44             tail.setNext(null);
 45         }
 46         size--;
 47     }
 48
 49     public void removeLast(){
 50         if(size==0)
 51             throw new RuntimeException("link size is 0...");
 52
 53         if(size==1){
 54             head=tail=null;
 55             size--;
 56             return ;
 57         }
 58
 59         Node node=head;
 60         while(node.getNext()!=tail){
 61             node=node.getNext();
 62         }
 63         node.setNext(null);
 64         tail=node;
 65         size--;
 66     }
 67
 68     /**
 69      * 队列逆序
 70      */
 71     public void reverse() {
 72         Node preNode, node, tempNode;
 73         if (size == 0)
 74             return;
 75         preNode = head;
 76         node = preNode.getNext();
 77         preNode.setNext(null);
 78         tail = preNode;
 79         while (node != null) {
 80             tempNode = node.getNext();
 81             node.setNext(preNode);
 82             preNode = node;
 83             node = tempNode;
 84         }
 85         head = preNode;
 86     }
 87
 88     /**
 89      * 在第index个节点后插入newNode
 90      *
 91      * @param newNode
 92      * @param index
 93      */
 94     public void insert(Node newNode, int index) {
 95         if (index < 0 || index > size)
 96             throw new RuntimeException("索引错误");
 97
 98         if (index == 0) {
 99             newNode.setNext(head);
100             head = newNode;
101             size++;
102             return;
103         }
104
105         if (index == size) {
106             newNode.setNext(null);
107             tail.setNext(newNode);
108             tail = newNode;
109             size++;
110             return;
111         }
112
113         Node node = head;
114         for (int i = 1; node != null; i++, node = node.getNext()) {
115             if (i == index) {
116                 newNode.setNext(node.getNext());
117                 node.setNext(newNode);
118                 size++;
119                 return;
120             }
121         }
122
123     }
124
125     /**
126      * 移除Node节点    Node节点需重写equals()方法
127      *
128      * @param node
129      */
130     public void remove(Node node) {
131         if (node == null || size == 0)
132             throw new RuntimeException("remove error...");
133         for (Node temp = head; temp != null; temp = temp.getNext()) {
134             if (temp == head) {
135                 if (temp.equals(node)) {
136                     head = head.getNext();
137                     size--;
138                     continue;
139                 }
140             }
141             if (temp.getNext().equals(node)) {
142                 temp.setNext(temp.getNext().getNext());
143                 size--;
144             }
145
146         }
147     }
148
149     public Node getFirst() {
150         if (size == 0)
151             return null;
152         return head;
153     }
154
155     public Node getLast() {
156         if (size == 0)
157             return null;
158         return tail;
159     }
160
161     public int size() {
162         return size;
163     }
164
165     public boolean isEmpty() {
166         if (size == 0)
167             return true;
168         return false;
169     }
170 }

栈类,实现了入栈、出战、获取栈顶元素以及判空的操作:

 1 package cn.wzbrilliant.datastructure;
 2
 3 /**
 4  * 栈
 5  * @author ice
 6  *
 7  */
 8 public class Stack {
 9     private int size;
10     private Node top;
11
12     public Stack() {
13         size = 0;
14         top = null;
15     }
16
17     public void push(Node node) {
18         node.setNext(top);
19         top = node;
20         size++;
21     }
22
23     public Node pop() {
24         if (top == null)
25             return null;
26         Node node = top;
27         top = top.getNext();
28         size--;
29         return node;
30     }
31
32     public Node top() {
33         return top;
34     }
35
36     public int size() {
37         return size;
38     }
39
40     public boolean isEmpty() {
41         if (size == 0)
42             return true;
43         return false;
44     }
45 }

队列类,实现了入队、出队、判空的操作:

 1 package cn.wzbrilliant.datastructure;
 2
 3 /**
 4  * 队列
 5  * @author ice
 6  *
 7  */
 8 public class Queue {
 9
10     private Node head;
11     private Node tail;
12     private int size;
13
14     public Queue() {
15         this.head = null;
16         this.tail = null;
17         this.size = 0;
18     }
19
20     public void enQueue(Node node) {
21         tail.setNext(node);
22         tail = node;
23         size++;
24     }
25
26     public Node deQueue() {
27         if (size == 0)
28             return null;
29         Node node = head;
30         head = head.getNext();
31         size--;
32         return node;
33     }
34
35     public int size() {
36         return size;
37     }
38
39     public boolean isEmpty() {
40         if (size == 0)
41             return true;
42         return false;
43     }
44
45 }

转载于:https://www.cnblogs.com/z941030/p/4700760.html

数据结构之链表、栈和队列 java代码实现相关推荐

  1. 栈、队列 java代码实现

    文章目录 普通队列 数组实现 java代码实现 单元测试 控制台打印 链表实现 java代码实现 单元测试 控制台打印 LinkedList队列使用 优先队列:PriorityQueue使用 栈 数组 ...

  2. char栈java,Java数据结构与算法-栈和队列(示例代码)

    (摘录加总结)------ 栈和队列不属于基础的数据结构,它们都属于线性表. 一.栈 对于栈存储操作元素只能在栈结构的一端进行元素的插入和删除,是一种性质上的线性表结构.按照"先进后出&qu ...

  3. 数据结构与算法(二) 栈与队列(代码示例)

    数据结构与算法 栈与队列 1. 数组和链表实现栈 2. 用O(1)的时间复杂度求栈中的最小元素 3. 链表和数组实现队列 4. 用两个栈模拟队列操作 1. 数组和链表实现栈 链表的方式: /*** 描 ...

  4. DSt:数据结构的简介、最强学习路线(逻辑结构【数组-链表-栈和队列/树-图-哈希】、物理结构、数据运算【十大排序/四大查找-图三大搜索-树三大遍历】、高级算法【贪心/分治/动态规划】之详细攻略

    DSt:数据结构的简介.最强学习路线(逻辑结构[数组-链表-栈和队列/树-图-哈希].物理结构[元素/关系].数据运算[十大排序/四大查找-图三大搜索-树三大遍历].高级算法[贪心/分治/动态规划]) ...

  5. 数据结构与算法-栈与队列

    数据结构与算法-栈与队列 栈 基本概念 简单表述就是仅在表尾进行插入和删除操作的线性表. 常见操作 入栈和出栈, 均在线性表的尾部进行. 基本原则就是, 先入后出. 队列 基本概念 和栈不同的是,队列 ...

  6. 《LeetCode力扣练习》剑指 Offer 09. 用两个栈实现队列 Java

    <LeetCode力扣练习>剑指 Offer 09. 用两个栈实现队列 Java 一.资源 题目: 用两个栈实现一个队列.队列的声明如下,请实现它的两个函数 appendTail 和 de ...

  7. sdut-3332 数据结构实验之栈与队列五:下一较大值(一)

    数据结构实验之栈与队列五:下一较大值(一) Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Desc ...

  8. SDUT-2132_数据结构实验之栈与队列二:一般算术表达式转换成后缀式

    数据结构实验之栈与队列二:一般算术表达式转换成后缀式 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于一个基于二元运 ...

  9. SDUT-2449_数据结构实验之栈与队列十:走迷宫

    数据结构实验之栈与队列十:走迷宫 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个由n * m 个格子组成的迷宫,起 ...

最新文章

  1. struts2配置文件的位置
  2. when and where is getControllerName called
  3. 计算机的安全设置在哪儿,电脑防火墙在哪里设置
  4. java cas原理_Java并发之原子变量及CAS算法-上篇
  5. 缩点【洛谷P1262】 间谍网络
  6. 微软为 Chromium 版 Edge 浏览器推出新的 logo
  7. Oracle——集合运算
  8. python3 beautifulsoup_Python3中BeautifulSoup的使用方法
  9. 全文搜索工具 AnyTXT Searcher(转载)
  10. 扎根黄金赛道,尚未盈利的捍宇医疗如何遨游行业蓝海?
  11. animation动画全解
  12. Vue 3的provide和inject用法
  13. android 获得ram大小,Android中获取(RAM)总运存大小和可用运存大小
  14. 烤仔的朋友们丨政策暖风吹来,国内公链们的春天来了?
  15. virt-viewer的简单使用
  16. WQ7033开发指南(基础篇)之1.2 烧录固件详解
  17. 微信公众号支付对接流程
  18. PaddleDetection复现笔记
  19. Altium Designer16.0中查找元器件的三种方法
  20. Ethereal的CAP文件格式说明

热门文章

  1. ArcGIS Server 10.1动态图层 以及Windows Phone/Silverlight客户端实现
  2. [转载] C#面向对象设计模式纵横谈——12. Flyweight享元模式
  3. 数据中心虚拟化不是IT变革的终点
  4. 用php对文件的操作
  5. redux源码分析之一:createStore.js
  6. Oracle Rac 11R2添加节点
  7. 高性能服务器架构(一):缓冲策略
  8. 【.net深呼吸】动态类型(高级篇)
  9. 启示—地点IT高管20在职场心脏经(读书笔记6)
  10. Gridview实现银行选择列表