Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.

Similar presentations


Presentation on theme: "CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine."— Presentation transcript:

1 CHAPTER 8 Lists

2 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine three types of list collections: ordered lists unordered lists indexed lists

3 3 Ordered Lists The elements in an ordered list are ordered by some inherent characteristic of the elements names in alphabetical order scores in ascending order Adding an element must preserve the order

4 4 A conceptual view of an ordered list

5 5 Unordered Lists Unordered lists: elements are ordered by their placement in the list A new element can be put on the front or the rear of the list, or it can be inserted after a particular element already in the list

6 6 A conceptual view of an unordered list

7 7 Indexed Lists Indexed list: elements are referenced by their numeric position in the list There is no inherent relationship among the elements Every time the list changes, the indexes are updated

8 8 A conceptual view of an indexed list

9 9 List Operations Add and Remove elements Elements can be added to or removed from any position in the list Examine elements retrieve the contents of the first, last, or specified element Check the status of the list Get the size of the list; check if it is empty The key differences between the list types involve the way elements are added

10 10 List ADT Notation: L - list e - item of the same type as the information part of an element (a node) in L b - boolean value terminology: head: first node tail: last node current: current node

11 11 Create / Destroy a list InitList(L) Procedure to initialize the list L to empty. head = tail = current = null DestroyList(L) Procedure to make an existing list L empty. Remove all nodes and pointers (performed automatically in Java) MakeEmptyList(L) Procedure to make an existing list L empty. head = tail = current = null

12 12 Expand / Shrink the list Insert(L,e) Procedure to insert a node with information e before the current position, or if L is empty - as the only node in L. Preconditions: If list is not empty, current is not empty (i.e. current is not null) Postconditions: current = new node InsertAfter(L,e) Procedure to insert a node with information e after the current position, or if L is empty - as the only node in L. Preconditions: If list is not empty, current is not empty (i.e. current is not null) Postconditions: current = new node

13 13 Expand / Shrink the list Delete(L) Procedure to delete the current node in L and to have the current position indicate the next node. Preconditions: current is not empty (i.e. current is not null) Postconditions: current becomes the node after the deleted node (empty if the deleted node was the last node.

14 14 Read / Write operations StoreInfo(L,e) Procedure to update the information part of the current node Preconditions: current is not empty (i.e. current is not null) Postconditions: current has new information RetrieveInfo(L)  e Procedure to return the information in the current node. Preconditions: current is not empty (i.e. current is not null) Postconditions: none

15 15 Read / Write operations RetrieveNextInfo(L)  e Procedure to return the information in the node after the current node. Preconditions: current is not empty (i.e. current is not null). current.getNext() is not empty Postconditions: none

16 16 Changing the current node (moving along the list) Advance(L) Preconditions: current is not empty (i.e. current is not null). Postconditions: current = current.getNext() ToFirst(L) Preconditions: none Postconditions: current = head Tolast(L) Preconditions: none Postconditions: current = tail

17 17 Report the current position AtFirst(L)  b Returns TRUE if the current node is the first node. (the head) AtEnd(L)  b Returns TRUE if the current node is the last node (the tail)

18 18 Report status of the list Contains(L,e)  b Returns TRUE if the list contains an element with information e ListEmpty(L)  b Returns TRUE if the list is empty ListFull(L)  b Returns TRUE if the list is full (array implementation) CurrentEmpty(L)  b Returns TRUE if the current position is empty. Size(L)  int Returns the number of elements in the list

19 19 Common operations on a list

20 20 The operation particular to an ordered list

21 21 The operations particular to an unordered list

22 22 The operations particular to an indexed list

23 23 List Operations Define the common list operations in one interface Derive three others from it that define the interfaces of the three list types

24 24 The various list interfaces

25 25 Implementations Ordered list Unordered list Indexed list Array implementations Linked implementations Single linked list Double linked list

26 26 Array implementation of unordered list Representation T [ ] list; // array of type T int head; // index of the first element // used only if circular array int tail ; // index of the last element int count; // number of elements int Capacity = 100

27 27 Array implementation of unordered list Constructor list = new T [ Capacity ] head = 0 // used only if circular array tail = 0 count = 0 Capacity: default or specified in the declaration  Two constructors needed

28 28 Operations: addToFront /O(N)/ void addToFront (T element) Expand capacity if necessary Shift all elements one position to the right Store the new element at list[head] Increment count Increment tail Can you make addToFront O(1))?

29 29 Operations: addToRear /O(1)/ void addToRear (T element) Expand capacity if necessary Increment tail Store the new element at list[tail] Increment count

30 30 Operations: addAfter /O(N)/ void addAfter (T element1, T element2) Expand capacity if necessary Find index k of element2 Shift all elements one position to the right Store the new element at list[k+1] Increment count Increment tail


Download ppt "CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine."

Similar presentations


Ads by Google