好好理解一下DS吧^_^,下面是LinkedList,上学期学DS的时候写的 //ADT of list interface List{ public void clear(); public void insert(Object item); public void append(Object item); public Object remove(); public void setFirst(); public void next(); public void prev(); public int length(); public void setPos(int pso); public void setValue(Object val); public Object currValue(); public boolean isInList(); public void print(); } class Link{//节点类 private Object element; private Link next; Link(Object it,Link nextval){ element = it; next = nextval; } Link(Link nextval){ next = nextval; } Link next() {return next;} Link setNext(Link nextval) {return next = nextval;} Object element(){return element;} Object setElement(Object it){ return element = it;} } class LList implements List{//单链表 private Link head; private Link tail; protected Link curr; LList(int sz){setup();} LList(){setup();} private void setup(){ tail = head = curr = new Link(null); } public void clear(){ head.setNext(null); curr = tail = head; } public void insert(Object it){ Assert.notNull(curr,"No current element"); curr.setNext(new Link(it,curr.next())); if(tail == curr) tail = curr.next(); } public void append(Object it){ tail.setNext(new Link(it,null)); tail = tail.next(); }
public Object remove(){ if(! isInList()) return null; Object it = curr.next().element(); if(tail == curr.next()) tail = curr; curr.setNext(curr.next().next()); return it; } public void setFirst(){ curr = head; } public void next(){ if(curr != null) curr = curr.next(); }
public void prev(){ if((curr == null) || (curr == head)) {curr = null;return;} Link temp = head; while((temp != null) && (temp.next() != curr)) temp = temp.next(); curr = temp; } public int length(){ int cnt = 0; for(Link temp = head.next();temp != null;temp = temp.next()) cnt++; return cnt; } public void setPos(int pos){ curr = head; for(int i = 0;(curr != null) && (i < pos);i++) curr = curr.next(); } public void setValue(Object it){ Assert.notFalse(isInList(),"Not in List"); curr.next().setElement(it); } public Object currValue(){ if(! isInList()) return null; return curr.next().element(); } public boolean isEmpty(){ return head.next() == null; } public boolean isInList(){ return (curr != null) && (curr.next() != null); } public void print(){ if(isEmpty()) System.out.println("()"); else{ System.out.print("("); for(setFirst();isInList();next()) System.out.print(currValue() + " "); System.out.println(")"); } } }
粘一个我在学的书上(《Java How to program》)的例子。 // Fig. 22.3: List.java // Class ListNode and class List definitions package com.deitel.jhtp3.ch22;class ListNode { // package access data so class List can access it directly Object data; ListNode next; // Constructor: Create a ListNode that refers to Object o. ListNode( Object o ) { this( o, null ); } // Constructor: Create a ListNode that refers to Object o and // to the next ListNode in the List. ListNode( Object o, ListNode nextNode ) { data = o; // this node refers to Object o next = nextNode; // set next to refer to next } // Return a reference to the Object in this node Object getObject() { return data; } // Return the next node ListNode getNext() { return next; } }// Class List definition public class List { private ListNode firstNode; private ListNode lastNode; private String name; // String like "list" used in printing // Constructor: Construct an empty List with s as the name public List( String s ) { name = s; firstNode = lastNode = null; } // Constructor: Construct an empty List with // "list" as the name public List() { this( "list" ); } // Insert an Object at the front of the List // If List is empty, firstNode and lastNode will refer to // the same object. Otherwise, firstNode refers to new node. public synchronized void insertAtFront( Object insertItem ) { if ( isEmpty() ) firstNode = lastNode = new ListNode( insertItem ); else firstNode = new ListNode( insertItem, firstNode ); } // Insert an Object at the end of the List // If List is empty, firstNode and lastNode will refer to // the same Object. Otherwise, lastNode's next instance // variable refers to new node. public synchronized void insertAtBack( Object insertItem ) { if ( isEmpty() ) firstNode = lastNode = new ListNode( insertItem ); else lastNode = lastNode.next = new ListNode( insertItem ); } // Remove the first node from the List. public synchronized Object removeFromFront() throws EmptyListException { Object removeItem = null; if ( isEmpty() ) throw new EmptyListException( name ); removeItem = firstNode.data; // retrieve the data // reset the firstNode and lastNode references if ( firstNode.equals( lastNode ) ) firstNode = lastNode = null; else firstNode = firstNode.next; return removeItem; } // Remove the last node from the List. public synchronized Object removeFromBack() throws EmptyListException { Object removeItem = null; if ( isEmpty() ) throw new EmptyListException( name ); removeItem = lastNode.data; // retrieve the data // reset the firstNode and lastNode references if ( firstNode.equals( lastNode ) ) firstNode = lastNode = null; else { ListNode current = firstNode; while ( current.next != lastNode ) // not last node current = current.next; // move to next node
lastNode = current; current.next = null; } return removeItem; } // Return true if the List is empty public synchronized boolean isEmpty() { return firstNode == null; } // Output the List contents public synchronized void print() { if ( isEmpty() ) { System.out.println( "Empty " + name ); return; } System.out.print( "The " + name + " is: " ); ListNode current = firstNode; while ( current != null ) { System.out.print( current.data.toString() + " " ); current = current.next; } System.out.println( "\n" ); } }/************************************************************************** * (C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall. * * All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/
basic以前也不照样写吗?
指针灵活但难理解
提供指针只是多一种手段
并不是说没有指针就不型了
看来楼主很依赖指针
{
int stck[]=new int[10];
int tos;
stack()
{
tos=-1;
}
void push(int item)
{
if(tos==9)
{
System.out.println("Stack is full!");
}else{
stck[++tos]=item;
}
}
int pop()
{
if(tos<0)
{
System.out.println("Stack is underFlow!");
return 0;
}else{
return stck[tos--];
}
}
}
class testStack
{
public static void main(String[] args)
{
stack mystack1=new stack();
stack mystack2=new stack();
for(int i=0;i<10;i++) mystack1.push(i);
for(int i=10;i<20;i++) mystack2.push(i);
System.out.println("Stack in mystack1:");
for(int i=0;i<10;i++)
{
System.out.println(mystack1.pop());
}
System.out.println("Stack in mystack2:");
for(int i=0;i<10;i++)
{
System.out.println(mystack2.pop());
}
}
}
LinkedList类是封装好的链表结构
其他的可以看一下java库中关于集合的一些类
此问题相当于“汽车没有腿,如何移动?”。CSDN第一大蠢贴。别在这里丢人现眼啦...
//ADT of list
interface List{
public void clear();
public void insert(Object item);
public void append(Object item);
public Object remove();
public void setFirst();
public void next();
public void prev();
public int length();
public void setPos(int pso);
public void setValue(Object val);
public Object currValue();
public boolean isInList();
public void print();
}
class Link{//节点类
private Object element;
private Link next; Link(Object it,Link nextval){
element = it;
next = nextval;
}
Link(Link nextval){
next = nextval;
}
Link next() {return next;}
Link setNext(Link nextval) {return next = nextval;}
Object element(){return element;}
Object setElement(Object it){ return element = it;}
}
class LList implements List{//单链表
private Link head;
private Link tail;
protected Link curr; LList(int sz){setup();}
LList(){setup();} private void setup(){
tail = head = curr = new Link(null);
} public void clear(){
head.setNext(null);
curr = tail = head;
} public void insert(Object it){
Assert.notNull(curr,"No current element");
curr.setNext(new Link(it,curr.next()));
if(tail == curr)
tail = curr.next();
} public void append(Object it){
tail.setNext(new Link(it,null));
tail = tail.next();
}
public Object remove(){
if(! isInList()) return null;
Object it = curr.next().element();
if(tail == curr.next()) tail = curr;
curr.setNext(curr.next().next());
return it;
} public void setFirst(){
curr = head;
} public void next(){
if(curr != null)
curr = curr.next();
}
public void prev(){
if((curr == null) || (curr == head))
{curr = null;return;}
Link temp = head;
while((temp != null) && (temp.next() != curr))
temp = temp.next();
curr = temp;
} public int length(){
int cnt = 0;
for(Link temp = head.next();temp != null;temp = temp.next())
cnt++;
return cnt;
} public void setPos(int pos){
curr = head;
for(int i = 0;(curr != null) && (i < pos);i++)
curr = curr.next();
} public void setValue(Object it){
Assert.notFalse(isInList(),"Not in List");
curr.next().setElement(it);
} public Object currValue(){
if(! isInList()) return null;
return curr.next().element();
} public boolean isEmpty(){
return head.next() == null;
} public boolean isInList(){
return (curr != null) && (curr.next() != null);
} public void print(){
if(isEmpty()) System.out.println("()");
else{
System.out.print("(");
for(setFirst();isInList();next())
System.out.print(currValue() + " ");
System.out.println(")");
}
}
}
懒得回答你。 star821116(原来爱曾给我美丽心情)给出的是标准包里List的实现,不是叫你照着打一遍。不过鉴于你也不会懂什么叫封装,我也不多说了。
反正看到你这个SB的帖子就来气!你以后出来一次我就骂你一次。
链表是相对于线性表而言的
当然可以用数组来实现
不仅数据结构java版中有
你去看看C、C++版都有先好好看看数据结构吧
说到性能,汇编性能最好,是不是也要拿Java和汇编比一比啊?可笑可是Java实现了跨平台,别说汇编了,C/C++也不行你这是拿着Java天生缺憾在找事儿,与数据结构一点儿关系都没有
我喜欢楼主的坚韧
// Fig. 22.3: List.java
// Class ListNode and class List definitions
package com.deitel.jhtp3.ch22;class ListNode {
// package access data so class List can access it directly
Object data;
ListNode next; // Constructor: Create a ListNode that refers to Object o.
ListNode( Object o ) { this( o, null ); } // Constructor: Create a ListNode that refers to Object o and
// to the next ListNode in the List.
ListNode( Object o, ListNode nextNode )
{
data = o; // this node refers to Object o
next = nextNode; // set next to refer to next
} // Return a reference to the Object in this node
Object getObject() { return data; } // Return the next node
ListNode getNext() { return next; }
}// Class List definition
public class List {
private ListNode firstNode;
private ListNode lastNode;
private String name; // String like "list" used in printing // Constructor: Construct an empty List with s as the name
public List( String s )
{
name = s;
firstNode = lastNode = null;
} // Constructor: Construct an empty List with
// "list" as the name
public List() { this( "list" ); } // Insert an Object at the front of the List
// If List is empty, firstNode and lastNode will refer to
// the same object. Otherwise, firstNode refers to new node.
public synchronized void insertAtFront( Object insertItem )
{
if ( isEmpty() )
firstNode = lastNode = new ListNode( insertItem );
else
firstNode = new ListNode( insertItem, firstNode );
} // Insert an Object at the end of the List
// If List is empty, firstNode and lastNode will refer to
// the same Object. Otherwise, lastNode's next instance
// variable refers to new node.
public synchronized void insertAtBack( Object insertItem )
{
if ( isEmpty() )
firstNode = lastNode = new ListNode( insertItem );
else
lastNode = lastNode.next = new ListNode( insertItem );
} // Remove the first node from the List.
public synchronized Object removeFromFront()
throws EmptyListException
{
Object removeItem = null; if ( isEmpty() )
throw new EmptyListException( name ); removeItem = firstNode.data; // retrieve the data // reset the firstNode and lastNode references
if ( firstNode.equals( lastNode ) )
firstNode = lastNode = null;
else
firstNode = firstNode.next; return removeItem;
} // Remove the last node from the List.
public synchronized Object removeFromBack()
throws EmptyListException
{
Object removeItem = null; if ( isEmpty() )
throw new EmptyListException( name ); removeItem = lastNode.data; // retrieve the data // reset the firstNode and lastNode references
if ( firstNode.equals( lastNode ) )
firstNode = lastNode = null;
else {
ListNode current = firstNode; while ( current.next != lastNode ) // not last node
current = current.next; // move to next node
lastNode = current;
current.next = null;
} return removeItem;
} // Return true if the List is empty
public synchronized boolean isEmpty()
{ return firstNode == null; } // Output the List contents
public synchronized void print()
{
if ( isEmpty() ) {
System.out.println( "Empty " + name );
return;
} System.out.print( "The " + name + " is: " ); ListNode current = firstNode; while ( current != null ) {
System.out.print( current.data.toString() + " " );
current = current.next;
} System.out.println( "\n" );
}
}/**************************************************************************
* (C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall. *
* All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
真不像http://expert.csdn.net/Expert/topic/2629/2629891.xml?temp=.1389582java完了是Basic,我倒觉得他只是在挖坑
至于我们灌的水,他估计一点儿都不看
有问题就问不是好的学习方法
实现数据结果和算法不一定要用指针,在java中,我们用“句柄”
这就相当于指针。但比指针好,因为有类型检查。灌水:
对于楼主,一开始我还以为楼主做了名人后变谦虚了,也想附和一下叫大家不要再攻击他
不过看了
http://expert.csdn.net/Expert/topic/2629/2629891.xml?temp=.1389582
觉得呀,唉...“30分的大帖竟然没人回?是没人会?”江山依旧在,几度夕阳红啊。
书上用静态数组举个例子,你就只会用静态数组了?FT死了,当时我面试的时候还没有想过Java怎么实现数据结构呢,面试的人就让我想一下用Java怎么实现树结构还不是二叉树,照你这么说,我直接回答书上没教算了楼主不是一直吹嘘有思想吗?
http://expert.csdn.net/Expert/topic/2276/2276584.xml?temp=.8083155怎么这会儿就?汗 -_-b
还有错啊,这不是有这么多人给答案了么,在这里就要学会厚脸皮,goodluck!!