链表的简单的增加结点与打印:
范例:
class Node{
private String data ;
private Node next ;
public Node(String data){
this.data = data ;
}
public void setNext(Node next){
this.next = next;
}
public Node getNext(){
return this.next ;
}
public String getData(){
return this.data ;
}
public void addNode(Node node){
if(this.next == null){
this.next = node ;
}else{
this.next.addNode(node) ;
}
}
public void printNode(){
System.out.println(this.data) ;
if(this.next != null){
this.next.printNode();
}
}
}
class Link{
private Node root ;
public void add(String data){
Node newNode = new Node(data) ;
if(root == null)
this.root = newNode;
else{
this.root.addNode(newNode) ;
}
}
public void print(){
root.printNode() ;
}
}
public class Test{
public static void main(String args[]){
Link link = new Link () ;
link.add("hello") ;
link.add("world") ;
link.add("haha") ;
link.print();
}
}
2.开发可用链表:
使用链表实现数据的增加修改删除操作:
在开发中Node类不可以直接使用。这个时候使用内部类是一个不错的选择。将内部类用private定义。
class Link{
private class Node{
private String data ;
private Node next ;
public Node (String data){
this.data = data ;
}
public void add(Node newNode){
if(this.next == null)
this.next = newNode;
else{
this.next.add(newNode) ;
}
}
public void print(){
System.out.println(this.data) ;
if(this.next == null)
return ;
else {
this.next.print() ;
}
}
public boolean containsNode(String data){
if(data.equals(this.data))
return true;
else {
if(this.next != null)
return this.next.containsNode(data);
else
return false ;
}
}
public String getNode(int index){
if(Link.this.foot ++ == index){
return this.data ;
} else {
return this.next.getNode(index) ;
}
}
public void setNode(int index ,String data){
if(Link.this.foot ++ == index){
this.data = data ;
}
else{
this.next.setNode(index,data) ;
}
}
public void removeNode(Node previous ,String data) {
if(data.equals(this.data)){
previous.next = this.next ;
} else {
this.next.removeNode(this,data) ;
}
}
public void toArrayNode(){
Link.this.retArray[Link.this.foot ++ ] = this.data ;
if(this.next != null)
this.next.toArrayNode() ;
}
}
private Node root ;
private int count = 0 ;
private int foot = 0 ;
private String [] retArray ;
public void add(String data){
if(data == null)
return ;
Node newNode = new Node(data) ;
if (root == null)
root = newNode;
else {
this.root.add(newNode) ;
}
this.count ++ ;
}
public void print(){
this.foot = 0 ;
if(root == null)
return ;
else{
root.print() ;
}
}
public int size() {
return this.count ;
}
public boolean isEmpty(){
return this.count == 0 ;
}
public boolean contains(String data) {
if(this.root == null || data == null)
return false ;
else {
return this.root.containsNode(data) ;
}
}
public String get(int index){
if(index > this.count)
return null ;
else{
return this.root.getNode(index) ;
}
}
public void set(int index , String data) {
if(index > this.count)
return ;
else{
this.foot = 0 ;
this.root.setNode(index,data) ;
}
}
public void remove(String data){
if(this.contains(data)){
if(data.equals(this.root.data))
this.root = this.root.next ;
else{
this.root.next.removeNode(root,data) ;
}
}
}
public String [] toArray(){ //对象数组实现所有的元素遍历
if(this.root == null){
return null ;
} else {
this.foot = 0 ;
this.retArray = new String[this.count] ;
this.root.toArrayNode() ;
return this.retArray ;
}
}
}
public class Test{
public static void main(String args[]){
Link all = new Link() ;
all.add("hello") ;
all.add("world") ;
all.add("zhuopeng") ;
String [] data = all.toArray() ;
for( int x =0 ; x < all.size() ;x ++) {
System.out.println(data[x]) ;
}
}
}
插入一个Book类,然后将 String替换为Book,将比较时候的equals替换为 Compare方法
代码:
class Book{
private String title ;
private double price ;
public Book(String title,double price) {
this.title = title ;
this.price = price ;
}
public boolean Compare(Book book){
if(this.title.equals(book.title) && this.price == book.price)
return true ;
else
return false ;
}
public String getInfo(){
return "title = " + this.title + "price " + this.price ;
}
}
class Link{
private class Node{
private Book data ;
private Node next ;
public Node (Book data){
this.data = data ;
}
public void add(Node newNode){
if(this.next == null)
this.next = newNode;
else{
this.next.add(newNode) ;
}
}
public boolean containsNode(Book data){
if(data.Compare(this.data))
return true;
else {
if(this.next != null)
return this.next.containsNode(data);
else
return false ;
}
}
public Book getNode(int index){
if(Link.this.foot ++ == index){
return this.data ;
} else {
return this.next.getNode(index) ;
}
}
public void setNode(int index ,Book data){
if(Link.this.foot ++ == index){
this.data = data ;
}
else{
this.next.setNode(index,data) ;
}
}
public void removeNode(Node previous ,Book data) {
if(data.Compare(this.data)){
previous.next = this.next ;
} else {
this.next.removeNode(this,data) ;
}
}
public void toArrayNode(){
Link.this.retArray[Link.this.foot ++ ] = this.data ;
if(this.next != null)
this.next.toArrayNode() ;
}
}
private Node root ;
private int count = 0 ;
private int foot = 0 ;
private Book [] retArray ;
public void add(Book data){
if(data == null)
return ;
Node newNode = new Node(data) ;
if (root == null)
root = newNode;
else {
this.root.add(newNode) ;
}
this.count ++ ;
}
public int size() {
return this.count ;
}
public boolean isEmpty(){
return this.count == 0 ;
}
public boolean contains(Book data) {
if(this.root == null || data == null)
return false ;
else {
return this.root.containsNode(data) ;
}
}
public Book get(int index){
if(index > this.count)
return null ;
else{
return this.root.getNode(index) ;
}
}
public void set(int index , Book data) {
if(index > this.count)
return ;
else{
this.foot = 0 ;
this.root.setNode(index,data) ;
}
}
public void remove(Book data){
if(this.contains(data)){
if(data.Compare(this.root.data))
this.root = this.root.next ;
else{
this.root.next.removeNode(root,data) ;
}
}
}
public Book [] toArray(){
if(this.root == null){
return null ;
} else {
this.foot = 0 ;
this.retArray = new Book[this.count] ;
this.root.toArrayNode() ;
return this.retArray ;
}
}
}
public class Test{
public static void main(String args[]){
Link all = new Link() ;
all.add(new Book("hello",10.0) );
all.add(new Book("world",23.4) );
all.add(new Book("zhuopeng",40) );
Book [] data = all.toArray() ;
for( int x =0 ; x < all.size() ;x ++) {
System.out.println(data[x].getInfo()) ;
}
}
}