一、我们知道在Java中是没有指针的,那么如何在Java中创建链表呢,自我感觉比C++的好理解,Java中用类创建一个包括数据与next的类的引用就可以了。
1.首先是一个基本链表节点类的创建
public class Link {
public long dDate;
public Link next;
public Link(long dd){
dDate=dd;
}
public void displayLink(){
System.out.print(dDate+" ");
}
}
2.链表的一些基本对象,只有创建了才可以使用对象
import com.sun.xml.internal.fastinfoset.util.FixedEntryStringIntMap;
class LinkList{
private Link first;
public LinkList(){
first=null;
}
public boolean isEmpty(){
return (first==null);
}
public void insertFirst(long dd){
Link newLink=new Link(dd);
newLink.next=first;
first = newLink;
}
public long deletefirst(){
Link temp=first;
first=first.next;
return temp.dDate;
}
public void displayList(){
Link current=first;
while(current!=null){
current.displayLink();
current=current.next;
}
System.out.println("");
}
}
3.数组与链表的唯一区别就是链表你用多少就创建多少,不会造成数据的浪费。
public class LinkStack {
private LinkList theList;
public LinkStack(){
theList=new LinkList();
}
public void push(long j){
theList.insertFirst(j);
}
public long pop(long j){
return theList.deletefirst();
}
public boolean isEmpty(){
return(theList.isEmpty());
}
public void displayStak(){
System.out.print("Stack(top-->bottom):");
theList.displayList();
}
}
4.主类测试
import java.util.Random;
public class LinkStackApp {
public static void main(String[] args){
LinkStack theStack=new LinkStack();
Random r =new Random();
for(int i=0;i<5;i++){
long temp=r.nextInt(100);
System.out.println(temp);
theStack.push(temp);
if(temp>=15&&temp<=50){
theStack.pop(temp);
}
}
theStack.displayStak();
}
}
二、基本的一些队列类与对象的创建
package com.company;
public class Link {
public long dDate;
public Link next;
public Link(long d){
dDate=d;
}
public void displayLink(){
System.out.print(dDate+" ");
}
}
package com.company;
public class FirstLastList {
private Link first;
private Link last;
public FirstLastList(){
first=null;
last=null;
}
public boolean isEmpty(){
return first==null;
}
public void insertLast(long dd){
Link newLink=new Link(dd);
if(isEmpty())
first=newLink;
else
last.next=newLink;
last=newLink;
}
public long deleteFirst(){
long temp=first.dDate;
if(first.next==null)
last=null;
first=first.next;
return temp;
}
public void displayList(){
Link current=first;
while(current!=null){
current.displayLink();
current= current.next;
}
System.out.println("");
}
}
package com.company;
public class FirstLastList {
private Link first;
private Link last;
public FirstLastList(){
first=null;
last=null;
}
public boolean isEmpty(){
return first==null;
}
public void insertLast(long dd){
Link newLink=new Link(dd);
if(isEmpty())
first=newLink;
else
last.next=newLink;
last=newLink;
}
public long deleteFirst(){
long temp=first.dDate;
if(first.next==null)
last=null;
first=first.next;
return temp;
}
public void displayList(){
Link current=first;
while(current!=null){
current.displayLink();
current= current.next;
}
System.out.println("");
}
}
package com.company;
import java.util.Random;
public class LinkQueueApp {
public static void main(String[] args) {
LinkQueue theQueue=new LinkQueue();
for(int i=0;i<20;i++){
Random r=new Random();
long temp=r.nextInt(700);
System.out.println(temp);
theQueue.insert(temp);
if(temp>=0&&temp<=500)
theQueue.remove();
}
System.out.println("最终结果为:");
theQueue.displayQueue();
}
}