链表,要简单讲一讲。
链(Link)一般有两个部分,一是节点(node)和指针(point)
Node:存放内容(content)
Point:指向下一个节点,把节点穿起来,就形成链表
看代码, 这个的link其实是一个节点(Node),link穿起来就是一张链表(LinkedList)
实现一下功能
创建链表
在链表头部添加一个节点
删除头部节点
查找一个节点
删除指点节点
public class Link { public String bookName; public int bookSold; public Link next; public Link(String bookName, int bookSold){ this.bookName = bookName; this.bookSold = bookSold; } public void printLink(){ System.out.println(bookName +": "+ bookSold); } public String toString(){ return bookName; } public static void main (String [] args){ System.out.println("Create a new Linklist"); LinkList linkList = new LinkList(); System.out.println(); System.out.println("Add first node: "); linkList.insertFirstLink("Java", 500); linkList.display(); System.out.println(); System.out.println("Add second node: "); linkList.insertFirstLink("Python", 200); linkList.display(); System.out.println(); System.out.println("Add third and forth nodes: "); linkList.insertFirstLink("JavaScript", 300); linkList.insertFirstLink("c++", 100); linkList.display(); System.out.println(); System.out.print("Remove first node: "); System.out.println(linkList.removeFirst()); linkList.display(); System.out.println(); System.out.println("Find the node name is Python"); System.out.println(linkList.find("Python")); System.out.println(); System.out.print("Remove the node name: "); System.out.println(linkList.removeLink("Python")); linkList.display(); System.out.println(); } } class LinkList{ public Link firstLink; LinkList(){ firstLink = null; } public boolean isEmpty(){ return firstLink == null; } public void insertFirstLink(String bookName, int bookSold){ Link newLink = new Link(bookName, bookSold); newLink.next = firstLink; firstLink = newLink; } public Link removeFirst(){ Link referenceLink = firstLink; if(!isEmpty()){ firstLink = firstLink.next; } else System.out.println("LinkList is Empty"); return referenceLink; } public void display(){ Link theLink = firstLink; while (theLink!=null){ theLink.printLink(); System.out.println("The next Link: " + theLink.next); theLink = theLink.next; } } public Link find(String bookName){ Link theLink = firstLink; if(!isEmpty()){ while (!theLink.bookName.equals(bookName)){ if(theLink.next==null){ return null; } else { theLink= theLink.next; } } } else { System.out.println("LinkList is Empty"); } return theLink; } public Link removeLink(String bookName){ Link currentLink = firstLink; Link previousLink = firstLink; while(!currentLink.bookName.equals(bookName)){ if(currentLink.next == null){ return null; } else { previousLink = currentLink; currentLink = currentLink.next; } } if(currentLink == firstLink){ firstLink= firstLink.next; } else { previousLink.next = currentLink.next; } return currentLink; } }
输出结果
Create a new Linklist Add first node: Java: 500 The next Link: null Add second node: Python: 200 The next Link: Java Java: 500 The next Link: null Add third and forth nodes: c++: 100 The next Link: JavaScript JavaScript: 300 The next Link: Python Python: 200 The next Link: Java Java: 500 The next Link: null Remove first node: c++ JavaScript: 300 The next Link: Python Python: 200 The next Link: Java Java: 500 The next Link: null Find the node name is Python Python Remove the node name: Python JavaScript: 300 The next Link: Java Java: 500 The next Link: null
本文出自 “10314466” 博客,请务必保留此出处http://10324466.blog.51cto.com/10314466/1662201
原文地址:http://10324466.blog.51cto.com/10314466/1662201