(三)使用链式数据实现包(java)
目标:
1) 描述数据的链式组织方式
2) 描述如何在链式节点链的开头添加新节点
3) 描述如何删除链式节点链的首节点
4) 描述如何在链式节点链中找到某个数据
5) 使用链式节点链实现ADT包
6) 描述基于数组实现和链式实现的ADT包的不同
3. 使用链式数据实现包
3.1链式数据
添加到开头形成一个链
3.2 ADT包的链式实现
3.2.1 私有类Node
节点(node),data域:数据部分(data portion),next域:链接部分(link portion)
Node是ADT包的实现细节,应该对包的客户隐藏。一种方法是定义在包中,且含在实现包的类中。
private class Node{
private T data; // Entry in bag
private Node next; // Link to next node private Node(T dataPortion) {
this(dataPortion, null);
} // end constructor private Node(T dataPortion, Node nextNode) {
data = dataPortion;
next = nextNode;
} // end constructor
} // end Node
3.2.2 类LinkedBag的框架
头引用(head reference)的数据域来保存指向第一个节点的引用,第二个数据域可以记录包中项的个数,即链中节点的个数。
/**
* A class of bags whose entries are stored in a chain of linked nodes.
* The bag is never full
* @author Administrator
*
* @param <T>
*/
public class LinkedBag<T> implements BagInterface<T> {
private Node firstNode; // Reference to first node
private int numberOfEntries; public LinkedBag() {
firstNode = null;
numberOfEntries = 0;
} // end default constructor @Override
public int getCurrentSize() {
// TODO Auto-generated method stub
return 0;
} @Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
} @Override
public boolean add(Object newEntry) {
// TODO Auto-generated method stub
return false;
} @Override
public Object remove() {
// TODO Auto-generated method stub
return null;
} @Override
public boolean remove(Object anEntry) {
// TODO Auto-generated method stub
return false;
} @Override
public void clear() {
// TODO Auto-generated method stub
} @Override
public int getFrequencyOf(Object anEntry) {
// TODO Auto-generated method stub
return 0;
} @Override
public boolean contains(Object anEntry) {
// TODO Auto-generated method stub
return false;
} @Override
public Object[] toArray() {
// TODO Auto-generated method stub
return null;
} private class Node{
private T data; // Entry in bag
private Node next; // Link to next node
private Node(T dataPortion) {
this(dataPortion, null);
} // end constructor private Node(T dataPortion, Node nextNode) {
data = dataPortion;
next = nextNode;
} // end constructor
} // end Node
}
注意:没有定义initialized变量表示初始化状态。
3.2.3 定义一些核心方法
方法add:
/**
* Adds a new entry to this bag.
* @param newEntry: The object to be added as a new entry.
* @return: True if the addition is successful, or false if not.
*/
@Override
public boolean add(T newEntry) { // OutOfMemoryError posiible
Node newNode = new Node(newEntry);
newNode.next = firstNode; // Make new node reference test of chain
// (firstNode is null if chain is empty)
firstNode = newNode; // New node is at beginning of chain
numberOfEntries++;
return true;
} // end add
安全说明:内层类Node应该执行安全检查吗?
因为Node是私有内层类,所以将它看做外层类LinkedBag的实现细节。因此,让LinkedBag负责所有的安全检查。另外,注意Node的构造方法只做了简单的赋值,并没有抛出异常,即使不是这种情况,Node抛出了异常,LinkedBag也应该能处理它。
安全说明:类LinkedBag应该执行安全检查吗?
默认构造方法只进行了两个简单的赋值。实际上,所赋的值与省略构造方法时使用默认的赋值是一样的值。这些赋值不会失败。add分配新的节点,如果没有足够的内存空间,这个分配可能会失败,OutOfMemoryError,链完好无损且保持不变。或者是空的或者含有之前的节点。若客户捕获这个异常并处理,这样操作属于恰当。因为任何LinkedBag对象的完整性已经得到维护,所以不需要为类ArrayBag添加的那些安全检查。
方法toArray:
/**
* Retrieves all entries that are in this bag.
* @return: A newly allocated array of all the entries in the bag.
* Note: If the bag is empty, the returned array is empty.
*/
@Override
public T[] toArray() {
// The cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] result = (T[])new Object[numberOfEntries]; // Unchecked cast
int index = 0;
Node currentNode = firstNode;
while((index < numberOfEntries) && (currentNode != null)) {
result[index] = currentNode.data;
index++;
currentNode = currentNode.next;
} // end while
return result;
} // end toArray
3.2.4 测试核心方法
/**
* A test of the methods add, toArray, isEmpty, and getCurrentSize,
* as defined in the first draft of the class LinkedBag
* @author Administrator
*
*/
public class LinkedBagDemo1 { public static void main(String[] args) {
System.out.println("Creating an empty bag:");
BagInterface<String> aBag = new LinkedBag<>();
testIsEmpty(aBag, true);
displayBag(aBag); String[] contentOfBag = {"A", "D", "B", "A", "C", "A", "D"};
testAdd(aBag, contentOfBag);
testIsEmpty(aBag, false);
} public static void testAdd(BagInterface<String> bag, String[] content) {
System.out.println("Testing the add method:");
for (int index = 0; index < content.length; index++) {
if(bag.add(content[index])) {
System.out.print(content[index] + " ");
} // end if
} // end for
System.out.println();
displayBag(bag);
} // end testAdd private static void displayBag(BagInterface<String> bag) {
System.out.println("the bag contains " + bag.getCurrentSize() +
" string(s), as follows:");
Object[] bagArray = bag.toArray();
for (int index = 0; index < bagArray.length; index++) {
System.out.print(bagArray[index] + " ");
} // end for
System.out.println();
} // end displayBag private static void testIsEmpty(BagInterface<String> aBag, boolean correctResult) {
System.out.println("Testing isEmpty with ");
if(correctResult) {
System.out.println("an empty bag:");
}
else {
System.out.println("a bag that is not empty:");
} // end if System.out.print("isEmpty finds the bag ");
if(correctResult && aBag.isEmpty()) {
System.out.println("empty: OK.");
}
else if(correctResult) {
System.out.println("not empty, but it is empty: ERROR.");
}
else if(!correctResult && aBag.isEmpty()) {
System.out.println("empty, but it is not empty: ERROR.");
}
else {
System.out.println("not empty: OK.");
} // if else System.out.println();
} // end testIsEmpty
}
3.2.5 方法getFrequencyOf
/**
* Counts the number of times a given entry appears in this bag.
* @param anEntry: The entry to counted.
* @return: The number of times anEntry appears in the bag.
*/
@Override
public int getFrequencyOf(T anEntry) {
Node currentNode = firstNode;
int counter = 0;
int loopCounter = 0;
while((loopCounter < numberOfEntries) && (currentNode != null)) {
if(currentNode.data.equals(anEntry)) {
counter++;
} // end if
loopCounter++;
currentNode = currentNode.next;
} // end while
return counter;
} // end getFrequencyOf
3.2.6 方法contains
/**
* Tests whether this bag contains a given entry.
* @param anEntry: The entry to locate.
* @return: True if the bag contains anEntry, or false if not.
*/
@Override
public boolean contains(T anEntry) {
Node currentNode = firstNode;
boolean found = false;
while(!found && (currentNode != null)) {
if(currentNode.data.equals(anEntry)) {
found = true;
break;
}
else {
currentNode = currentNode.next;
} // end if
} // end while
return false;
} // end contains
3.3 从链中删除一项
删除未指定项:remove()
/**
* Removes one unspecified entry from this bag, if possible.
* @return: Either the removed entry, if the removel was successful, or null.
*/
@Override
public T remove() {
T result = null;
if(firstNode != null) {
result = firstNode.data;
firstNode = firstNode.next;
numberOfEntries--;
} // end if
return result;
} // end remove
删除指定项:remove(T)
删除思想与Array相同,用首节点的值将其替换,然后firstNode=firstNode.next。
/**
* Removes one occurrence of a given entry from this bag, if possible.
* @param anEntry: The entry to be removed.
* @return: True if the removal was successful, or false if not.
*/
@Override
public boolean remove(T anEntry) {
boolean result = false;
Node nodeN = getReferenceTo(anEntry);
if(nodeN != null) {
nodeN.data = firstNode.data;
firstNode = firstNode.next;
numberOfEntries--;
result = true;
} // end if
return result;
} // end remove // Locates a given entry within this bag.
// Returns a reference to the node containing the entry, if located,
// or null otherwise.
private Node getReferenceTo(T anEntry) {
Node currentNode = firstNode;
while(currentNode != null) {
if(currentNode.data.equals(anEntry)) {
break;
} // end if
currentNode = currentNode.next;
} // end while
return currentNode;
} // end getReferenceTo
方法clear:
/**
* Removes all entries from this bag.
*/
@Override
public void clear() {
while(!isEmpty()) {
remove();
} // end while
} // end clear
设计决策:LinkedBag应该限制包的容量吗?
LinkedBag每次增加一个节点,如果添加失败,则现有的链保持不变。若希望在有限的内存状态下使用LinkedBag,仍可以限制LinkedBag的容量。
3.4 使用链实现ADT包的优缺点
最大的优点是链可以按需求来改变大小,所以包也是如此。只要内存可用,可以在链中添加想要的任意多的节点。可以删除并回收不再需要的节点。(数组需要更大的数组且需要复制)。添加新项到数组尾货链头都简单(除过数组调大小),删除一项也都简单,指定项都需要查找。对于相同的长度,链比数组需要更大的内存。但是数组常比所需大,也有内存浪费,链按需使用。
(三)使用链式数据实现包(java)的更多相关文章
- 链式二叉树的实现(Java)
定义树节点: package 链式二叉树; public class TreeNode { private Object data; private TreeNode left; private Tr ...
- C++ 运算符重载三(链式编程)
//运算符重载之链式编程 #include<iostream> using namespace std; //对于友元函数重载运算符只适用于左操作数是系统变量的场景 //因为成员无法在系统 ...
- Python特色的序列解包、链式赋值、链式比较
一.序列解包 序列解包(或可迭代对象解包):解包就是从序列中取出其中的元素的过程,将一个序列(或任何可迭代对象)解包,并将得到的值存储到一系列变量中. 一般情况下要解包的序列包含的元素个数必须与你在等 ...
- 第4.7节 Python特色的序列解包、链式赋值、链式比较
一.序列解包 序列解包(或可迭代对象解包):解包就是从序列中取出其中的元素的过程,将一个序列(或任何可迭代对象)解包,并将得到的值存储到一系列变量中. 一般情况下要解包的序列包含的元素个数必须与你在等 ...
- PHP设计模式:类自动载入、PSR-0规范、链式操作、11种面向对象设计模式实现和使用、OOP的基本原则和自动加载配置
一.类自动载入 SPL函数 (standard php librarys) 类自动载入,尽管 __autoload() 函数也能自动加载类和接口,但更建议使用 spl_autoload_registe ...
- 数据结构Java实现07----队列:顺序队列&顺序循环队列、链式队列、顺序优先队列
一.队列的概念: 队列(简称作队,Queue)也是一种特殊的线性表,队列的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置插入和删除,而队列只允许在其一端进行插入操作在其 ...
- 数据结构Java实现05----栈:顺序栈和链式堆栈
一.堆栈的基本概念: 堆栈(也简称作栈)是一种特殊的线性表,堆栈的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置进行插入和删除操作,而堆栈只允许在固定一端进行插入和删除 ...
- 数据结构Java实现03----栈:顺序栈和链式堆栈
一.堆栈的基本概念: 堆栈(也简称作栈)是一种特殊的线性表,堆栈的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置进行插入和删除操作,而堆栈只允许在固定一端进行插入和删除 ...
- Java实现链式存储的二叉查找树(递归方法)
二叉查找树的定义: 二叉查找树或者是一颗空树,或者是一颗具有以下特性的非空二叉树: 1. 若左子树非空,则左子树上所有节点关键字值均小于根节点的关键字: 2. 若右子树非空,则右子树上所有节点关键字值 ...
随机推荐
- 授权oAuth
使用Client Credentials Grant授权方式给客户端发放access token 只验证客户端(Client),不验证用户(Resource Owner),只要客户端通过验证就发acc ...
- 当图片加载失败时更换图片, Firefox onerror 报错
当图片加载失败时更换图片. <!DOCTYPE html> <meta charset="UTF-8"> <img src="http:// ...
- Excel 导出通用类
public class ExportToExcelHelper { public static void ExportExcel(DataTable dt) { try { //创建一个工作簿 IW ...
- 深入浅析Spring的AOP实现原理
转载来源:https://www.jb51.net/article/81788.htm AOP(Aspect-OrientedProgramming,面向切面编程),可以说是OOP(Object-Or ...
- “ORA-06550: 第 1 行, 第 7 列”解决方法
将本机能正常运行的维修生产日志代码发布到公司内测环境里无法正常运行,报错如下: execute() - pls–QuartzJob.java–quartzjob 开始执行! java.sql.SQLE ...
- bufferedReader中的数据, 只是读过一次, 就没有了(拿走,自然就没了),只能读一次( load, readLine 等只要是读操作)
- leecode第一百四十一题(环形链表)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- 用aws cli 下载s3中数据到本地
参考https://blog.csdn.net/DynastyRumble/article/details/76649120 1 首先注册AWS账户,绑定信用卡.一定要填写正确的手机,因为验证方式是它 ...
- Codeforces 1076 E - Vasya and a Tree
E - Vasya and a Tree 思路: dfs动态维护关于深度树状数组 返回时将当前节点的所有操作删除就能保证每次访问这个节点时只进行过根节点到当前节点这条路径上的操作 代码: #pragm ...
- Codeforces 985 F - Isomorphic Strings
F - Isomorphic Strings 思路:字符串hash 对于每一个字母单独hash 对于一段区间,求出每个字母的hash值,然后排序,如果能匹配上,就说明在这段区间存在字母间的一一映射 代 ...