Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 340Chapter 3: Lists, Stacks, and Queues1 Abstract Data Types An ADT is a set of operations upon a set of data. Implementation details are not specified.

Similar presentations


Presentation on theme: "CS 340Chapter 3: Lists, Stacks, and Queues1 Abstract Data Types An ADT is a set of operations upon a set of data. Implementation details are not specified."— Presentation transcript:

1

2 CS 340Chapter 3: Lists, Stacks, and Queues1 Abstract Data Types An ADT is a set of operations upon a set of data. Implementation details are not specified in an ADT. Implementation details are not specified in an ADT. The program designer determines the operations that are needed and the specific data that will be used in the implementation. The program designer determines the operations that are needed and the specific data that will be used in the implementation. The implementation of the ADT should be easy to modify, and such modifications should be transparent to any code deploying the ADT. The implementation of the ADT should be easy to modify, and such modifications should be transparent to any code deploying the ADT. An ADT is a set of operations upon a set of data. Implementation details are not specified in an ADT. Implementation details are not specified in an ADT. The program designer determines the operations that are needed and the specific data that will be used in the implementation. The program designer determines the operations that are needed and the specific data that will be used in the implementation. The implementation of the ADT should be easy to modify, and such modifications should be transparent to any code deploying the ADT. The implementation of the ADT should be easy to modify, and such modifications should be transparent to any code deploying the ADT.

3 CS 340Chapter 3: Lists, Stacks, and Queues2 ADT #1: The List A list is a finite ordered collection of items of the same type. Common list operations include: Emptying the entire list Emptying the entire list Determining whether the list is empty Determining whether the list is empty Determining the size of the list Determining the size of the list Determining the location of a particular list element Determining the location of a particular list element Determining the value of an element at a particular location in the list Determining the value of an element at a particular location in the list Inserting a new list element at a particular location Inserting a new list element at a particular location Removing a particular element from the list Removing a particular element from the list Outputting the entire list Outputting the entire list A list is a finite ordered collection of items of the same type. Common list operations include: Emptying the entire list Emptying the entire list Determining whether the list is empty Determining whether the list is empty Determining the size of the list Determining the size of the list Determining the location of a particular list element Determining the location of a particular list element Determining the value of an element at a particular location in the list Determining the value of an element at a particular location in the list Inserting a new list element at a particular location Inserting a new list element at a particular location Removing a particular element from the list Removing a particular element from the list Outputting the entire list Outputting the entire list

4 CS 340Chapter 3: Lists, Stacks, and Queues3 An Array Implementation’s Performance An Array Implementation’s Performance List Implementation Option: An Array a1a1a1a1 a1a1a1a1 a2a2a2a2 a2a2a2a2 a3a3a3a3 a3a3a3a3 :: a n-2 a n-1 anananan anananan ?? ?? :: ?? nn Problems with the array implementation: Problems with the array implementation: –Data movement really slows down insertions to and removals from the list –The maximum list size must be specified Problems with the array implementation: Problems with the array implementation: –Data movement really slows down insertions to and removals from the list –The maximum list size must be specified Emptying the list O(1)O(1) Determining if the list is empty O(1)O(1) Determining the size of the list O(1)O(1) Determining the location of a particular element O(n)O(n) Determining the value of an element in a particular location O(1)O(1) Inserting a new element into a particular location O(n)O(n) Removing a particular element O(n)O(n) Outputting the list O(n)O(n)

5 CS 340Chapter 3: Lists, Stacks, and Queues4 A Linked List Implementation’s Performance A Linked List Implementation’s Performance List Implementation Option: A Linked List Problems with the linked list implementation: Problems with the linked list implementation: –Pointers consume memory not needed with arrays –Lack of indexing necessitates repeated list traversals (e.g., binary search is impossible) Problems with the linked list implementation: Problems with the linked list implementation: –Pointers consume memory not needed with arrays –Lack of indexing necessitates repeated list traversals (e.g., binary search is impossible) a1a1a1a1 a1a1a1a1 a2a2a2a2 a2a2a2a2 a3a3a3a3 a3a3a3a3 :: a n-2 a n-1 anananan anananan Emptying the list O(n)O(n) Determining if the list is empty O(1)O(1) Determining the size of the list O(n)O(n) Determining the location of a particular element O(n)O(n) Determining the value of an element in a particular location O(n)O(n) Inserting a new element into a particular location O(1)O(1) Removing a particular element O(1)O(1) Outputting the list O(n)O(n)

6 CS 340Chapter 2: Algorithm Analysis5 Performance - Compression and Caching Effects n Compression –Compress bitmaps for optimal space (core memory and disk storage) –Perform bitmap operations on compressed bitmaps. n Caching –Locality of Reference principle –Keep record of location of recent bits referenced –Cache area is relatively small –Each bitmap has its own cache area

7 CS 340Chapter 2: Algorithm Analysis6 Effect of cache on loading bitmaps

8 CS 340Chapter 2: Algorithm Analysis7 Effect of cache on OR bitmap operations

9 CS 340Chapter 2: Algorithm Analysis8 Effect of cache on AND bitmap operations

10 CS 340Chapter 3: Lists, Stacks, and Queues9 #ifndef LIST_H #include template class list { protected: struct node { Etype element; node *next; node(Etype e = 0, node *n = NULL) : element(e), next(n) {} }; node *head; node *current; void deleteList(); public: list(): head(new node), current(head) {} virtual ~list() { deleteList(); } const list& operator = (list &value); const list& operator ++ (); boolean operator ! () const; const Etype& operator () () const; boolean isEmpty() const { return (head->next == NULL); } virtual boolean find(const Etype &x); virtual boolean findPrevious(const Etype &x); void first() { if (head->next != NULL) current = head->next; } void header() { current = head; } boolean remove(const Etype &x); virtual void insert(const Etype &x); virtual void insertAsFirstElement(const Etype &x); }; Linked List Implementation in Visual C++ Class Template Structure Structure Constructor Class Constructor Class Destructor Virtual Function: Derived classes may have their own version of this function Constant Return: Value returned is treated as a constant Constant Modifier: This operator accesses but doesn’t modify In-Line Code

11 CS 340Chapter 3: Lists, Stacks, and Queues10 // Member function to free all memory associated with the linked list. template void list :: deleteList() { node *p = head->next; node *temp; while (p != NULL) { temp = p->next; delete p; p = temp; } delete head; } // Assignment operator: duplicates parameterized linked list. template inline const list & list :: operator = (list &value) { if (this == &value) return *this; deleteList(); current = head = new node; for (value.first(); !value; ++value) { current->next = new node(value(), current->next); current = current->next; } current->next = NULL; first(); value.first(); return *this; } In-Line Function: Prompts the compiler to generate code inline instead of laying it down once and calling it through the usual mechanisms

12 CS 340Chapter 3: Lists, Stacks, and Queues11 // Increment operator: moves current pointer to next node (if possible). template inline const list & list :: operator ++ () { if (current != NULL) current = current->next; return *this; } // Logical “not” operator: indicates whether current pointer is non-NULL. template inline boolean list :: operator ! () const { return (current != NULL); } // Parenthetical operator: returns value of current node (or head node if current is NULL). template inline const Etype& list :: operator () () const { if (current != NULL) return current->element; else return head->element; }

13 CS 340Chapter 3: Lists, Stacks, and Queues12 // Member function to determine whether parameterized element is in list. template boolean list :: find(const Etype &x) { node *p; for (p = head->next; p != NULL; p = p->next) { if (p->element == x) { current = p; return true; } return false; } // Member function to locate predecessor of parameterized value in list. template boolean list :: findPrevious(const Etype &x) { node *p; for (p = head; p->next != NULL; p = p->next) { if (p->next->element == x) { current = p; return true; } return false; }

14 CS 340Chapter 3: Lists, Stacks, and Queues13 // Member function to remove first occurrence of parameterized value from list. template boolean list :: remove(const Etype &x) { node *cellToDelete; if (findPrevious(x)) { cellToDelete = current->next; current->next = cellToDelete->next; delete cellToDelete; return true; } return false; } // Member function to insert parameterized value after current node in list. template void list :: insert(const Etype &x) { node *p = new node(x, current->next); if (p != NULL) { current->next = p; current = current->next; } // Member function to insert parameterized value as new head element in list. template void list :: insertAsFirstElement(const Etype &x) { header(); insert(x); } #define LIST_H #endif

15 CS 340Chapter 3: Lists, Stacks, and Queues14 #include "list.h" #include void printList(list &lst); // The main function generates a couple of integer lists // tp test the functionality of the linked list class. void main() { list lst1, lst2; cout << "(This should be empty)" << endl; printList(lst1); for (int i = 1; i <= 5; i++) lst1.insertAsFirstElement(i); cout << "(This should be 5 4 3 2 1)" << endl; printList(lst1); for (i = 4; i <= 6; i++) if (lst1.find(i)) cout << "Found " << lst1() << endl; else cout << i << " not found" << endl; lst2 = lst1; cout << "(This should be 5 4 3 2 1)" << endl; printList(lst2); lst2.remove(3); cout << "(This should be 5 4 2 1)" << endl; printList(lst2); cout << "(but this should still be 5 4 3 2 1)" << endl; printList(lst1); } A Test Driver For The Linked List Implementation

16 CS 340Chapter 3: Lists, Stacks, and Queues15 // The printList function outputs the contents // of the linked list, starting at the head. void printList(list &lst) { if (lst.isEmpty()) cout << "Empty list." << endl; else for (lst.first(); !lst; ++lst) cout << lst() << endl; }

17 CS 340Chapter 3: Lists, Stacks, and Queues16 Inheritance: The sortedList Class If Etype values can be sorted, then the list class can be modified to accommodate this sorting. This can be easily accomplished by deriving a subclass of list, and overriding the two insertion functions insert and insertAsFirstElement. If Etype values can be sorted, then the list class can be modified to accommodate this sorting. This can be easily accomplished by deriving a subclass of list, and overriding the two insertion functions insert and insertAsFirstElement. #include "list.h" template class sortedList: public list { public: virtual void insert(const Etype &x); virtual void insertAsFirstElement(const Etype &x) {} } // This member function inserts the parameterized // value and preserves the sorted nature of the list. template void sortedList :: insert(const Etype &x) { for (node *p = head; p->next != NULL; p = p->next) { if (p->next->element > x) break; } current = p; list ::insert(x); }

18 CS 340Chapter 3: Lists, Stacks, and Queues17 Example Linked List Application: Polynomials Let Etype be a two-field structure containing the coefficient and exponent of each monomial within the polynomial. Sort the nodes comprising each polynomial based upon the values of their exponents. Let Etype be a two-field structure containing the coefficient and exponent of each monomial within the polynomial. Sort the nodes comprising each polynomial based upon the values of their exponents. Polynomial p 1 (x) = 35x 6 - 7x 4 + 19x - 5 35 6 6 -7 4 4 19 1 1 -5 0 0 Polynomial p 2 (x) = -3x 8 + 20x 3 - 4x -3 8 8 20 3 3 -4 1 1 The operations for this ADT could include: Addition, subtraction, and multiplication of polynomials Addition, subtraction, and multiplication of polynomials Derivatives of polynomials Derivatives of polynomials Evaluation of polynomials (i.e., plugging in values) Evaluation of polynomials (i.e., plugging in values) The operations for this ADT could include: Addition, subtraction, and multiplication of polynomials Addition, subtraction, and multiplication of polynomials Derivatives of polynomials Derivatives of polynomials Evaluation of polynomials (i.e., plugging in values) Evaluation of polynomials (i.e., plugging in values)


Download ppt "CS 340Chapter 3: Lists, Stacks, and Queues1 Abstract Data Types An ADT is a set of operations upon a set of data. Implementation details are not specified."

Similar presentations


Ads by Google