Presentation is loading. Please wait.

Presentation is loading. Please wait.

A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.

Similar presentations


Presentation on theme: "A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order."— Presentation transcript:

1 A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order to do that, we need to identify: 1. The collection of data items 2. Basic operation that must be performed on them Abstract Data Type (ADT): a collection of data items together with the operations on the data

2 Abstract Data Type (ADT) The word “abstract” refers to the fact that the data and the basic operations defined on it are being studied independently of how they are implemented We think about what can be done with the data, not how it is done

3 Chapter 6 Lists Dr. Bernard Chen Associate Professor University of Central Arkansas

4 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??

5 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

6 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

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

8 Design the list class Designing a class to implement an ADT consist of identifying “functions” members that are need to be carry out It is important that we describe the function members independently of how the class will be implemented Function members must be describe in some manner that does not depend on any particular representation of the objects

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

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

11 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

12 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

13 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

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

15 Static Array based Insert: it’s more complicated For example: if we wish to insert the new value 56 at index 4: Original--- 0 1 2 3 4 5 6 7 8 9 23, 25, 34, 48, 61, 79, 82, 89, 91, 99 Produce--- 23, 25, 34, 48, 56, 61, 79, 82, 89, 91, 99

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

17 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 range from size to pos+1: array[i]=array[i-1] //C++ starts index from 0 array[pos]=item size++

18 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

19 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

20 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--

21 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

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

23 Linked List Data part – stores an element of the list Next part – stores link/pointer to next element class node { public: int data; node* next; };

24 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)

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

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

27 Construction PointerList() { top = NULL; count=0; }

28 Empty We can then perform the Empty operation--- determining whether a list is empty, simply by checking whether first is null: bool empty() { if(top==NULL) return true; else return false; }

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

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

31 Traverse void print() { node *temp; temp = top; while(temp!=NULL) { cout data<<","; temp=temp->next; }

32 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

33 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

34 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 !

35 Insertion (normal case) void insert(int position, int element) { node *preptr; preptr = top; for(int i=0;i<position-1;i++) preptr=preptr->next; newptr->next = preptr->next; preptr->next = newptr; }

36 void insert(int position, int element) { node *newptr = new node; newptr->data = element; if(top == NULL)//insert the very first element if(position==0) { newptr->next = NULL; top = newptr; } else cout<<"Location Error!!"; else if(position==0)//insert on the first position when some elements existed { newptr->next = top; top = newptr; } else//most cases belongs to this situation (as showed in the class slide) { node *preptr; preptr = top; for(int i=0;i<position-1;i++) preptr=preptr->next; newptr->next = preptr->next; preptr->next = newptr; }

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

38 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

39 Deletion void remove(int position) { node *preptr; preptr = top; for(int i=0;i<position-1;i++) preptr=preptr->next; node * ptr = preptr->next; preptr->next = ptr->next; delete(ptr); }

40 Deletion Complete void remove(int position) { if(position==0)//delete the first element { node * ptr = top; top = ptr->next; delete(ptr); } else { node *preptr; preptr = top; for(int i=0;i<position-1;i++) preptr=preptr->next; node * ptr = preptr->next; preptr->next = ptr->next; delete(ptr); }

41 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

42 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.


Download ppt "A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order."

Similar presentations


Ads by Google