20172303 2018-2019-1 "program design and data Structure" 4th Week study Summary

Source: Internet
Author: User

20172303 2018-2019-1 "program design and data Structure" 4th Week study Summary of learning contents I. Queue Overview
    • Concept: A list is a linear collection of things that are organized in a linear way.
    • Features: The capacity of the list collection can grow as needed; The list collection can add or remove elements at the middle and end of the list.
    • Categories: Lists have three types of ordered, unordered, and indexed lists.
List in the 1.Java collection API
    • The list classes provided by the Java Collection API are primarily support index lists .
    • The Java Collection API provides the use of array implementations ArrayList类 and the use of linked lists LinkedList类 , which can store elements defined by the generic parameter E, and also implement the list interface.
    • Some methods in the list interface:
    • List ADT: List ADT contains some operations that are common to both ordered and unordered lists, including:
      • Removefirst: Removes the first element from the list.
      • Removelast: Removes the last element from the list.
      • Remove: Removes an element from the list.
      • First: View the elements that are at the front of the list.
      • Last: View the elements at the end of the list.
      • Contains: Determines whether the list contains an element.
      • IsEmpty: Determines whether the list is empty.
      • Size: Determines the number of elements in the list.
2. Ordered list
    • concept : A list whose elements are sorted according to an intrinsic characteristic of the element.
    • Only comparable objects can be stored in a sequence table.
    • Add an element to the list
      • Because a sequence table is sorted based on a key value of an element, for any element that is to be added to the list, it will have a fixed position in the list as long as the element's key value is given.
      • So to implement an operation that adds elements to a sequence list, you just need to set up an add operation. Because when you do this, it will itself determine the insertion in the head, middle, or tail of the list based on a comparison of the key values.
3. Unordered list
    • concept : A list whose elements are sorted according to their position in the list.
    • Note : unordered lists, which are called "No sequential lists", do not mean that they are randomly generated, but rather that they are arranged in a particular order , but this order is determined by the user, regardless of the nature of the element itself.
    • Add an element to the list
      • Because the order of unordered lists is determined by the consumer, to implement an unordered list to add elements, you need to provide the user with all the possibilities that can be added. That is, Addtofront (added at the front of the list) Addtorear (added at the end of the list) and addafter (adding elements to the list after an element) three operations.
4. Index List
    • concept : A list whose elements can be referenced by a numeric index.
    • An index list is similar to an unordered list, except that each element in the index list can be referenced by an index value starting at 0 .
    • the difference between an indexed list and an array : When you delete an element in an array, the location of the other elements does not change. When an element is removed from the index list, the other element's position is moved to eliminate the gap resulting from the deletion of the element.
Second, use the array to implement the list
    • Array-based implementation lists first, fix one end of the list at index 0, set an integer variable rear represents the number of elements in the list, and represent the next available location at the end of the list.
    • Remove Action : This action is used to delete the specified element in the list, in general, several comparisons and panning operations are required, at most (when the element to be deleted is the last element of the list) n comparisons and panning are required, so its time complexity is O (n).
      • The remove operation uses a find operation that is used to find out whether the specified element exists. The benefit of having the find operation stand-alone is three points: (1) makes the remove operation simple. (2) The find operation can also be applied to other operations, such as contains operations. (3) The method does not throw an exception, so the program calling the method can define its own operation when the element is not found.
    • contains action : This action is used to determine whether the specified element is in the list, so multiple comparisons are performed, at most (when the found element is not in the list to traverse the entire list) requires n comparison operations, so its time complexity is O (n).
    • Add action : Both the Addtoftont and Addafter operations for the add operation of the sequence table and the unordered list are similar to the remove operation and require multiple comparisons and panning, so their time complexity is O (n). The addtorear operation is similar to the push operation of the stack, with a time complexity of O (1).

      When using a ring array in a queue, you can change the efficiency of the dequeue from O (n) to O (1), because it does not need to translate the array, but in the list because we can arbitrarily add delete elements anywhere, the operation of the pan array cannot be omitted, so it is not important to use a ring array.

Third, the list is implemented with the linked lists
    • Using list implementation lists in some ways similar to the deque implemented in the previous chapter, many methods are the same.
    • Remove Action : The remove operation of a linked list does not require a translation element, but this operation may encounter four situations: the list has only one element, the first element of the list is deleted, the tail element of the list is deleted, and the middle element of the delete list is removed. In the case where there is still an n-time comparison operation, the time complexity is also O (n).
    • contains operation : The contains operation of a linked list needs to look for an element, so its content is very similar to the remove operation, and the first is to write a method that determines the target element, so its time complexity is also O (n).
Problems in teaching materials learning and the solving process
    • Question 1: What are some of the codes mentioned in the book instanceof ? How to use it?
public boolean equals(Object other)    {        boolean result = false;        if (other instanceof Course)        {            Course otherCourse = (Course) other;            if (prefix.equals(otherCourse.getPrefix()) &&                    number == otherCourse.getNumber()) {                result = true;            }        }        return result;    }
    • Problem 1 Solution: Check the JDK without fruit, the online explanation is: instanceof is a simple two-dollar operator, it is used to determine whether an object is a class instance. In short, it and the "= =" function somewhat similar, but not exactly the same, instanceof there are many qualifications, for example, not casually between two types can be used instanceof, if the type of two operands is a class, one must be another subtype.
boolean b1 = new String() instanceof String; //b1为trueboolean b2 = "Sting" instanceof Object;//b2为true,因为String是Object的子类boolean b3 = new Object() instanceof String; //b3为false,Object是父类boolean b4 = 'A' instanceof Character; //编译不通过,‘A’在此处视为基本数据类型char,instanceof操作符只能用作对象的判断boolean b5 = null instanceof String; //b5为false,这是instanceof特有的规则:若左操作数为null,结果就直接返回false,不再运算右操作数是什么类。boolean b6 = (String)null instanceof String; //b6为false,即使类型转换还是个 nullboolean b7 = new Date() instanceof String; //编译不通过,instanceof操作符的左右操作数必须有继承或实现关系,否则编译出错
Problems in code debugging and the resolution process
    • Issue 1: The Addtofront method encountered an error while doing an unordered list test of an array implementation.
    • Problem 1 Workaround: Debug finds that the condition I wrote in the For loop is wrong, causing the remaining elements to be equal to the second element in addition to the newly added element.

      This is due to the order of the conditions, which can be resolved by turning the original order into reverse.
    • question 2: In the test of a linked list implementation of an unordered list, Addafter always inserts the inserted element before the specified position rather than after it.

    • Problem 2 Workaround: The problem is with the element that is compared to target , if the element I set is temp , It refers to the next node of the node that is used to traverse the linked list, and when it points to the target element that is being looked for, the actual position to be inserted (that is, the node that current points to) is the previous one of the target element, so the result is that Addafter inserts the inserted element before the specified position instead of after. Change the element that is compared to target to current .
    • Issue 3: Popup nullpointerexception
    • Issue 3 workaround when Removelast and last actions are implemented using a list of linked lists: As this semester hit the code of the process encountered the most times the exception, now every time I see it I feel my heart is calm and want to fall computer intertwined . Because when I finished this Part I was referring to the Deque class that was written in the previous chapter, when its add and remove were written in a class, so tail would change. However, in the list, the remove operation and the add operation are not in a class, so if the tail in the LinkedList class is set to null from the beginning, it is a natural exception to be thrown if directly referenced.
//原last方法public T last() throws EmptyCollectionException    {        if (isEmpty()){            throw new EmptyCollectionException("LinkedList");        }        return tail.getElement();    }    //修改过的last方法public T last() throws EmptyCollectionException    {        if (isEmpty()){            throw new EmptyCollectionException("LinkedList");        }        LinearNode<T> temp = head;        T result = temp.getElement();        while (temp.getNext() != tail){            temp = temp.getNext();        }        tail = temp;        return tail.getElement();    }
Code Hosting

Last week's quiz summary (right green, error red) chapters 3rd and 4th
    • The wrong title 1:the implementation of the collection operations should affect the the-the-the-interact with the collection.
      • A. True
      • B. False
    • The wrong problem 1 solution: Do the problem when you feel that you have seen this statement, but the book did not find, and later in a good look at the book found in 37 pages and 44 pages have mentioned this sentence. Indeed, if the collection operation in the implementation of the process if the impact of interaction, such as the method with a linked list, the method with an array, then the results can be imagined to be very messy.
    • The wrong title 2:an array dynamically grows as needed and essentially has no capacity limitations.
      • A. True
      • B. False
    • The wrong question 2 solution: The problem is simply the wrong hand. Very angry oneself wrong two questions are not what problem, the wrong reason also is not knowledge point not grasp but careless, hereafter want to try to avoid this situation to happen again.
5th Chapter

There is no wrong question in this chapter.

Pair and peer review templates:
    • Blogs that are worth learning or questions:
      • Pros: The code is written with a very detailed record of the problem.
      • Question: Last week's error analysis should be two times, my pair of partners and the same as last semester forget one of the.
    • Something worth learning or doing in your code:
      • Question: It's still a commit, but it's a little more perfunctory this week without improvement ... Look at the situation so the code is commit all at once.
reviewed the classmates blog and code
    • This week's study of the knot
      • 20172322
      • Pairs of learning content
        • This paper mainly discusses how to use linked lists to implement ordered and unordered lists.
        • Help each other to solve the problems that occur during the operation.
Other (sentiment, thinking, etc., optional)
    • The early end of National Day holiday, fourth to do the classroom began to learn, probably because every day with a very large chunk of time to study, so that this week to learn the content is not very difficult, the process of learning is also very fun.
    • It is very gratifying that this time finally remember that in the process of finishing homework encountered problems in a timely manner, will not be as clear as before the process of the problem many but the final summary of the time nothing to think of.
    • Probably more than the process I value the results _ (: З"∠) _
Learning progress Bar
lines of code (new/cumulative) Blog Volume (Add/accumulate) Learning Time (new/cumulative) Important Growth
Goal 5000 rows 30 Articles 400 hours
First week 10/10 1/1 10/10
Second week 246/366 2/3 20/30
Third week 567/903 1/4 10/40
Week Four 2346/2712 2/6 20/60
    • Planned study time: 20 hours

    • Actual learning time: 20 hours

    • Improvement: Just see this week's code volume when really startled, can be in one weeks or so of time to knock 2000 lines of code, feeling still a bit strong _ (: З"∠) _

Resources
    • How does the instanceof in Java come true?

    • 3 Types of linear tables with linked lists (ordered linked list, unordered list, index linked list)

20172303 2018-2019-1 "program design and data Structure" 4th Week study Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.