链表
程序代码
class Node {
Object item; Node next;
Node (Object v) {
item = v; next = null;
}
}
头指针,空尾指针
初始化:head = null;
在x后插入t:
if ( x == null)
{ head = t; head.next = null; }
else { t.next = x.next; x.next = t; } <script type="text/javascript">google_ad_client = "pub-4475724770859924";google_alternate_color = "FFBBE8";google_ad_width = 468;google_ad_height = 60;google_ad_format = "468x60_as";google_ad_type = "text_image";google_ad_channel ="9379930647";google_color_border = "F0F0F0";google_color_bg = "FFFFFF";google_color_link = "FF6FCF";google_color_url = "38B63C";google_color_text = "B3B3B3";</script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
移走x之后的结点:t = x.next; x.next = t.next;
循环遍历:for ( t = head; t != null; t = t.next )
检查链表是否为空:if ( head == null )
空头结点,空尾指针
初始化:head = new Node(); head.next = null;
在x后插入t:t.next = x.next; x.next = t;
移走x之后的结点:t = x.next; x.next = t.next;
循环遍历:for ( t = head.next; t != null; t = t.next )
检查链表是否为空:if ( head.next == null )
空头结点,空尾结点
初始化:head = new Node(); z = new Node(); head.next = z; z.next = z;
在x后插入t:t.next = x.next; x.next = t;
移走x之后的结点:t = x.next; x.next = t.next;
循环遍历:for ( t = head.next; t != z; t = t.next )
检查链表是否为空:if ( head.next == z )
循环链表
第一次插入:head.next = head;
在x后插入t:t.next = x.next; x.next = t;
移走x之后的结点:t = x.next; x.next = t.next;
循环遍历:t = head; do { t = t.next; } while ( t != head );
检查是否只有一个数据项:if ( head.next == head )
堆栈
数组实现
程序代码
class Stack {
private Object[] s;
private int n;
Stack ( int maxN ) {
s = new Object[maxN]; n = 0;
}
boolean isEmpty() { return ( n == 0 ); }
void push ( Object item ) { s[n++] = item; }
Object pop() {
Object t = s[--n]; s[n] = null; return t;
}
}
链表实现
class Stack {
private Node head;
private class Node {
Object item; Node next;
Node ( Object item, Node next ) {
this.item = item; this.next = next;
}
}
Stack ( Object maxN ) { head = null; }
boolean isEmpty() { return ( head ==null ); }
void push ( Object item ) { head = new Node(item, head); }
Object pop() {
Object v = head.item;
Node t = head.next;
head = t;
return v;
}
}
FIFO队列的链表实现
程序代码
class Queue {
private class Node {
Object item; Node next;
Node ( Object item ) {
this.item = item; this.next = null;
}
}
Private Node head, tail;
Queue ( Object max ) { head = null; tail = null; }
boolean isEmpty() { return ( head ==null ); }
void put ( Object item ) {
Node t = tail;
tail = new Node(item);
if ( empty() )
head = tail;
else t.next = tail
}
Object get() {
Object v = head.item;
Node t = head.next;
head = t;
return v;
}
}
本人只试了这几种数据结构,如果你有兴趣,不妨尝试一下别的结构,譬如多线程的控制之类
相关书籍:《Java数据结构和算法(第二版)》
书中内容有:
<script type="text/javascript">google_ad_client = "pub-4475724770859924";google_alternate_color = "FFBBE8";google_ad_width = 468;google_ad_height = 60;google_ad_format = "468x60_as";google_ad_type = "text_image";google_ad_channel ="9379930647";google_color_border = "F0F0F0";google_color_bg = "FFFFFF";google_color_link = "FF6FCF";google_color_url = "38B63C";google_color_text = "B3B3B3";</script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
通过由基于JAVA的演示所组成的可视专题讨论来掌握数据结构和算法
学会如何为常见和不太常见的编程条件选择正确的算法
利用数据结构和算法为现实世界的处理过程建模
了解不同的数据结构的优势和弱点,考虑如何利用它们改进编程的效率
学会如何用面向对象的编程简化数据结构和算法
本书以一种易懂的方式教授如何安排和操纵数据的问题,其中不乏一些难题;了解这些知识以期使计算机的应用获得最好的表现。
不管使用何种语言或平台,掌握了数据结构和算法将改进程序的质量和性能。
书中提供了一套独创的可视讨论专题用以阐明主要的论题;它使用JAVA语言说明重要的概念,而避免了C/C++语言的复杂性,以便集中精力论述数据结构和算法。
经验丰富的作者Robert Lafore先生提供了许多简单明了的例子,避免了对于这类命题常见的冗长、繁琐的数学证明。在第二版中,他利用Java语言最新特性,修改并扩充了他的例子。在每一章后都有问题和练习,使读者有机会测试自己的理解程序。
书中介绍了计算机编程中使用的数据结构和算法,对于在计算机应用中如何操作和管理数据以取得最优性能提供了深入浅出的讲解。
全书共分为15章,分别讲述了基本概念、数组、简单排序、堆和队列、链表、递归、进阶排序、二叉树、红黑树、哈希表及图形等知识。附录中则提供了运行专题Applet和例程、相关书籍和问题解答.
《Java数据结构和算法(第二版)》为中文版,有英文版的。
书中全部源代码都可以从Web站点:
http://www.samspublishing.com/下载
这里也可以下载《Java数据结构和算法(第二版)》源代码。
http://www.jspcn.net/software/20058313/15768.rar
《Java 数据结构与面向对象编程基础》
书中书以Java GUI编程为描述方法,以UML为建模工具,应用面向对象的编程方法研究经典数据结构。《Java 数据结构与面向对象编程基础》全书分两大部分18章。第一部分是第1到8章,介绍了面向对象的编程方法和Java GUI编程方法。第二部分包括第9到18章,着重讨论了一些经典的数据结构,并配有精心设计的实验程序以加强读者对基本概念和原理的理解。附录介绍了UML、算法复杂度的简单概念以及本书所用到的Foundations类库的安装与使用。