java数据结构代码(持续更新中),欢迎指正

1、顺序表(数组实现)

package sj;

public class SequenceList<T>{
    private int N=0;   //记录当前顺序表中的元素个数
    private T[] eles;  //顺序表用数组实现,存储元素

    //构造方法,初始化数组和长度
    public SequenceList(int capacity){
        this.eles = (T[])new Object[capacity];
        this.N = 0;
    }

    //清空线性表
    public void clear(){
        this.N= 0;
    }

    //判断是否为空表
    public boolean isEmpty(){
        return this.N==0;
    }

    //获取线性表的长度
    public int length(){
        return this.N;
    }

    //获取指定位置的元素
    public T get(int i){
        return eles[i];
    }

    //添加元素
    public void insert(T t){
        if(N==eles.length){
            eles = reSize(2*eles.length);
        }
        eles[N++] = t;
    }

    //在位置i处插入元素t
    public void insert(int i,T t){
        //插入元素前要进行是否扩容的判断
        if(N == eles.length){
            eles = reSize(2*eles.length);
        }
        N++;  //元素个数+1
        for (int j = N; j > i ; j--) {  //将i以及i之后的元素依次向后推移一位
            eles[j-1]= eles[j-2];
        }
        eles[i] = t;
    }

    //删除指定位置i处的元素,并返回该元素
    public T remove(int i){
        T t = eles[i];
        for (int j = i; j <eles.length-1 ; j++) {
            eles[j] = eles[j+1];
        }
        N--;
        //删除元素后,判断该线性表长度是否小于数组容器的4分之一,小于则将数组容器的长度减半,减少空间浪费
        if(N <= eles.length/4){
            eles = reSize(eles.length/2);
        }
        return t;
    }

    //查找t元素第一次出现的第一个位置
    public int indexof(T t){
        for (int i = 0; i <eles.length ; i++) {
            if(t.equals(eles[i])){
                return i;
            }
        }
        return -1;
    }

    //在元素插入与删除时进行扩容或者缩容的方法
    public T[] reSize(int newsize){
        T[] t = (T[]) new Object[newsize];
        for (int i = 0; i <eles.length ; i++) {
            t[i] = eles[i];
        }
        return  t;
    }
}

2、单向链表

package sj;

public class LinkList<T> {
    private int N;  //链表长度
    private Node head; //头节点

    //构造方法
    public LinkList(){
        this.N = 0;
        this.head = new Node(null,null);  //初始化头节点
    }

    //成员内部类--节点类
    private class Node<T>{
        T t;       //用以保存节点的数据域
        Node next;  //用以节点的指针域,指向下一个节点
        public Node(T t,Node next){
            this.t = t;
            this.next = next;
        }
    }

    //清空链表 1、使得头节点指针域为null 2、N==0
    public void clear(){
        head.next = null;
        this.N = 0;
    }
    public boolean isEmpty(){
        return N==0;
    }
    public int length(){
        return N;
    }
    //获取指定位置i的元素位置的值
    public T get(int i){
        Node n = head; //用n表示头节点,则下方第一次循环获取的是第2位置也就是索引为0的节点,头节点不算表长
        for (int j = -1;  j<i ; j++) {
             n= n.next;
        }
        return (T) n.t;
    }
    //向链表中添加元素t 1、找到最后一个节点 2、最后节点指针域指向新节点
    public void insert(T t){
        Node n = head;
        for (int i = 0; i <N ; i++) {
            n= n.next;
        }
        n.next = new Node(t,null);  //最后一个节点指针域指向t元素所在节点
        N++;  //元素个数+1
    }
    //在指定i位置处添加元素 1、找到i-1处节点  2、插入操作
    public void insert(int i,T t){
        Node n = head;
        for (int j = 0; j <i ; j++) {
            n = n.next;
        }
        Node temp = n.next; //临时节点来保存原来i位置的节点
        Node newNode = new Node(t,temp);  //新节点指针域指向原i位置节点
        n.next = newNode;  //让i-1位置节点的指针域指向新节点
        N++;
    }
    //删除i位置处的节点
    public T delete(int i){
        Node n = head;
        for (int j = 0; j <i-1 ; j++) {
            n= n.next;
        }
        Node temp = n.next;  //此处表示i位置的节点,即把i位置的节点赋值给temp
        n.next = temp.next;  //i-1位置的指针域指向i+1节点
        N--;
        return (T) temp.t;
    }
    //查找出元素t第一次在链表中出现的位置
    public int indexOf(T t){
        Node n = head;
        for (int i = 0; i <N-1 ; i++) {
            n = n.next;
            if(t.equals(n.next)){
                return i+1;
            }
        }
        return -1;
    }
}

3、双向链表

package sj;

public class TowWayLinkList <T>{
    //成员内部类,即节点类
    public class Node{
        public Node pre;
        public Node next;
        public T t;
        //构造方法
        public Node(Node pre,T t,Node next){
            this.pre = pre;
            this.next = next;
            this.t = t;
        }

    }
    private int N;  //双向链表长度
    private Node head;  //头节点
    private Node last;  //尾节点
    //构造方法,初始化头节点和尾节点
    public TowWayLinkList(){
        this.N = 0;
        this.head = new Node(null,null,null);
        this.last = null; //最开始是没有尾节点的
    }

    //置空双向链表
    public void clear(){
        head.next = null;
        last=null;
        this.N = 0;
    }
    //判断是否为空
    public boolean isEmpty(){
        return N==0;
    }
    //获取双向链表中元素的个数
    public int length() {
        return N;
    }
    //获取第一个元素
    public T getFirstItem(){
        if(isEmpty()){
            return null;
        }
        else{
            return head.next.t;
        }
    }
    //获取最后一个元素的值
    public T getLastItem(){
        if(isEmpty()){
            return null;
        }
        else{
            return last.t;
        }
    }
    //读取并且返回双向链表中i位置元素的值
    public T get(int i){
        Node n = head.next;
        for(int j = 0 ;j<i ;j++){
            n = n.next;
        }
        return n.t;
    }
    //往双向链表中添加一个元素t
    public void insert(T t){
        if(isEmpty()){
            Node newNode = new Node(head,t,null);
            head.next = newNode;
            last = newNode;  //让新节点成为尾节点
            N++;
        }
        else{
            Node oldLast = last;
            Node newNode = new Node(oldLast,t,null); //新节点前指针域指向前一个节点
            oldLast.next = newNode;   //前一个节点后指针域指向新节点
            last = newNode;  //新节点成为尾节点
            N++;
        }
    }
    //在双向链表i位置元素之前插入元素t
    public void insert(int i,T t){
        Node n = head.next;
        for (int j = 0; j <i-1; j++) {
            n = n.next;
        }
        Node nodei =n.next;
        Node newNode =  new Node(n,t,nodei);
        n.next = newNode;  //这步操作完成了i-1节点与新节点的双向指向,以及新节点后指针域指向原i节点
        nodei.pre = newNode;
        N++;
    }
    //删除并返回i位置元素的值
    public T remove(int i){
        Node n = head.next;
        for (int j = 0; j <i ; j++) {
            n = n.next;
        }
        Node pre1Nodei =n.pre; //i-1位置节点
        Node last1Nodei = n.next;  //i+1位置节点
        pre1Nodei.next = last1Nodei;
        last1Nodei.pre = pre1Nodei;
        N--;
        return n.t;
    }
    //返回双向链表中首次出现的指定元素的序列号,若无则返回-1
    public int indexOf(T t1){
        Node n = head.next;
        int j=0;
        while (n.next!= null){
            if(n.t.equals(t1)){
                return j;
            }
            else {
                n = n.next;
                j++;
            }
        }
        return -1;
    }
    //获取第一个元素
    public T getFirst(){
        return head.next.t;
    }
    //获取最后一个元素
    public T getLast(){
        return last.t;
    }
}


4、栈(单链表实现)

package sj;

public class Stack<T> {
    //成员内部类,节点类
    private class Node{
        public Node next;
        public T t;
        //构造方法
        public Node(T t,Node next) {
            this.next = next;
            this.t = t;
        }
    }
    public int N;  //长度
    public Node first;  //头节点
    //构造方法
    public Stack(){
        this.N = 0;
        this.first = new Node(null,null);
    }

    //判空
    public boolean isEmpty(){
        return N==0;
    }
    //获取栈中元素的个数
    public int size(){
        return N;
    }
    //压栈
    public void push(T t){
        if(isEmpty()){
            first.next = new Node(t,null);
        }
        else{
            Node n = first.next;  //头节点的下一个元素
            Node newNode = new Node(t,n); //新节点指向原来位置0处的元素
            first.next = newNode;  //头节点指向新节点
        }
        N++;
    }
    //出栈
    public T pop(){
        if(isEmpty()){
            return null;
        }
        else{
            Node n0 = first.next;  //位置0处节点
            first.next = n0.next;  //头节点指针域指向原位置1处的节点
            N--;
            return n0.t;
        }
    }
    //i位置元素
    public T get(int i){
        Node n = first;
        for (int j = -1; j <i ; j++) {
            n = n.next;
        }
        return n.t;
    }
}

5、队列(单链表实现)

package sj;
//队列--单链表实现
public class Queue<T>{
    //成员类,节点类
    private class Node{
        public T t;
        public Node next;
        //构造方法
        public Node(T t,Node next){
            this.next = next;
            this.t = t;
        }
    }
    public Node head;
    public int N;
    public Node last;
    //构造方法
    public Queue(){
        this.head = new Node(null,null);
        this.N = 0;
        this.last = null;
    }
    //判空
    public boolean isEmpty(){
        return N == 0;
    }
    //元素个数
    public int length(){
        return N;
    }
    /*头插尾取,虽然更符合队列的图示结构,但在取出元素的时候不好操作,
    要遍历或者是用双向链表实现,故此处入队出队都在头节点附近操作数据避免遍历*/
    //入队列
    public void enqueue(T t){
        if(isEmpty()){
            head.next = new Node(t,null);
            last = head.next;//last指向新插入的节点
        }
        else{
            last.next = new Node(t,null);
            Node newNode = last.next;
            last = newNode;
        }
        N++;
    }
    //出队列,头节点后的第一个元素先取出
    public T dequeue(){
        Node n0;
        Node n1;
        if(isEmpty()){
            return null;
        }
        else{
            n0 = head.next;
            n1 = n0.next;  //记录1位置的节点
            head.next = n1;
        }
        N--;
        if(isEmpty()){
            last = null;
        }
        return n0.t;
    }
    //get
    public T get(int i){
        Node n = head.next;
        for (int j = 0; j <i ; j++) {
            n = n.next;
        }
        return n.t;
    }
}
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值