刚看了Java泛型编程 以前写过C++的模板感觉还好
把代码贴出来 希望大家来指点其中 设计和细节的好坏下面的是带头节点的List模板,不足不好的大家说 class List<T extends ListNode> { /**
*带头节点的list,头节点没有用于存放数据
*/
T head;
T current;
T prior;
T last; //指向最后的元素
public List(T headnode){//传入头节点,第二个节点才是真正的数据开始
head = headnode;
current = head;
prior = head;
last = head;
} public boolean isEmpty(){
return head == last ? true: false;
}
public boolean isStartOfList(){
return current == head ? true : false;
}
public boolean isEndOfList(){
return current == null ? true:false;
}
public boolean isPriorNullErr(){
return prior == null ? true : false;
}
public T hasNext(){
if(isEmpty() || isEndOfList() || isPriorNullErr()){
return null;
}
else{
if(!isStartOfList() || current != prior){
prior = current;
}
current = (T) current.getNext();//后移指针再返回 ,因为list带头节点
return current;
}
} public boolean removeCurrent(){ if(this.isEndOfList() || isPriorNullErr()){
return false;
}
else{
prior.setNext(current.getNext());
current = prior; //指针回退,防止接着hasNext();而漏掉节点
return true;
}
}
public void rollIndextoHead(){
current = head;
prior = head;
}
public void insertFront(T newnode){
if(this.isEmpty()){
head.setNext(newnode);
last = newnode;
}else{
newnode.setNext(head.getNext());
head.setNext(newnode);
}
}
public void insetBack(T newnode){
last.setNext(newnode);
last = newnode;
}
public T removeFront(){
if(this.isEmpty()){
return null;
}else{
T temp = (T)head.getNext();//这儿有个警告,不用@SuppressWarnings("unchecked")
//这句话这么消除
head.setNext(temp.getNext());
return temp;
}
}
// public T removeBack(){}
public void printList(){
System.out.println(this.head.toString());
T temp = hasNext();
while(temp != null){
System.out.println(temp.toString());
temp = this.hasNext();
}
}
}
interface ListNode{
public boolean setNode(ListNode listnode);
public void setNext(ListNode next);
public ListNode getNode();
public ListNode getNext();
public String toString();
}
把代码贴出来 希望大家来指点其中 设计和细节的好坏下面的是带头节点的List模板,不足不好的大家说 class List<T extends ListNode> { /**
*带头节点的list,头节点没有用于存放数据
*/
T head;
T current;
T prior;
T last; //指向最后的元素
public List(T headnode){//传入头节点,第二个节点才是真正的数据开始
head = headnode;
current = head;
prior = head;
last = head;
} public boolean isEmpty(){
return head == last ? true: false;
}
public boolean isStartOfList(){
return current == head ? true : false;
}
public boolean isEndOfList(){
return current == null ? true:false;
}
public boolean isPriorNullErr(){
return prior == null ? true : false;
}
public T hasNext(){
if(isEmpty() || isEndOfList() || isPriorNullErr()){
return null;
}
else{
if(!isStartOfList() || current != prior){
prior = current;
}
current = (T) current.getNext();//后移指针再返回 ,因为list带头节点
return current;
}
} public boolean removeCurrent(){ if(this.isEndOfList() || isPriorNullErr()){
return false;
}
else{
prior.setNext(current.getNext());
current = prior; //指针回退,防止接着hasNext();而漏掉节点
return true;
}
}
public void rollIndextoHead(){
current = head;
prior = head;
}
public void insertFront(T newnode){
if(this.isEmpty()){
head.setNext(newnode);
last = newnode;
}else{
newnode.setNext(head.getNext());
head.setNext(newnode);
}
}
public void insetBack(T newnode){
last.setNext(newnode);
last = newnode;
}
public T removeFront(){
if(this.isEmpty()){
return null;
}else{
T temp = (T)head.getNext();//这儿有个警告,不用@SuppressWarnings("unchecked")
//这句话这么消除
head.setNext(temp.getNext());
return temp;
}
}
// public T removeBack(){}
public void printList(){
System.out.println(this.head.toString());
T temp = hasNext();
while(temp != null){
System.out.println(temp.toString());
temp = this.hasNext();
}
}
}
interface ListNode{
public boolean setNode(ListNode listnode);
public void setNext(ListNode next);
public ListNode getNode();
public ListNode getNext();
public String toString();
}
写泛型的警告是有的,因为有关于类型的很多,转化时有问题
可以用@SuppressWarnings("unckecked”)
lz用接口表示结点,不大合理,结点包含数据域和指针域,应该用类表示吧而头结点是list本身就有的,干嘛还要传递给构造函数呢.
还有lz的list是这样定义的,跟一般的list不一样呢,一般的list就是插,删,改,索引,查找几个操作.
下面是thingink in java 中链式栈的实现,非常简洁,贴出来下:
public class LinkedStack<T> {
private static class Node<U> {
U item;
Node<U> next;
Node() { item = null; next = null; }
Node(U item, Node<U> next) {
this.item = item;
this.next = next;
}
boolean end() { return item == null && next == null; }
}
private Node<T> top = new Node<T>(); // End sentinel
public void push(T item) {
top = new Node<T>(item, top);
}
public T pop() {
T result = top.item;
if(!top.end())
top = top.next;
return result;
}
public static void main(String[] args) {
LinkedStack<String> lss = new LinkedStack<String>();
for(String s : "Phasers on stun!".split(" "))
lss.push(s);
String s;
while((s = lss.pop()) != null)
System.out.println(s);
}
} /* Output:
哦 头节点的设计的确不好 那我要想很好的删去当前current节点 这么实现才比较好呢
以前学C++的思想的确有很大影响
用接口写节点是为了 实现这一接口的类 都能作为 List的节点
链表排序嘛 可以在List 里加 值域都可以得到 排序就简单了
hasNext得到的是ListNode类型,父类能访问到子类的定义的值域吗?
类似下面的:
class LinkNode <T>{
private T data;
private Node next;
private Node prior;
public Node(T data) {
this.data = data;
this.next = null;
}
public void setData(T data) {
this.data = data;
}
public T getData() {
return data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Node getPrior(){
return prior;
}
public void setPrior(Node prior){
this.prior=prior;
}
//下面还可以加一些其它的方法:
//
}
这样写貌似可以把功能写很强大啊
好像有点多余啊?
看样子是学C++学多了
return current == head
而且,用==比较,有点晕,建议用equals
java 编程思想里可以查阅相关资料
List<Integer,IntNode<Integer>> list = new List<Integer,IntNode<Integer>>();
System.out.println(list.toString());
}
}/**
* 该类中有两个参数类型未知.
* 1: ListNode中包含的数据类型参数
* 2:实现ListNode接口的具体类
*
* ListNode中包含的数据类型未知,用T表示。
* List中包含的元素类型未知用E表示。
*
* 如:
* class IntNode<T> implements ListNode<T>{
* ...
* }
* 使用:(好比你将Map放到List中,或者将List放到Map中。)
* List<Integer,IntNode<Integer>> list = new List<Integer,IntNode<Integer>>();
* System.out.println(list.toString());
*/
class List<T,E extends ListNode<T>> { /**
*带头节点的list,头节点没有用于存放数据
*/
ListNode<T> head;
ListNode<T> current;
ListNode<T> prior;
ListNode<T> last; //指向最后的元素
public List(){}
public List(ListNode<T> headnode){//传入头节点,第二个节点才是真正的数据开始
head = headnode;
current = head;
prior = head;
last = head;
} public boolean isEmpty(){
return head == last ? true: false;
}
public boolean isStartOfList(){
return current == head ? true : false;
}
public boolean isEndOfList(){
return current == null ? true:false;
}
public boolean isPriorNullErr(){
return prior == null ? true : false;
}
public ListNode<T> hasNext(){
if(isEmpty() || isEndOfList() || isPriorNullErr()){
return null;
}
else{
if(!isStartOfList() || current != prior){
prior = current;
}
current = current.getNext();//后移指针再返回 ,因为list带头节点
return current;
}
} public boolean removeCurrent(){ if(this.isEndOfList() || isPriorNullErr()){
return false;
}
else{
prior.setNext(current.getNext());
current = prior; //指针回退,防止接着hasNext();而漏掉节点
return true;
}
}
public void rollIndextoHead(){
current = head;
prior = head;
}
public void insertFront(E newnode){
if(this.isEmpty()){
head.setNext(newnode);
last = newnode;
}else{
newnode.setNext(head.getNext());
head.setNext(newnode);
}
}
public void insetBack(E newnode){
last.setNext(newnode);
last = newnode;
}
public ListNode<T> removeFront(){
if(this.isEmpty()){
return null;
}else{
ListNode<T> temp = head.getNext();//这儿有个警告,不用@SuppressWarnings("unchecked")
//这句话这么消除
head.setNext(temp.getNext());
return temp;
}
}
// public T removeBack(){}
public void printList(){
System.out.println(this.head.toString());
ListNode<T> temp = hasNext();
while(temp != null){
System.out.println(temp.toString());
temp = this.hasNext();
}
}
}//ListNode中包含的数据类型未知,用T表示。
interface ListNode<T>{
public boolean setData(T data);
public void setNext(ListNode<T> next);
public T getData();
public ListNode<T> getNext();
public String toString();
}class IntNode<T> implements ListNode<T>{ public T getData() {
// TODO Auto-generated method stub
return null;
} public ListNode<T> getNext() {
// TODO Auto-generated method stub
return null;
} public boolean setData(T data) {
// TODO Auto-generated method stub
return false;
} public void setNext(ListNode<T> next) {
// TODO Auto-generated method stub
}
}
这个比喻不好,很糟糕,等待大家更贴切的比喻。