220 likes | 380 Views
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.
E N D
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
A Node NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95
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?
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
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.
How the user would use createEmptyList() usersList = createEmptyList()
access(uList, i) return list[i]
length(uList) return n
isEmptyList(uList) if n == 0 return true else return false
isEmptyList(uList) return n == 0
insert(uList, x) list[n] = x n = n + 1 return uList
searchFor(uList, key) for j = 0; j < n; j++ if list[j].key == key return j return -1
remove(uList, i) n = n - 1 list[i] = list[n] return uList
inserti(uList, i, x) list[n] = list[i] list[i] = x n = n + 1 return uList
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
concat(uList1, uList2) if n1 == 0 return uList2 for j = 0; j < n2; j++ list1[j + n1] = list2[j] return uList1
searchFor(uList, key) ordered list 2 3 5 4 1
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)
remove(uList, i) for j = i; j < n; j = j + 1 list[j] = list[j + 1] n = n - 1 return uList
A Node Pointer NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95
A Node With Pointer NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95
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?