Data structure lecture

61 358 0
Data structure lecture

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Data Structures Data Structures Chapter Contents Chapter Contents Chapter Objectives 12.1 Introductory Example: Counting Internet Addresses 12.2 The ArrayList and LinkedList Classes 12.3 Example: A Stack Application and Class 12.4 Example: A Queue Class 12.5 An Introduction to Trees Part of the Picture: Data Structures 12.6 Graphical/Internet Java: A PolygonSketcher Class Chapter Objectives Chapter Objectives  Study the Java collection classes, ArrayList and LinkedList  Show how to build collection classes  Study the stack and queue structures  Learn about linked structures  linked lists and binary trees  Implement and use linked structures  Discover how collection classes are used in graphical programming Review Arrays Review Arrays  An array stores a sequence of values type [] anArray = new type [ capacity ];  Drawback: – capacity of array fixed – must know max number of values at compile time – either the program runs out of space or wastes space  Solution: collection classes – capacity can grow and shrink as program runs 12.1 Introductory Example: 12.1 Introductory Example: Counting Internet Addresses Counting Internet Addresses  Internet TCP/IP addresses provide for two names for each computer – A host name, meaningful to humans – an IP address, meaningful to computers  Problem: – network administrator needs to review file of IP addresses using a network gateway  Solution: – read file of addresses – keep track of addresses and how many times each address shows up in the file Class Class AddressCounter AddressCounter  Note source code, Figure 12.1  Attributes – maximum message length – address – count  Methods – constructor – comparison method, equals() – count incrementer – accessors for address, count – to-string converter for output Class Class GatewayUsageCounter GatewayUsageCounter  Note source code, Figure 12.2  Purpose – counts IP addresses using an array list  Receives name of text file from args[0]  Action: – reads IP address from file – prints listing of IP addresses and access count for each  Note use of ArrayList class – can grow or shrink as needed 12.2 The 12.2 The ArrayList ArrayList and and LinkedList LinkedList Classes Classes  Collection classes provide capability to grow and shrink as needed  Categories of collection classes – Lists: store collection of items, some of which may be the same – Sets: store collection of items with no duplicates – Maps: store collections of pairs, each associates a key with an object  Note List methods, table 12.1 ArrayList ArrayList Class Class  Implements the List using an array – by using an Object array, can store any reference type – cannot directly store primitive types – can indirectly store such values by using instances of their wrapper types  Consider the declaration: ArrayList addressSequence = newArrayList(); AddressSeqeunce size array 0  Adding to Adding to addressSequence addressSequence  The command addressSequence.add(anAddressCounter); – appends anAddressCounter object to the sequence  The system will then … AddressSeqeunce size array 0 [0] [1] [2] . . . [m-1] 128.159.4.201 Allocate the array Make first element point to the AddressCounter 1 Update size attribute of the ArrayList [...]... can be added only at back of ArrayList – to insert a value and "shift" others after it requires extensive copying of values – similarly, deleting a value requires shifting  We need a slightly different structure to allow simple insertions and deletions – the LinkedList class will accomplish this The LinkedList Class  Given LinkedList alist = new LinkedList(); aList.add(new(integer(88)); aList.add(new(integer(77));... list Variations on Linked Lists  Lists can be linked doubly as shown  Lists can also be linked in one direction only – attribute would not need link to tail – node needs forward link and pointer to data only – last item in list has link set to null  Lists can be circularly linked – last node has link to first node Using a LinkedList  Solve the IP address counter to use LinkedList  Note source... addressSequence head  size tail 0  Adding to the Linked List  Results of command for first add addressSequence.add(anAddressCounter); addressSequence head size tail 0 •Successive adds • create more nodes and data values •adjust links   123.111.345.444, 1 Accessing Values in a Linked List  Must use the get method ((AddressCounter) addresssSequence.get(index)).incrementCount();  A LinkedList has no array... addressSequence.add(n, new anAddressCounter); Which will … – build a new node – update head and tail links if required – update node handle links to place new node to be nth item in the list – allocates memory for the data item Choosing the Proper List Algorithm Efficiency  "Time-efficiency" is not a real-time issue – rather an issue of how many steps an algorithm requires  Linear time – time proportional to n – referred... "order n"  Constant time – expressed as O(1) Demonstration of Efficiency  Note sample program ListTimer, Figure 12.4, demonstrates performance  Observations – appending to either ArrayList or LinkedList structures takes negligible time – far more time-consuming to access middle value in a LinkedList than an ArrayList – far more time consuming to insert values into an ArrayList than a LinkedList Conclusions... choice  Implement as an ArrayList attribute – poor choice – adding values at one end, removing at other end require multiple shifts Implementing a Queue Class  Build a Queue from scratch – build a linked structure to store the queue elements  Attributes required – handle for the head node – handle for tail node – integer to store number of values in the queue – use SinglyLinkedNode class, source code, . Data Structures Data Structures Chapter Contents Chapter Contents Chapter Objectives 12.1 Introductory Example:. collection classes  Study the stack and queue structures  Learn about linked structures  linked lists and binary trees  Implement and use linked structures  Discover how collection classes. Application and Class 12.4 Example: A Queue Class 12.5 An Introduction to Trees Part of the Picture: Data Structures 12.6 Graphical/Internet Java: A PolygonSketcher Class Chapter Objectives Chapter

Ngày đăng: 24/10/2014, 01:17

Mục lục

  • 12.1 Introductory Example: Counting Internet Addresses

  • 12.2 The ArrayList and LinkedList Classes

  • Enlarging the AddressSequence Array

  • Variations on Linked Lists

  • Adding to the Linked List

  • Accessing Values in a Linked List

  • Inserting Nodes Anywhere in a Linked List

  • Choosing the Proper List Algorithm Efficiency

  • 12.3 Example: a Stack Application and Class

  • Need for a Stack

  • Number Base Conversion Algorithm

  • Implementing a Stack Class

  • Java's Stack Class

  • 12.4 Example: Building a Queue Class

  • Queues in a Computer System

  • Implementing a Queue Class

  • 12.5 An Introduction to Trees

  • Examples of Binary Trees

  • Tree Traversal is Recursive

  • Constructing Binary Search Trees

Tài liệu cùng người dùng

Tài liệu liên quan