数据结构之链表、栈和队列 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个字符,可能 ...
随机推荐
- nyoj 667 碟战 最小割(最大流)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=677 题意转化:将点0与所有的有间谍的点相连,则题意变为求点0到点n的最小割,直接套最大流 ...
- sql 查询基本语法
1.计算列 select * from emp --*表示所有的 --from emp 表示从emp表查询 select empno,enam ...
- XSLT
一.简介 XSLT 是一种用于将 XML 文档转换为 XHTML 文档或其他 XML 文档的语言. XSL(eXtensible Stylesheet Language) -- 可扩展标记语言,主要用 ...
- 简单好用的日志管理工具 Logrotate
前言 日志就像程序的生命记录仪,详细记录下了程序运行的点点滴滴. 慎重的选择记录哪些日志:在茫茫日志海中寻找真正记录问题的日志,你是不想经历的: 精心的定时压缩转移日志:故障发生了,日志却丢了,此时的 ...
- Shell basic1
A shell script is a text file that typically begins with a shebang, as follows: #!/bin/bash /bin/bas ...
- hdu 1028 Ignatius and the Princess III 简单dp
题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...
- disabled和readonly的区别?
在博客园中看到这样一篇文章,关于disabled和readonly的区别,以前还真的没有注意它们的区别,还是有必要知道它们的区别的,所以转载了. 这两个属性有类似之处,但是区别也是巨大的,之所以说类似 ...
- SSH框架总结(框架分析+环境搭建+实例源码下载) 《转》
这篇文章比较易懂,易理解: 首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层W ...
- JavaWeb学习----JSP简介及入门(含Eclipse for Java EE及Tomcat的配置)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- Unity3D开发之搭建Mac OS开发环境
运行图 首先上几张图 IOS模拟器 坚屏 横屏 打包任务 摸索了一上午,才搞定在模拟器中运行.至于在Iphone真机中运行,虽然有开发者证书,目前还没在Xcode中配置好. 我今天第一次接触并使用MA ...