一个结点和链表类 import java.awt.*; import java.applet.*; class Node { private int Data; private Node Next; public Node(int x) { Data=x; Next=null; } public Node(int x,Node next) { Data=x; Next=next; } public void setData(int x) { Data=x; } public int getData() { return Data; } public void setNext(Node NewNode) { Next=NewNode;
} public Node getNext() { return Next; }
} class List { private Node FirstNode; public List() { FirstNode=null; } public List(int data) { FirstNode=new Node(data); } //±éÀú public String visitAll() { Node head=FirstNode; String str=""; if(head==null) return str; else do { str+=head.getData()+";"; } while((head=head.getNext())!=null); return str; } public void insertAtBegin(int data) { if(FirstNode==null) FirstNode=new Node(data); else FirstNode=new Node(data,FirstNode); } public void addAtEnd(int data) { Node head=FirstNode; if(FirstNode!=null) { while(head.getNext()!=null) { head=head.getNext(); } head.setNext(new Node(data)); } else FirstNode=new Node(data); } //delete the first node whose data is id public boolean removeAtId(int id) { Node head=FirstNode; Node fellow; if(head==null) System.out.println("The list is empty!"); else { while(head.getData()!=id) { fellow=head; head=head.getNext(); } } if(head==null) return false; else { //fellow.setNext(head.getNext()); return true; } } public void removeAll() { FirstNode=null; } }
当然可以自己写啦。 如:实现队列 interface Queue{ void add(Object obj); Object remove(); int size(); } class LinkedListQueue implements Queue{ LinkedListQueue(){...} public void add(Object obj){...} public Object remove(){...} public int size(){...} private Link head; private Link tail; }
stack:如下
import com.bruceeckel.simpletest.*;
import java.util.*;
import com.bruceeckel.util.*;public class StackL {
private LinkedList list = new LinkedList();
public void push(Object v) {
list.addFirst(v);
}
public Object top() { return list.getFirst(); }
public Object pop() {
return list.removeFirst();
}
public static void main(String[] args) {
SimpleTest monitor =
new SimpleTest("StackL");
StackL stack = new StackL();
for(int i = 0; i < 10; i++)
stack.push(Collections2.countries.next());
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
monitor.expect(new String[] {
"CHAD",
"CHAD",
"CHAD",
"CENTRAL AFRICAN REPUBLIC",
"CAPE VERDE"
});
}
} ///:~
JAVA没有指针好像对处理这些动态结构的能力就要差一点了。请各位大侠给我推荐几本这方面的书好吗?
import java.awt.*;
import java.applet.*;
class Node
{
private int Data;
private Node Next;
public Node(int x)
{
Data=x;
Next=null;
}
public Node(int x,Node next)
{
Data=x;
Next=next;
}
public void setData(int x)
{
Data=x;
}
public int getData()
{
return Data;
}
public void setNext(Node NewNode)
{
Next=NewNode;
}
public Node getNext()
{
return Next;
}
} class List
{
private Node FirstNode;
public List()
{
FirstNode=null;
}
public List(int data)
{
FirstNode=new Node(data);
}
//±éÀú
public String visitAll()
{
Node head=FirstNode;
String str="";
if(head==null)
return str;
else
do
{
str+=head.getData()+";";
}
while((head=head.getNext())!=null);
return str;
}
public void insertAtBegin(int data)
{
if(FirstNode==null)
FirstNode=new Node(data);
else
FirstNode=new Node(data,FirstNode);
}
public void addAtEnd(int data)
{
Node head=FirstNode;
if(FirstNode!=null)
{
while(head.getNext()!=null)
{
head=head.getNext();
}
head.setNext(new Node(data));
}
else
FirstNode=new Node(data);
}
//delete the first node whose data is id
public boolean removeAtId(int id)
{
Node head=FirstNode;
Node fellow;
if(head==null)
System.out.println("The list is empty!");
else
{
while(head.getData()!=id)
{
fellow=head;
head=head.getNext();
}
}
if(head==null)
return false;
else
{
//fellow.setNext(head.getNext());
return true;
}
}
public void removeAll()
{
FirstNode=null;
}
}
如:实现队列
interface Queue{
void add(Object obj);
Object remove();
int size();
}
class LinkedListQueue implements Queue{
LinkedListQueue(){...}
public void add(Object obj){...}
public Object remove(){...}
public int size(){...} private Link head;
private Link tail;
}