定义抽象节点类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代码实现的更多相关文章

  1. Java数据结构和算法 - 栈和队列

    Q: 栈.队列与数组的区别? A: 本篇主要涉及三种数据存储类型:栈.队列和优先级队列,它与数组主要有如下三个区别: A: (一)程序员工具 数组和其他的结构(栈.队列.链表.树等等)都适用于数据库应 ...

  2. SDUT-3335_数据结构实验之栈与队列八:栈的基本操作

    数据结构实验之栈与队列八:栈的基本操作 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 堆栈是一种基本的数据结构.堆栈具 ...

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

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

  4. SDUT-2088_数据结构实验之栈与队列十一:refresh的停车场

    数据结构实验之栈与队列十一:refresh的停车场 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description refresh最近发 ...

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

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

  6. SDUT-1479_数据结构实验之栈与队列九:行编辑器

    数据结构实验之栈与队列九:行编辑器 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个简单的行编辑程序的功能是:接受用 ...

  7. SDUT-3334_数据结构实验之栈与队列七:出栈序列判定

    数据结构实验之栈与队列七:出栈序列判定 Time Limit: 30 ms Memory Limit: 1000 KiB Problem Description 给一个初始的入栈序列,其次序即为元素的 ...

  8. SDUT-3332&3333_数据结构实验之栈与队列五:下一较大值

    数据结构实验之栈与队列六:下一较大值 Time Limit: 150 ms Memory Limit: 8000 KiB Problem Description 对于包含n(1<=n<=1 ...

  9. SDUT-2134_数据结构实验之栈与队列四:括号匹配

    数据结构实验之栈与队列四:括号匹配 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给你一串字符,不超过50个字符,可能 ...

随机推荐

  1. Redis高级应用

    上一篇博文讲述了Redis的一些常用命令,可以对数据库及数据库服务器进行操作,本篇将讲述Redis的高级应用及配置 安全性 设置密码:修改redis.conf中的requirepass,在其后面添加密 ...

  2. nginx根据IP限制访问

    nginx有两个模块可以控制访问 HttpLimitZoneModule    限制同时并发访问的数量 HttpLimitReqModule     限制访问数据,每秒内最多几个请求 http{ ## ...

  3. Linux gcc命令

    一.简介 GCC 的意思也只是 GNU C Compiler 而已.经过了这么多年的发展,GCC 已经不仅仅能支持 C 语言:它现在还支持 Ada 语言.C++ 语言.Java 语言.Objectiv ...

  4. Android反编译

    反编译(未混淆情况) 1.获取资源文件: 命令行界面apktool.bat d -f  test.apk  fileName  (然而修改后缀名为.zip即可获得): apktool2.0以上版本:a ...

  5. docker containerd中的容器操作

    containerd的中的各种操作都是通过Task来进行的,因此对于容器的create, start, delete等等操作其实都是一个个的Task而已. Task的数据结构如下所示: type Ta ...

  6. Linux的交叉编译 及configure配置

    这两天需要把一个CDVS的工程代码从Linux 平台上移植到ARM平台上,花了两天才搞定,之前很早申请的博客,到现在还没有技术文章会,以后决定凡是花两三天才搞定的东西都会把解决过程发到这里,很多东西靠 ...

  7. 内网穿透利器 Ngrok 使用教程

    ngrok 服务可以分配给你一个域名让你本地的web项目提供给外网访问,特别适合向别人展示你本机的web demo 以及调试一些远程的API (比如微信公众号,企业号的开发) 下面开始教程 Step ...

  8. Redis安装,mongodb安装,hbase安装,cassandra安装,mysql安装,zookeeper安装,kafka安装,storm安装大数据软件安装部署百科全书

    伟大的程序员版权所有,转载请注明:http://www.lenggirl.com/bigdata/server-sofeware-install.html 一.安装mongodb 官网下载包mongo ...

  9. nginx 与 tomcat 集群 一二事 (0) - 简单介绍

    最近看了nginx以及tomcat的集群,通俗的做一下简单总结吧 nginx 是一个http服务器,是由俄罗斯人发明的,目前主流的服务器,作为负载均衡服务器,性能非常好,最高支持5万个并发连接数,在淘 ...

  10. MySQL数据库学习笔记(十)----JDBC事务处理、封装JDBC工具类

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...