1 / 22

Lists

Lists. List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1 <= the key of x i for all i where 0 < i < n. The sort symbol <= can be replaced by >= or any other function that determines ordering in the keys.

sally
Download Presentation

Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lists List L = x0 x1 x2 x3 … xn-1 n = # elements If a list is ordered than the key of xi-1 <= the key of xi for all i where 0 < i < n. The sort symbol <= can be replaced by >= or any other function that determines ordering in the keys. An unordered list does not have this restriction. Functions: access(L, i) returns xi length(L) returns n concat(L1, L2) returns a new list with L2 concatenated on to L1 createEmptyList() returns a newly created empty list isEmptyList(L) returns true if L is empty and false if it is not searchFor(L, key) returns i where key of xi = key remove(L, i) returns a list with xi removed; the old xi+1 is now xi, etc. inserti(L, i, x) returns a list with x inserted as xi; the old xi is now xi+1, etc. insert(L, x) returns a list with x added to L sort(L) returns the list in sorted order

  2. A Node NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95

  3. Homework 0 • Describe how to use an array to implement an unordered list (assume a max size of 100 elements). • Determine how to do the following functions: access, length, concat, createEmptyList, isEmptyList, searchFor, remove, and insert. • How would any of these functions change if the list was to be ordered?

  4. Array NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95

  5. createEmptyList() Declare an array of type node array node list[100] Declare an int indicating the number of elements in the array int n = 0 Return a pointer to the object that is in the class List.

  6. How the user would use createEmptyList() usersList = createEmptyList()

  7. access(uList, i) return list[i]

  8. length(uList) return n

  9. isEmptyList(uList) if n == 0 return true else return false

  10. isEmptyList(uList) return n == 0

  11. insert(uList, x) list[n] = x n = n + 1 return uList

  12. searchFor(uList, key) for j = 0; j < n; j++ if list[j].key == key return j return -1

  13. remove(uList, i) n = n - 1 list[i] = list[n] return uList

  14. inserti(uList, i, x) list[n] = list[i] list[i] = x n = n + 1 return uList

  15. inserti(uList, i, x) for j = n; j > i; j = j - 1 list[j] = list[j – 1] list[i] = x n = n + 1 return uList

  16. concat(uList1, uList2) if n1 == 0 return uList2 for j = 0; j < n2; j++ list1[j + n1] = list2[j] return uList1

  17. searchFor(uList, key) ordered list 2 3 5 4 1

  18. insert(uList, x)ordered list for j = 0; j < n; j++ if list[j].key >= x_key return inserti(uList, i, x) return inserti(uList, n, x)

  19. remove(uList, i) for j = i; j < n; j = j + 1 list[j] = list[j + 1] n = n - 1 return uList

  20. A Node Pointer NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95

  21. A Node With Pointer NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95

  22. Homework 1 • Describe how to use a set of nodes with pointers (as described in class) to implement a variable length unordered list. • Determine how to do the following functions: access, length, concat, createEmptyList, isEmptyList, searchFor, remove, inserti, and insert. • How would any of these functions change if the list was to be ordered?

More Related