数据结构之链表、栈和队列 java代码实现
定义抽象节点类Node:
package cn.wzbrilliant.datastructure; /**
* 节点
* @author ice
*
*/
public abstract class Node {
private Node next; public Node(){
next=null;
} public void setNext(Node nextNode){
next=nextNode;
} public Node getNext(){
return next;
}
}
链表类,实现了插入首尾节点、指定位置节点,删除节点、指定位置节点,链表的逆序以及判空操作:
package cn.wzbrilliant.datastructure; /**
* 链表
* @author ice
*
*/
public class Link { protected Node head;
protected Node tail;
protected int size; public Link() {
this.head = null;
this.tail = null;
this.size = 0;
} public void addAtFirst(Node node){
node.setNext(head);
head=node;
if(size==0)
tail=node;
size++;
} public void addAtLast(Node node){
if(size==0){
head=tail=node;
}else{
tail.setNext(node);
tail=node;
}
node.setNext(null);
size++;
} public void removeFirst(){
if(size==0)
throw new RuntimeException("link size is 0...");
head=head.getNext();
if(size==1){
tail.setNext(null);
}
size--;
} public void removeLast(){
if(size==0)
throw new RuntimeException("link size is 0..."); if(size==1){
head=tail=null;
size--;
return ;
} Node node=head;
while(node.getNext()!=tail){
node=node.getNext();
}
node.setNext(null);
tail=node;
size--;
} /**
* 队列逆序
*/
public void reverse() {
Node preNode, node, tempNode;
if (size == 0)
return;
preNode = head;
node = preNode.getNext();
preNode.setNext(null);
tail = preNode;
while (node != null) {
tempNode = node.getNext();
node.setNext(preNode);
preNode = node;
node = tempNode;
}
head = preNode;
} /**
* 在第index个节点后插入newNode
*
* @param newNode
* @param index
*/
public void insert(Node newNode, int index) {
if (index < 0 || index > size)
throw new RuntimeException("索引错误"); if (index == 0) {
newNode.setNext(head);
head = newNode;
size++;
return;
} if (index == size) {
newNode.setNext(null);
tail.setNext(newNode);
tail = newNode;
size++;
return;
} Node node = head;
for (int i = 1; node != null; i++, node = node.getNext()) {
if (i == index) {
newNode.setNext(node.getNext());
node.setNext(newNode);
size++;
return;
}
} } /**
* 移除Node节点 Node节点需重写equals()方法
*
* @param node
*/
public void remove(Node node) {
if (node == null || size == 0)
throw new RuntimeException("remove error...");
for (Node temp = head; temp != null; temp = temp.getNext()) {
if (temp == head) {
if (temp.equals(node)) {
head = head.getNext();
size--;
continue;
}
}
if (temp.getNext().equals(node)) {
temp.setNext(temp.getNext().getNext());
size--;
} }
} public Node getFirst() {
if (size == 0)
return null;
return head;
} public Node getLast() {
if (size == 0)
return null;
return tail;
} public int size() {
return size;
} public boolean isEmpty() {
if (size == 0)
return true;
return false;
}
}
栈类,实现了入栈、出战、获取栈顶元素以及判空的操作:
package cn.wzbrilliant.datastructure; /**
* 栈
* @author ice
*
*/
public class Stack {
private int size;
private Node top; public Stack() {
size = 0;
top = null;
} public void push(Node node) {
node.setNext(top);
top = node;
size++;
} public Node pop() {
if (top == null)
return null;
Node node = top;
top = top.getNext();
size--;
return node;
} public Node top() {
return top;
} public int size() {
return size;
} public boolean isEmpty() {
if (size == 0)
return true;
return false;
}
}
队列类,实现了入队、出队、判空的操作:
package cn.wzbrilliant.datastructure; /**
* 队列
* @author ice
*
*/
public class Queue { private Node head;
private Node tail;
private int size; public Queue() {
this.head = null;
this.tail = null;
this.size = 0;
} public void enQueue(Node node) {
tail.setNext(node);
tail = node;
size++;
} public Node deQueue() {
if (size == 0)
return null;
Node node = head;
head = head.getNext();
size--;
return node;
} public int size() {
return size;
} public boolean isEmpty() {
if (size == 0)
return true;
return false;
} }
数据结构之链表、栈和队列 java代码实现的更多相关文章
- Java数据结构和算法 - 栈和队列
Q: 栈.队列与数组的区别? A: 本篇主要涉及三种数据存储类型:栈.队列和优先级队列,它与数组主要有如下三个区别: A: (一)程序员工具 数组和其他的结构(栈.队列.链表.树等等)都适用于数据库应 ...
- SDUT-3335_数据结构实验之栈与队列八:栈的基本操作
数据结构实验之栈与队列八:栈的基本操作 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 堆栈是一种基本的数据结构.堆栈具 ...
- SDUT-2132_数据结构实验之栈与队列二:一般算术表达式转换成后缀式
数据结构实验之栈与队列二:一般算术表达式转换成后缀式 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于一个基于二元运 ...
- SDUT-2088_数据结构实验之栈与队列十一:refresh的停车场
数据结构实验之栈与队列十一:refresh的停车场 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description refresh最近发 ...
- SDUT-2449_数据结构实验之栈与队列十:走迷宫
数据结构实验之栈与队列十:走迷宫 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个由n * m 个格子组成的迷宫,起 ...
- SDUT-1479_数据结构实验之栈与队列九:行编辑器
数据结构实验之栈与队列九:行编辑器 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个简单的行编辑程序的功能是:接受用 ...
- SDUT-3334_数据结构实验之栈与队列七:出栈序列判定
数据结构实验之栈与队列七:出栈序列判定 Time Limit: 30 ms Memory Limit: 1000 KiB Problem Description 给一个初始的入栈序列,其次序即为元素的 ...
- SDUT-3332&3333_数据结构实验之栈与队列五:下一较大值
数据结构实验之栈与队列六:下一较大值 Time Limit: 150 ms Memory Limit: 8000 KiB Problem Description 对于包含n(1<=n<=1 ...
- SDUT-2134_数据结构实验之栈与队列四:括号匹配
数据结构实验之栈与队列四:括号匹配 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给你一串字符,不超过50个字符,可能 ...
随机推荐
- 在VS2010中使用Outlook工具栏
参考资料:微软MSDN.VS2010示例代码 一开始上段子总是能活跃气氛,等哪天我再打开自己的这篇博客,总是能够让自己傻傻的乐一下. 我一女同学,毕业去一大公司应聘,竞争很激烈,最后剩下她和一位女士. ...
- 读书笔记——Windows核心编程(8)Interlocked单向链式栈
SLists使用了无锁算法来保证原子同步,以提升系统性能,避免了诸如优先级挂和互锁的问题. 注意:所有的链表项必须对齐到MEMORY_ALLOCATION_ALIGNMENT.否则会出现奇葩的错误. ...
- Docker容器操作
启动一次容器并执行命令(执行完命令后结束): docker run centos cat /etc/redhat-release 启动容器进入交互模式: docker run -i -t centos ...
- linux基本命令学习笔记
这个几天在研究linux的常用基本命令.以下是此时间内的幻灯片截图笔记,在这里留个脚印. linux 常用命令 1,命令的基本格式 2,文件处理命令 3,文件搜索命令 4,帮助命令 5,压缩解压缩命令 ...
- java 根据 根节点及所有子成员 构造树tree
实体类entity package com.ompa.biz.entity; import java.util.ArrayList; import java.util.List; public cla ...
- 无向图的最短路径算法JAVA实现
一,问题描述 给出一个无向图,指定无向图中某个顶点作为源点.求出图中所有顶点到源点的最短路径. 无向图的最短路径其实是源点到该顶点的最少边的数目. 本文假设图的信息保存在文件中,通过读取文件来构造图. ...
- ehcache 一二事 - ssm 中ehcashe的简单配置应用
Ehcache是一个开源Java分布式缓存.可以配合mybatis来使用 首先,在资源文件夹中新建ehcache.xml 内容如下: <?xml version="1.0&qu ...
- java 15-10 List的三个子类的特点
List:(面试题List的子类特点) ArrayList: 底层数据结构是数组,查询快,增删慢. 线程不安全,效率高. Vector: 底层数据结构是数组,查询快,增删慢. 线程安全,效率低. Li ...
- finder怎么才能找到library
右键Finder——前往目录 输入~/Library
- Mina 中遇到SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".SLF4J: Defaulting to no-op ...