Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linear (or Ordered) Lists instances are of the form (e 0, e 1, e 2, …, e n-1 ) where e i denotes a list element n >= 0 is finite list size is n.

Similar presentations


Presentation on theme: "Linear (or Ordered) Lists instances are of the form (e 0, e 1, e 2, …, e n-1 ) where e i denotes a list element n >= 0 is finite list size is n."— Presentation transcript:

1

2 Linear (or Ordered) Lists instances are of the form (e 0, e 1, e 2, …, e n-1 ) where e i denotes a list element n >= 0 is finite list size is n

3 Linear Lists L = (e 0, e 1, e 2, e 3, …, e n-1 ) relationships e 0 is the zero’th (or front) element e n-1 is the last element e i immediately precedes e i+1

4 Linear List Examples Students in COP3530 = (Jack, Jill, Abe, Henry, Mary, …, Judy) Exams in COP3530 = (exam1, exam2, exam3) Days of Week = (S, M, T, W, Th, F, Sa) Months = (Jan, Feb, Mar, Apr, …, Nov, Dec)

5 Linear List Operations—Length() determine list size L = (a,b,c,d,e) length = 5

6 Linear List Operations—Find(theIndex,x) get element with given index L = (a,b,c,d,e) Find(0,x)  x=a, return True Find(2,x)  x=c, return True Find(-1,x)  return False Find(9,x)  return False

7 Linear List Operations— Search(theElement) determine the index of an element L = (a,b,d,b,a) Search(d) = 2 Search(a) = 0 Search(z) = -1

8 Linear List Operations— remove(theIndex) remove and return element with given index L = (a,b,c,d,e,f,g) remove(2) returns c and L becomes (a,b,d,e,f,g) index of d,e,f, and g decrease by 1

9 Linear List Operations— Delete(theIndex,x) remove and return element with given index L = (a,b,c,d,e,f,g) Delete(1,x) => x=b, return (a,c,d,e,f,g) Delete(20,x) => return False

10 Linear List Operations— Insert(theIndex, theElement) add an element so that the new element has a specified index L = (a,b,c,d,e,f,g) Insert(0,h) => L = (h,a,b,c,d,e,f,g) index of a,b,c,d,e,f, and g increase by 1

11 Linear List Operations— Insert(theIndex, theElement) L = (a,b,c,d,e,f,g) Insert(2,h) => L = (a,b,h,c,d,e,f,g) index of c,d,e,f, and g increase by 1 Insert(10,h) => error Insert(-6,h) => error

12 Data Structure Specification  Language independent  Abstract Data Type  C++  Class  Java  Interface  Abstract Class

13 Linear List Abstract Data Type

14 Example of Linear List

15 Linear List – Array Representation An interface may include constants and abstract methods (i.e., methods for which no implementation is provided).

16 Linear List – Array Representation

17 Implemetation of LinearList

18 LinearList – Find, Search An abstract class may include constants, variables, abstract methods, and nonabstract methods.

19 LinearList -- Delete public abstract class LinearListAsAbstractClass { public abstract boolean isEmpty(); public abstract int size(); public abstract Object get(int index); public abstract int indexOf(Object theElement); public abstract Object remove(int index); public abstract void add(int index, Object theElement); public abstract String toString(); }

20 LinearList -- Insert

21 LinearList -- Output public class ArrayLinearList extends LinearListAsAbstractClass { // code for all abstract classes must come here }

22 Evaluation

23 Evaluation – cont.

24 Result


Download ppt "Linear (or Ordered) Lists instances are of the form (e 0, e 1, e 2, …, e n-1 ) where e i denotes a list element n >= 0 is finite list size is n."

Similar presentations


Ads by Google