The List, Stack, and Queue ADTsAbstract data type (ADT)- An.ppt
《The List, Stack, and Queue ADTsAbstract data type (ADT)- An.ppt》由会员分享,可在线阅读,更多相关《The List, Stack, and Queue ADTsAbstract data type (ADT)- An.ppt(17页珍藏版)》请在麦多课文档分享上搜索。
1、The List, Stack, and Queue ADTs Abstract data type (ADT): An ADT is a set of objects together with a set operations to be performed on these objects.An ADT corresponds to (one or more) classes in an OO language such as C+ and Java.There may be more than one implementation for the same ADT, with diff
2、erent advantages in terms of efficiency, ease of use, ease of implementation. We will review the ADTs for lists, stacks, and queues, mainly based on array or linked list implementations. We will also study some typical applications of these ADTs to learn when to use them.,The List ADT:A list of obje
3、cts A1, A2, , An, where n is the size of the list. Some typical operations include find, insert, remove, findKth, printList, makeEmpty.An array implementation requires (n) time for insert and remove operations in the worst case.A linked list implementation offers the flexibility of dynamically growi
4、ng the list and of O(1) time insert and remove operations (assuming the position in the list is known).,remove,insert,Programming Details in Linked List Implementation:Use three classes for the List ADT:(1) ListNode (for the individual nodes, or objects, in the list); (2) LinkedList (for the list ob
5、ject itself consisting of the nodes); and (3) LinkedListItr (for the current location in a LinkedList object, in a particular application).Each class consists of one or more constructor functions.The ListNode class contains two member variables: element for the “data” field and next for the link fie
6、ld lining to the next node in the list.,Linked List Implementation (contd):The LinkedListItr class contains functions isPastEnd, retrieve, advance; and contains a member variable current for the current location in a LinkedList object.The LinkedList class may use a dummy header node to be the first
7、node in the list which somewhat simplifies the insert function (thus, an “empty” list contains the header node as the only node in the list). The functions included in the class are find, remove, findPrevious, and insert. Variations of linked list implementations includedoubly linked lists each node
8、 uses two link fields to link to the two neighboring nodes in the list, respectively; andcircular linked lists the last node in the list is linked to the front of the list, eliminating the header node.,Two Applications of Linked Lists: Polynomials (in a single variable x) use nodes to represent the
9、non-zero terms arranged in decreasing order of the exponents; typical operations include: constructor (initialize the zero polynomial in constant time); add (in O(m + n) time for two polynomials of m and n terms, respectively); and multiply (in O(mn(min(m,n) time for two polynomials of m and n terms
10、, respectively). Radix sort sort n numbers in the range of 0m1, where m bp for some constant p (of moderate size), based on a multi-pass bucket sort technique, resulting in O(p(n + b) time using O(b+ n) space; this is a linear time O(n) algorithm when p is a constant and b = O(n). For example, m = 2
11、32 for 32-bit unsigned integers. Choose b = 211 = 2048, then m b3 and n numbers can be sorted in O(3(n + 211) = O(n) time.,Algorithm for adding two polynomials in linked lists p, q:set p, q to point to the two first nodes (no headers) initialize a linked list r for a zero polynomial while p != null
12、and q != null if p.exp q.exp create a node storing p.coeff and p.exp, then insert at the end of list r; advance p else if q.exp p.exp create a node storing q.coeff and q.exp, then insert at the end of list r; advance q else if p.exp = q.exp if p.coeff + q.coeff != 0 create a node storing p.coeff + q
13、.coeff and p.exp, then insert at the end of list r advance p, q if p != null copy the remaining terms of p to end of r else if q != null copy the remaining terms of q to end of r Each iteration of the while loop takes O(1) time, moving at least one of the two pointers p and q one position over. Any
14、remaining terms will be copied after the loop. Thus the total time is O(m+n) because there are a total of m+n terms in the two polynomials.,Algorithm for multiplying two polynomials in linked lists p, q:set p to the first node (no header) of one polynomial initialize a linked list r for a zero polyn
15、omial while p != null copy list q to a new list t multiply the term pointed by p to each term of t by set t.exp += p.exp set t.coeff *= p.coeff add polynomial t to polynomial r (then discard t) advance p If polynomial p, q have m, n terms, respectively, the while loop runs m iterations. In each iter
16、ation, producing the polynomial t (copy and multiply) costs O(n) time since the length of t is O(n). Adding polynomial t to r runs in time O(n + length of r). Since the length of r in the beginning of iteration k is O(k 1)n), so the total time of the algorithm is The choice of m and n is arbitrary,
17、so we can achieve O(mn(min(m,n) time.,An Example demonstrating Radix Sort: Given n = 10 numbers: 64, 8, 216, 512, 27, 729, 0, 1, 343, 125. Use b = 10 buckets and sort the numbers using the least significant digits in the first pass (i.e., distribute the numbers into10 buckets based on the last digit
18、s), then sort the list of numbers resulting from the first pass using the next significant digits; finally, sort the list (of pass two) using the most significant digits.,0 1 2 3 4 5 6 7 8 9 000 001 512 343 064 125 216 027 008 729,008 729 001 216 027 000 512 125 343 064,10 buckets,list after first p
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- THELIST STACK ANDQUEUEADTSABSTRACTDATATYPEADTANPPT

链接地址:http://www.mydoc123.com/p-373299.html