数据结构与算法分析学习笔记

@前言
迎来了本学期的重头专业课——数据结构与算法分析。
这次我们的老师是来自英国威尔士亚伯大学(Aberystwyth University)的Fred Long教授!!!老师曾以访问学者的身份来到CMU!!!!!!有六本著作!!

@知识点随笔
【新思想】
java中操作的都是引用!!!
即便是实例化,其实也是新建了一个引用!
操作引用很方便,很少直接操作本体!
【LinkedList的AddToFront方法传递的变量】
应该是传递对象,而不是节点。传对象好一些。
【上下层之间没有大小关系】
【AVL是实现平衡树的一种原则,另外还有红黑树等等】
【平衡树的目的:避免因为不规则的插入,没有及时调整,导致二叉树的时间复杂度急剧上升】
【AVL的目的:原本的绝对平衡二叉树过于严格,应用价值不大,AVL树允许左右深度相差1】
【平衡因素的改变仅限于新节点到根节点的路径上】
【泛型】可能就是增强代码的重用性
【在格式基础上可以实现固定任意的T类】
【如何理解泛型的作用】
1.多用于集合,便于控制集合内的元素类型固定唯一
2.另,可以当做参数来理解,即一套体系可以让不同的类适用
【B树】
·牢记各个树的性质!
·对于任给的一串数字,有可能有多种B树
【对java泛型的补充理解】
泛型的操作对象是集合!Collection
为什么不规定泛型时输出需要强制转化~
因为不规定泛型时,编译器默认泛型是Object
1、泛型的类型参数只能是类类型(包括自定义类),不能是简单类型。
2、同一种泛型可以对应多个版本(因为参数类型是不确定的),不同版本的泛型类实例是不兼容的。
3、泛型的类型参数可以有多个。
4、泛型的参数类型可以使用extends语句,例如。习惯上成为“有界类型”。
5、泛型的参数类型还可以是通配符类型。例如Class


public class BoundedStack {
    private Person[] list;
    private int firstFree = 0;
    private static final int DEFAULT_MAXIMUM = 10;

    // 2 Constructors
    public BoundedStack() {
        list = new Person[DEFAULT_MAXIMUM];
    }

    public BoundedStack(int size) {
        list = new Person[size];
    }

    // push method, which can add a new element at the end
    public void push(Person element) {

        if (firstFree == list.length) {
            throw new StackOverflowError();
        }

        list[firstFree] = element;
        firstFree++;
    }

    // pop method to peek and delete the top item
    public Person pop() {
        Person temp;
        temp = peek();

        this.list[firstFree - 1] = null;
        this.firstFree--;

        return temp;
    }

    // isEmpty method to determine whether the stack is empty
    public boolean isEmpty() {
        return firstFree == 0;
    }

    // peek method to return the top item
    public Person peek() {
        return this.list[firstFree - 1];
    }

    // depth method to get the number of the items in the stack
    public int depth() {
        return this.firstFree;
    }

    // overwrite toString
    public String toString(Person[] list) {
        String tempString = "";
        for (int i = 0; i < this.firstFree; i++)
            tempString += ("Name: " + list[i].getName() + " Number: " + list[i].getNum());
        return tempString;
    }

    // compare two stacks
    public boolean equals(BoundedStack comparedStack) {
        if (this.firstFree != comparedStack.firstFree)
            return false;
        else {
            for (int i = 0; i < this.firstFree; i++) {
                if (this.list[i] != comparedStack.list[i])
                    return false;
            }
        }
        return true;
    }

    // main method to test BoundedStack class
    public static void main(String args[]) {
        Person raven = new Person("Raven", 1);//Problem: using the same constructor
        Person messi = new Person("Messi", 2);
        Person neymar = new Person("Neymar", 3);
        Person mars = new Person("Mars", 4);

        BoundedStack myStack = new BoundedStack();
        myStack.push(raven);
        myStack.push(messi);
        myStack.push(neymar);
        myStack.push(mars);

        System.out.println(myStack.toString(myStack.list));
        System.out.println("The depth is: " + myStack.depth());
        System.out.println("The top element is: " + myStack.peek().getName());
        myStack.pop();

        System.out.println(myStack.toString(myStack.list));
        System.out.println("Now, the depth is: " + myStack.depth());
        System.out.println("Now, The top element is: " + myStack.peek().getName());

        System.out.println("Equal Test");
        BoundedStack testStack=new BoundedStack();
        System.out.println(myStack.equals(testStack));
        testStack=myStack;
        System.out.println(myStack.equals(testStack));

        System.out.println("Over");
    }
}

// Other classes
/*class Person {
    private String 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值