顺序表,链表,栈,队列_Java表示

代码不确认完全正确,主要看逻辑

顺序表

package com.datastruct.list;

public class SequenceList {
    //顺序表最大长度
    static final int MAXLEN=100;
    private Data[] DataList=new Data[MAXLEN];
    private int ListLength; 

    //初始化方法,确保空间内全为空
    public SequenceList(){      
        for(int i=0;i<MAXLEN;i++){
            this.DataList[i]=null;
        }
    }

    //长度获取
    public int getLength(){
        return this.ListLength;
    }

    //是否为空
    public boolean isEmpty(){
        if(this.ListLength==0){
            System.out.println("为空");
            return true;
        }else{
            return false;
        }
    }

    //是否已满
    public boolean isFull(){
        if(this.ListLength==MAXLEN){
            System.out.println("已满");
            return true;
        }else{
            return false;
        }
    }

    //在指定位置插入元素
    public int insert(int location,Data InsertData){
        if (this.getLength()>=MAXLEN){
            System.out.println("超出长度");
            return 0;
        }
        if(location<1||location>this.getLength()+1){
            System.out.println("插入位置不合理");
            return 0;
        }
        for (int i=this.ListLength;i>location-1;i--){
            this.DataList[i]=this.DataList[i-1];
        }
        this.DataList[location-1]=InsertData;
        this.ListLength++;
        return 1;
    }

    //在头插入元素
    public int addfirst(Data data){
        this.insert(1, data);
        return 1;
    }

    //在队尾插入元素
    public int addlast(Data data){
        this.insert(this.ListLength+1, data);
        return 1;
    }

    //删除指定数据
    public int delete(Data data){
        for (int i=0;i<this.ListLength;i++){
            if(this.DataList[i].equals(data)){
                for(int j=i;j<this.ListLength-1;j++){
                    this.DataList[j]=this.DataList[j+1];
                }
                this.ListLength--;
                return 1;
            }

        }
        System.out.println("没有这一元素");
        return 0;
    }

    //删除指定位置的数据
    public int delete(int location){
        if(location<1||location>this.ListLength){
            System.out.println("位置超出范围");
            return 0;
        }
        for(int i=location-1;i<this.ListLength-2;i++){
            this.DataList[i]=this.DataList[i+1];
        }
        this.ListLength--;
        return 1;
    }

    //删除第一个元素
    public int deletefisrt(){
        if(this.isEmpty()){
            return 0;
        }
        for(int i=0;i<this.ListLength-1;i++){
            this.DataList[i]=this.DataList[i+1];
        }
        this.ListLength--;
        return 1;
    }

    //删除最后一个元素
    public int deletelast(){
        if(this.isEmpty()){
            return 0;
        }
        this.DataList[this.ListLength-1]=null;
        this.ListLength--;
        return 1;
    }

    //按内容查找查找元素位置
    public int find(Data data){
        if(this.isEmpty()){
            return 0;
        }
        for (int i=0;i<this.ListLength;i++){
            if(this.DataList[i].equals(data)){               
                return i;
            }          
        }
        return 0;
    }

    //按位置查找元素内容
    public Data getData(int location){
        if(location<1||location>this.ListLength){
            System.out.println("No this location");
            return null;
        }
        return this.DataList[location-1];
    }

    //清空表
    public void clear(){
        for(int i=ListLength;i>0;i--){
            DataList[i-1]=null;
        }
        ListLength=0;
    }

    //销毁表  ,销毁和清空是不一样的
    public void free(SequenceList s){
        if(s!=null){
            s=null;
        }
    }

}

链表

package com.datastruct.list;

public class LinkList {
    private Node head;

    //在尾部添加节点
    public Node addLast(Data data){
        Node node=new Node();
        node.nodeData=data;
        node.next=null;
        Node temp=head;
        while(temp.next!=null){
            temp=temp.next;
        }
        temp.next=node;
        return head;
    }

    //在头部添加节点
    public Node addFirst(Data data){
        Node node=new Node();
        node.nodeData=data;
        node.next=head;
        head=node;
        return head;
    }

    //查找节点
    public Node find(Data data){
        Node temp=head;
        while(temp!=null){
            if(temp.nodeData.equals(data)){
                return temp;
            }
            temp=temp.next;
        }
        return null;
    }

    //插入节点
    public Node insert(Data data,Data locationData){
        Node temp=head;
        while(temp!=null){
            if(temp.nodeData.equals(locationData)){
                Node node=new Node();
                node.nodeData=data;
                node.next=temp.next;
                temp.next=node;
                return head;
            }
        }
        return head;
    }

    //删除节点
    public Node delete(Data data){
        Node temp=head;
        while(temp!=null){
            if(temp.next.nodeData.equals(data)){
                temp.next=temp.next.next;
                return head;
            }
        }
        return head;
    }

    //表长度
    public int length(){
        Node temp=head;
        int length=0;;
        while(temp!=null){
            length++;
            temp=temp.next;
        }
        return length;
    }

    public void free(){
        head=null;
    }
}

package com.datastruct.list;

public class Stack {

    private Data[] data;//以数组或者说顺序表的形式来实现栈,其实也可以用链表来实现
    private Data top;//定义栈顶
    private int maxSize;//栈的最大容量
    private int size;//栈的数据量

    public Stack(int maxSize){//栈初始化
        if((this.data=new Data[maxSize]) != null){//尝试开辟内存,如果系统内存不够,可能失败
            this.top=this.data[0];
            this.maxSize=maxSize;
            this.size=0;
        }
        System.out.println("内存分配失败");
        return;
    }

    //判断是否为空
    public boolean isEmpty(){
        return top==null;
    }

    //判断是否满栈
    public boolean isFull(){
        if(size==maxSize){
            System.out.println("Full");
            return true;
        }
        return false;
    }

    //入栈
    public Stack push(Data data){
        if(!isFull()){
            this.data[size]=data;
            size++;
            return this;
        }
        return this;
    }

    //出栈
    public Data pop(){
        if(!isEmpty()){
            Data popData=this.data[size-1];
            this.data[size-1]=null;
            this.size--;
            return popData;
        }
        return null;
    }

    //读取栈,读取指的是读栈顶元素,和出栈的不同是读取不会弹出栈顶元素。
    public Data read(){
        return this.data[size-1];
    }

    //栈元素数量
    public int size(){
        return size;
    }

     //清空栈
    public void clear(){
        for(int i=size;i>0;i--){
            data[i-1]=null;
        }
        top=data[0];
        size=0;
    }

    //销毁栈 销毁和清空是不一样的  
    public void free(Stack s){
        if(s!=null){
            s=null;
        }
    }

}

队列

package com.datastruct.list;

public class Queue {
    //队列同样可以使用顺序表或链表来实现,考虑到队列是头和尾都在移动的,链表更合适一些
    private Node head;
    private Node tail;

    //初始化队列
    public Queue(){
        head=tail;
    }

    //判断是否为空
    public boolean isEmpty(){
        return head==tail;
    }

    //清空队列
    public void clear(){
        head=null;
        tail=head;
    }

    //销毁队列
    public void free(Queue q){
        q=null;
    }

    //入队
    public void inQueue(Data data){
        Node node=new Node();
        node.nodeData=data;
        node.next=null;
        tail.next=node;
        tail=node;
    }

    //出队
    public Data outQueue(){
        Node node=head;
        head=head.next;
        return node.nodeData;
    }

    //读取头元素
    public Data readQueue(){
        return head.nodeData;
    }

    //队列长度
    public int length(){
        int length=0;
        Node temp=head;
        while(temp!=tail){
            length++;
            temp=temp.next;
        }
        return length;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值