数据结构之链表、栈和队列 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个字符,可能 ...
随机推荐
- MFC获取光标相对于控件所在行
获取光标在控件上所在的行数 /*editControl为Edit Control的变量*/ CPoint point = editControl.GetCaretPos(); //获取光标相对于控件的 ...
- Tomcat常用的优化技巧
(1)屏蔽DNS查询 Web应用程序可以通过Web容器提供的getRemoteHost()方法获得访问Web应用客户的IP地址和名称,但是这样会消耗Web容器的资源,并且还需要通过IP地址和DNS服务 ...
- 通过java来批量生成身份证号
通过java来批量生成身份证号,方便来测试程序. package com.diyvc.controller.user; import java.util.Calendar; import java.u ...
- python判断字符串,str函数isdigit、isdecimal、isnumeric的区别
s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小写s.isupper() ...
- [转]Oracle如何实现创建数据库、备份数据库及数据导出导入的一条龙操作
本文转自:http://www.cnblogs.com/wuhuacong/archive/2012/03/09/2387680.html Oracle中对数据对象和数据的管理,无疑都是使用PL/SQ ...
- 边工作边刷题:70天一遍leetcode: day 75
Group Shifted Strings 要点:开始就想到了string之间前后字符diff要相同. 思维混乱的地方:和某个string的diff之间是没有关系的.所以和单个string是否在那个点 ...
- 如何禁止 iPhone Safari video标签视频自动全屏?
最近做一个移动端微信页面项目,在微信页面中有视频播放,但是需要禁止IOS的自动全屏播放(前提必须使用video标签).如: <video id="post" autoplay ...
- 安全框架 - Shiro与springMVC整合的注解以及JSP标签
Shiro想必大家都知道了,之前的文章我也有提过,是目前使用率要比spring security都要多的一个权限框架,本身spring自己都在用shiro,之前的文章有兴趣可以去扒一下 最近正好用到s ...
- JavaWeb学习----JSP脚本元素、指令元素、动作元素
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- 为Unity项目生成文档(二)
Unity项目生成文档 接着上篇文章:为Unity项目生成文档(一) .Net项目可在VS配置XML 我们可以在VS中通过配置来生成xml文件,但是unity的project,就算同样配置了xml文档 ...