java实现顺序表,链表,栈,队列_数据结构与算法

书目是科大本科数据结构教材。涉及代码部分准备使用JAVA实现。

绪论

1.数据元素,数据元素之间的逻辑关系,逻辑关系在计算机中的存储表示,适用的操作->数据结构

2.逻辑结构:集合,线性,树状,图

   存储结构:顺序存储,链式存储,散列存储

3.o(1)<o(log2(n))<o(n)<o(nlog2(n))<o(n(2))<o(n(3))<o(2(n))

线性表

1.线性表: n个具有相同类型的数据元素的有限序列

2.顺序表:一组地址连续的存储单元存储线性表 ->数组

   查找 : 按位置->随机存取特性 o(1) , 按值 ->o(n)

   插入删除 : o(n)

package java_practice;


import java.util.Arrays;

/**Java中,可以使用
 * 数组:单种类对象或基本类型,指定大小(更符合书上顺序表定义)
 * ArrayList集合:多种类对象类型,动态大小
 * 来作为顺序表
 * 在此暂不考虑ArrayList等Java是如何实现的,只给出用法
*/
public class HelloWorld{
    public static void main(String[] args){
        /*数组*/
        MyArray my = new MyArray(6);
        my.insert(1,2);
        my.insert(0,3);
        my.delete(1);
        System.out.println(my.search_k(1));
        System.out.println(my.search_v(3));

    }
}

class MyArray{
    private int[] i;
    private int len = 5;
    MyArray(int len){
        this.len = len;
        //创建及初始化
        this.i =new int[this.len];
    }

    //查找
    public int search_v(int value){
        for (int x = 0;x<this.i.length;x++) {
            if (this.i[x] == value) {
                return x;
            }
        }
        return -1;
    }
    public int search_k(int key){
        return this.i[key];
    }

    //删除
    public void delete(int key){
        for(int x=key+1;x<this.i.length;x++){
            this.i[x-1] = this.i[x];
        }
        this.i[this.i.length-1] = 0;
        print();
    }

    //插入
    public void insert(int key,int value){
        for (int x=key;x<this.i.length;x++){
            int temp = this.i[x];
            this.i[x] = value;
            value = temp;
        }
        print();
    }

    //销毁
    public void destroy(){
        this.i = null;
        print();
    }

    //清空
    public void  clear(){
        this.i = new int[this.len];
        print();
    }

    //判空操作由于现在使用的是int数组只能判定是否为0,如果是对象类型数组则判断是否所有位置均为null
    //前驱后续均为基础数组操作,获调用search_k即可
    //打印
    public void print(){
        System.out.println(Arrays.toString(this.i));
    }
}
public class HelloWorld{
    public static void main(String[] args){
        /*ArrayList*/

        MyArrayList<Integer> mai = new MyArrayList<>();
        mai.create(6,6,8,9);
        mai.print();
        mai.insert(1,3);
        mai.get_k(1);
        mai.get_v(19);
        mai.delete(2);
    }
}

class MyArrayList<E>{
    private ArrayList<E> ai = null;

    //初始化后才可使用insert及set ArrayList是变容量的
    public void create(int capacity,E el1,E el2,E el3){
        this.ai = new ArrayList<E>(capacity);
        this.ai.add(el1);
        this.ai.add(el2);
        this.ai.add(el3);
    }

    //ArrayList 自带插入方法
    public void insert(int key,E value){
        this.ai.add(key,value);
        print();
    }
    //自带方法
    public void get_v(E element){
        System.out.println(this.ai.contains(element));
    }
    public void get_k(int k){
        E el = this.ai.get(k);
        System.out.println(el.toString());
    }
    //自带方法
    public void delete(int k){
        E el = this.ai.remove(k);
        print();
    }

    //清空,判空均为自带方法

    public void print(){
        System.out.println(this.ai.toString());
    }
}


3.链表:一组任意的存储单元存储线性表

   查找:顺序存取 按位置,按值->o(n)

   插入,删除 :o(n) 减少了数据移动

   循环单链表:环,可以从表中任意节点开始遍历整个链表

   双向链表:每个节点有两个指针域,指向前一个及后一个节点

   静态链表:采用数组模拟链表的指针

import java.util.LinkedList;

/**Java中,可以使用
 * 自定义节点类型及操作
 * LinkedList
 * 来作为链表
 * 在此暂不考虑Java是如何实现的,只给出用法
*/
public class HelloWorld{
    public static void main(String[] args){
        LinkedList<Integer> exp1 = new LinkedList<>();
        //插入
        exp1.add(0,1);
        exp1.add(0,10);
        System.out.println(exp1.toString());
        //查找
        System.out.println(exp1.get(1));
        System.out.println(exp1.indexOf(10));
        //删除
        exp1.remove(1);
        System.out.println(exp1.toString());
        //长度
        System.out.println(exp1.size());
    }
}
import java.util.LinkedList;
//大部分条件判断都没做,为了容错,应该加上对角标的判断
public class HelloWorld{
    public static void main(String[] args){
//       
        MyLinkList my = new MyLinkList();
        my.add(10);
        my.add(1);
        my.add(20);
//        my.print();
//        System.out.println(my.length());
//        my.getbyindex(1);
//        my.getbyindex(0);
//        my.insert(0,20);
        my.delete(1);
    }
}

class MyLinkList{
    //头结点
    private Node head;
    //指针
    private Node where;

    class Node{
        //数据域
        int data;
        //指针域
        Node next;

        public Node(int init){
            this.data = init;
            next = null;
        }
    }

    //初始化
    MyLinkList(){
        this.head = new Node(-1);
        this.where = this.head;
    }

    //添加
    public void add(int e){
        Node ne = new Node(e);
        this.where.next = ne;
        this.where = ne;
    }

    //查询
    public Node getbyindex(int index){
        int i = -1;
        Node in = this.head;
        while (i<index){
            in = in.next;
            i++;
        }

        return in;
    }

    //插入
    public void insert(int index, int data){
        Node now = new Node(data);
        Node pre = getbyindex(index-1);
        Node net = getbyindex(index);
//        System.out.print(net.data);
        pre.next = now;
        now.next = net;
        print();
    }

    public void delete(int index){
        Node pre = getbyindex(index-1);
        Node net = getbyindex(index+1);
        pre.next = net;
        print();
    }

    //长度
    public int length(){
//        System.out.print("sad");
        Node temp = this.head;
        int lenth = 0;
        while (temp.next != null){
//            System.out.print(lenth);
            lenth = lenth+1;
            temp = temp.next;
        }
//        System.out.print(lenth);
        return lenth;
    }

    public void print(){
        Node temp = this.head;
        while (temp.next != null){
            System.out.println(temp.next.data);
            temp = temp.next;
        }
    }


}

4.栈:运算受限的线性表 First In Last Out 。 也可以使用顺序表或链表进行实现

/**
 * for the test
 *
 * @auther:dan
 * @version:1.0
 * @date:2017.11.16
 */
package java_practice;

import java.util.LinkedList;

/**Java中,可以使用
 * 链表
 * 顺序表
 * stack类
 * 进行限定操作来模拟栈的操作
 */

public class HelloWorld{
    public static void main(String[] args){
        MyLinkList_stack my = new MyLinkList_stack();
        my.push(10);
        my.push(1);
        my.push(20);
        my.print();
        my.pop();
        my.print();

        my.pop();
        my.print();

        my.pop();
        my.print();

        my.pop();
        my.print();
    }
}

class MyLinkList_stack{

    //指针
    private Node top ;

    class Node{
        //数据域
        int data;
        //指针域
        Node next;

        public Node(int init){
            this.data = init;
            next = null;
        }
    }

    //初始化
    MyLinkList_stack(){
        this.top = null;
    }

    //入栈
    public void push(int e){
        Node ne = new Node(e);
        ne.next = this.top;
        this.top = ne;
    }

    //出栈
    public void pop(){
        if (this.top != null){
//            System.out.print(this.top.data);
            this.top = this.top.next;
        }else {
            System.out.print("null stack");
        }
    }


    public void print(){
        Node temp = this.top;
        while (temp != null){
            System.out.println(temp.data);
            temp = temp.next;
        }
    }
}
public class HelloWorld{
    public static void main(String[] args){
          MyArray_stack my = new MyArray_stack(5);
          my.push(10);
          my.push(1);
          my.push(20);
          my.print();

          my.pop();
          my.print();

          my.pop();
          my.print();

    }
}

class MyArray_stack{
    private int[] i;
    private int len = 5;
    private int top;
    MyArray_stack(int len){
        this.len = len;
        //创建及初始化
        this.i =new int[this.len];
        this.top = -1;
    }

    //入栈
    public void push(int e){
        if (this.top>this.len-1){
            System.out.print("full stack");
        }else {
            this.top = this.top + 1;
            this.i[this.top] = e;
        }
    }

    public void pop(){
        if (this.top<0){
            System.out.print("null stack");
        }else {
            this.i[this.top] = 0;
            this.top = this.top -1;
        }
    }
    public void print(){
        System.out.println(Arrays.toString(this.i));
    }
}

5.计算器的实现思路:后缀表达式,操作数入栈,读到操作符则取出两个操作数,结果入栈

6.队列:First In First Out。也可以使用顺序表或链表进行实现

   优先队列:利用堆实现,每次返回的是优先级最高的任务(后续补充实现,Java中的实现是PriorityQueue)

import java.util.Arrays;
import java.util.LinkedList;

/**Java中,可以使用
 * 链表
 * 顺序表(为了防止假溢出,使用循环队列实现)
 * Queue类,Queue是一个接口,可以使用ArrayDeque->数组队列,LinkedList->链表队列
 * 进行限定操作来模拟队列的操作
 */

public class HelloWorld{
    public static void main(String[] args){
          MyArray_queue my = new MyArray_queue(5);
          my.push(10);
          my.push(1);
          my.push(20);
          my.print();

          my.pop();
          my.print();

          my.pop();
          my.print();
    }
}

class MyArray_queue{
    private int[] i;
    private int len = 5;
    private int front;
    private int rear;
    //循环队列判空或满的方法:1.设置count变量记录个数;2.另设一个标志用于区分;3.少用一个数据空间,(rear+1)%len == front
    private int count;

    MyArray_queue(int len){
        this.len = len;
        //创建及初始化
        this.i =new int[this.len];
        this.front = -1;
        this.rear = -1;
        this.count = 0;
    }

    //入队
    public void push(int e){
        if (this.count>=this.len){
            System.out.print("full queue");
        }else {
            this.rear = (this.rear+1)%this.len;
            this.i[rear] = e;
            this.count = this.count+1;
        }
    }

    public void pop(){
        if (this.count<=0){
            System.out.print("null queue");
        }else {
            this.i[this.front+1] = 0;
            this.front = (this.front+1)%this.len;
            this.count = this.count-1;
        }
    }

    public void print(){
        System.out.println(Arrays.toString(this.i));
    }
}
import java.util.Arrays;
import java.util.LinkedList;

/**Java中,可以使用
 * 链表
 * 顺序表(为了防止假溢出,使用循环队列实现)
 * Queue类
 * 进行限定操作来模拟队列的操作
 */

public class HelloWorld{
    public static void main(String[] args){
        MyLinkList_queue my = new MyLinkList_queue();
        my.push(10);
        my.push(1);
        my.push(20);
        my.print();
        my.pop();
        my.print();

    }
}

class MyLinkList_queue{

    //指针
    private Node front ;
    private Node rear;

    class Node{
        //数据域
        int data;
        //指针域
        Node next;

        public Node(int init){
            this.data = init;
            next = null;
        }
    }

    //初始化
    MyLinkList_queue(){
        this.front = null;
        this.rear = null;
    }

    //入队,只能在队尾入队(单链表决定的)
    public void push(int e){
        Node ne = new Node(e);
        ne.next = null;
        if (this.front == null){
            this.front = ne;
            this.rear = ne;
        }else {
            this.rear.next = ne;
            this.rear = ne;
        }

    }

    //出栈
    public void pop(){
        if (this.front != null){
//            System.out.print(this.front.data);
            this.front = this.front.next;
//            System.out.print(this.front.data);
        }else {
            System.out.print("null stack");
        }
    }


    public void print(){
        Node temp = this.front;
        while (temp != null){
            System.out.println(temp.data);
            temp = temp.next;
        }
    }


}

  


猜你喜欢

转载自blog.csdn.net/u011010851/article/details/79404086