定义抽象节点类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. index merge的一次优化

    手机微博4040端口SQL优化 现象 某端口常态化延迟,通过使用pt-query-digest发现主要由于一条count(*)语句引发,具体如下: # .5s .58M rss, .84M vsz # ...

  2. 烂泥:通过vsphere给esxi添加本地硬盘

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 公司ESXi服务器的硬盘空间不够使用,现在新加了一块硬盘在ESxi服务器上.在服务器上添加完硬盘后,在Vsphere上是看不到新加硬盘的. 下面我们来通 ...

  3. 对Jena的简单理解和一个例子

    本文简单介绍Jena(Jena 2.4),使用Protégé 3.1(不是最新版本)创建一个简单的生物(Creature)本体,然后参照Jena文档中的一个例子对本体进行简单的处理,输出本体中的Cla ...

  4. Backbone模型

    现在进入最关键的组件 - 模型.模型用来存储应用的所有数据,以及直接和数据操作相关的逻辑.Backbone中的模型类是Backbone.Model,它包含了数据存储,数据验证,以及数据发生变动时触发相 ...

  5. NSThread基础使用

    1.创建和启动线程   一个NSThread对象就代表一条线程;   创建,启动线程 NSThread *thread = [[NSThread alloc] initWithTarget:self ...

  6. android使用微软雅黑字体

    android使用微软雅黑字体,需要下载字体 ttf文件 下载地址:http://download.csdn.net/detail/xiaoliu123586/9049815 放在assert,然后引 ...

  7. Sublime Text 2 (for OS X )配置成可以运行基于python3解释器的 .py文件

    Mac自带的python 其version是python 2.7 官网下的Sublime Text 2部署好了以后默认也是 为了使ST2 可以在command+B时可以运行基于python3的.py, ...

  8. linux中C语言获取高精度时钟gettimeofday函数

    前言:    在开发中,很多时候需要知道各个函数或者是某些设备对命令的操作用时,因此需要用到 gettimeofday 来获取当前时钟. 一,函数说明 #include  int gettimeofd ...

  9. ok,今天讲讲linux的部分指令吧

    Linux布置服务  cd 进入   ../      上一层目录    sh shutdown.sh   -------tomcat的关闭指令  sh startup.sh       ------ ...

  10. NOIP2010提高组 关押罪犯 -SilverN

    (洛谷P1525) 题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”( ...