用链表实现栈和队列

一、我们知道在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();

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值