Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.

Similar presentations


Presentation on theme: "Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010."— Presentation transcript:

1 Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010

2 Consider Every Day Lists Groceries to be purchased Job to-do list List of assignments for a course Dean's list Can you name some others??

3 List List is a collection of things It is homogeneous --- the elements are all of the same type It has finite length The elements are arranged sequentially

4 LIST ADT Collection of data elements A sequence with a finite number of data items, all of the same type Basic operations Construction---create a empty list Empty---check if the list is empty Insert---insert an item Delete---remove a item Traverse---go through the list or part of it

5 Building a List class It consists of two steps: Design the class Implement the class

6 Implementation of the List In this chapter, two approaches to implement List: Array Based Static array Dynamic array Pointer (Link list) Based

7 Static Array based An array is a common choice for storing list elements Element are sequential Most languages support array Algorithm development is easy An array thus seems to be a natural storage structure for implementing a list

8 Static Array based Normally sequential orderings of list elements match with array elements Of course, the array must be large enough to hold all of the list elements It means we need to chose carefully

9 Static Array based Three variables we use to tract array: Array--the array that stores the list Capacity--array’s capacity Size--the number of list elements that are stored in the array Constructor: Since we use static array, we can let the complier handle the memory allocation, and our constructor only need to set size to 0 Empty: only need to check whether size is 0

10 Static Array based Traverse: Lists are easy traversed using a loop: for index i ranging from 0 to size-1: process array[i]

11 Static Array based Insert: it’s more complicated For example: if we wish to insert the new value 56 after the element 48: Original--- 23, 25, 34, 48, 61, 79, 82, 89, 91, 99 Produce--- 23, 25, 34, 48, 56, 61, 79, 82, 89, 91, 99

12 Static Array based Insert: we need to do Check the capacity Need to move array elements to make room for the new element

13 Static Array based Algorithm for insert: If size is equal to capacity: display error due to limit space If (pos size): display error due to error position Else: for i in ranging from size to pos+1: array[i]=array[i-1] //C++ starts index from 0 array[pos]=item size++

14 Static Array based The efficiency of this insert algorithm obviously depends on the number of array elements that must be shifted to make room for the new item In worst case, the new item must be inserted at the beginning of the list O(n) The best case occurs when the new item is inserted at the end of it O(1) Two important special kinds of lists for which are stacks and queues

15 Static Array based Delete: also requires shifting array elements For example, to delete the second item in: 23, 25, 34, 48, 56, 61, 79, 82, 89, 91, 99

16 Static Array based Algorithm for delete: If size is zero: issue a error message If (pos =size): issue an error message Otherwise: for index i ranging from pos to size-2: array[i]=array[i+1] size--

17 Static Array based The computing time of such a function is easily seen to be the same as insert function --- O(n) in the worst case and O(1) for the best

18 List Implantation by Linked list In any structure used to store the elements of a list, it must be possible to perform at least the following operation: 1. Locate the First element 2. Given the location of any list element, find its successor 3. Locate the end of the list

19 Linked List Linked list nodes contain Data part – stores an element of the list Next part – stores link/pointer to next element (when no next element, null value)

20 Design the list class Should contain at least the following function members Constructor empty() insert() delete() display()

21 Construction To construct an empty list, we simply make first a null link to indicate that it does not refer to any node: first = null_value ;

22 Empty We can then perform the Empty operation--- determining whether a list is empty, simply by checking whether first is null: first == null_value ?

23 Traverse We begin by initializing an auxiliary variable ptr to point to the first node: Initialize a variable ptr to point to first node Process data where ptr points

24 Traverse Traverse (ctd) set ptr = ptr->next, process ptr->data Continue until ptr == null

25 Insertion To insert a new data value into a linked list, we must first obtain a new node and store the value in its data part The second step is to connect this new node to existing list Two cases in this situation: (1) insertion after some element in the list and (2) insertion at the beginning of the list

26 Insertion To insert 20 after 17 Need address of item before point of insertion predptr points to the node containing 17 Get a new node pointed to by newptr and store 20 in it Set the next pointer of this new node equal to the next pointer in its predecessor, thus making it point to its successor. Reset the next pointer of its predecessor to point to this new node 20 newptr predptr

27 Insertion Note: insertion also works at end of list pointer member of new node set to null Insertion at the beginning of the list predptr must be set to first pointer member of newptr set to that value (Where first points to) first set to value of newptr In all cases, no shifting of list elements is required !

28 Deletion For deletion, there are also two cases to consider: Deleting an element that has a predecessor Delete the first element in the list

29 Deletion Delete node containing 22 from list. Suppose ptr points to the node to be deleted predptr points to its predecessor (the 17) Do a bypass operation : Set the next pointer in the predecessor to point to the successor of the node to be deleted Deallocate the node being deleted. predptr ptr To free space

30 Deletion The second case is easier Just set the first points to the second node in the list and then returning the deleted node to the storage pool

31 Linked Lists - Advantages Access any item as long as external link to first item maintained Insert new item without shifting Delete existing item without shifting Can expand/contract as necessary

32 Linked Lists - Disadvantages No longer have direct access to each element of the list Many sorting algorithms need direct access Binary search needs direct access Access of n th item now less efficient must go through first element, and then second, and then third, etc.

33 Outline linked lists implementation An Array-Based Implementation of Linked Lists

34 Pointer Based Node Since each node has two different parts, a data part and a next part, it is nature to have a node class with two data members. class Node { ElementType data; Node *next; }

35 Pointer Based Node To declare a pointer to a node: Node *ptr To allocate a new node pointer to by ptr: ptr = new Node; ptr = new Node(data_nalue); ptr = new Node(data_value, link_value); To access the data nad next part of the node pointed to by ptr: ptr -> data ptr -> next

36 Array-Based Implementation of Linked Lists Given a list with names Implementation would look like this

37 How to do it??? First of all, define a 2D array int array[10][2];

38 How to do it??? Constructor( ) we make “first” variable equals to 7 to indicate we start with position 7 first ==7; How to choose a start point??

39 How to do it??? Put 88 into data position and set next position to NULL array[first][0]=88; array[first][1]=-1; size==1;

40 How to do Insertion??? Find a empty position (in this example it’s node 1) Insert(array, 1) { ptr==1; // find a empty position pre_ptr==7; // you need a function to find pre_ptr array[new_node][0]==54; array[new_node][1]==array[pre_ptr][1]; array[pre_ptr][1]==ptr; size++; }

41 Insertion To insert 20 after 17 Need address of item before point of insertion predptr points to the node containing 17 Get a new node pointed to by newptr and store 20 in it Set the next pointer of this new node equal to the next pointer in its predecessor, thus making it point to its successor. Reset the next pointer of its predecessor to point to this new node 20 newptr predptr

42 How to do Deletion???

43 Implementation Details For Insertion, there are also two cases to consider: insertion after some element in the list and insertion at the beginning of the list For deletion, there are also two cases to consider: Deleting an element that has a predecessor Delete the first element in the list


Download ppt "Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010."

Similar presentations


Ads by Google