CS 340Chapter 3: Lists, Stacks, and Queues1 Abstract Data Types An ADT is a set of operations upon a...

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

    19-Dec-2015
  • Category

    Documents

  • view

    215
  • download

    2

Transcript of CS 340Chapter 3: Lists, Stacks, and Queues1 Abstract Data Types An ADT is a set of operations upon a...

Page 1: 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.

CS 340 Chapter 3: Lists, Stacks, and Queues

1

Abstract Data TypesAbstract Data TypesAbstract Data TypesAbstract Data Types

An ADT is a set of operations upon a set of An ADT is a set of operations upon a set of data.data.

Implementation details are Implementation details are notnot specified in specified in an ADT.an ADT.

The program designer determines the The program designer determines the operations that are needed and the specific operations that are needed and the specific data that will be used in the implementation.data that will be used in the implementation.

The implementation of the ADT should be The implementation of the ADT should be easy to modify, and such modifications easy to modify, and such modifications should be transparent to any code deploying should be transparent to any code deploying the ADT.the ADT.

An ADT is a set of operations upon a set of An ADT is a set of operations upon a set of data.data.

Implementation details are Implementation details are notnot specified in specified in an ADT.an ADT.

The program designer determines the The program designer determines the operations that are needed and the specific operations that are needed and the specific data that will be used in the implementation.data that will be used in the implementation.

The implementation of the ADT should be The implementation of the ADT should be easy to modify, and such modifications easy to modify, and such modifications should be transparent to any code deploying should be transparent to any code deploying the ADT.the ADT.

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

CS 340 Chapter 3: Lists, Stacks, and Queues

2

ADT #1: The ListADT #1: The ListADT #1: The ListADT #1: The ListA list is a finite ordered collection of items of the same A list is a finite ordered collection of items of the same

type.type.

Common list operations include:Common list operations include: Emptying the entire listEmptying the entire list Determining whether the list is emptyDetermining whether the list is empty Determining the size of the listDetermining the size of the list Determining the location of a particular list elementDetermining the location of a particular list element Determining the value of an element at a particular Determining the value of an element at a particular

location in the listlocation in the list Inserting a new list element at a particular locationInserting a new list element at a particular location Removing a particular element from the listRemoving a particular element from the list Outputting the entire listOutputting the entire list

A list is a finite ordered collection of items of the same A list is a finite ordered collection of items of the same type.type.

Common list operations include:Common list operations include: Emptying the entire listEmptying the entire list Determining whether the list is emptyDetermining whether the list is empty Determining the size of the listDetermining the size of the list Determining the location of a particular list elementDetermining the location of a particular list element Determining the value of an element at a particular Determining the value of an element at a particular

location in the listlocation in the list Inserting a new list element at a particular locationInserting a new list element at a particular location Removing a particular element from the listRemoving a particular element from the list Outputting the entire listOutputting the entire list

Page 3: 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.

CS 340 Chapter 3: Lists, Stacks, and Queues

3

An Array Implementation’s PerformanceAn Array Implementation’s Performance An Array Implementation’s PerformanceAn Array Implementation’s Performance

List Implementation Option: An List Implementation Option: An ArrayArray

List Implementation Option: An List Implementation Option: An ArrayArray

aa11aa11aa22aa22aa33aa33::::

aan-2n-2aan-2n-2aan-1n-1aan-1n-1aannaann????

????

::::

????

nnnn

Problems with the array implementation:Problems with the array implementation:– Data movement really slows down Data movement really slows down

insertions to and removals from the listinsertions to and removals from the list– The maximum list size must be specifiedThe maximum list size must be specified

Problems with the array implementation:Problems with the array implementation:– Data movement really slows down Data movement really slows down

insertions to and removals from the listinsertions to and removals from the list– The maximum list size must be specifiedThe maximum list size must be specified

Emptying the listEmptying the listEmptying the listEmptying the listO(1O(1))O(1O(1))

Determining if the list is emptyDetermining if the list is emptyDetermining if the list is emptyDetermining if the list is emptyO(1O(1))O(1O(1))

Determining the size of the listDetermining the size of the listDetermining the size of the listDetermining the size of the listO(1O(1))O(1O(1))Determining the location of a particular Determining the location of a particular

elementelementDetermining the location of a particular Determining the location of a particular elementelement

O(nO(n))O(nO(n))

Determining the value of an element in a particular Determining the value of an element in a particular locationlocationDetermining the value of an element in a particular Determining the value of an element in a particular locationlocation

O(1O(1))O(1O(1))Inserting a new element into a particular Inserting a new element into a particular

locationlocationInserting a new element into a particular Inserting a new element into a particular locationlocation

O(nO(n))O(nO(n))

Removing a particular elementRemoving a particular elementRemoving a particular elementRemoving a particular elementO(nO(n))O(nO(n))

Outputting the listOutputting the listOutputting the listOutputting the listO(nO(n))O(nO(n))

Page 4: 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.

CS 340 Chapter 3: Lists, Stacks, and Queues

4

A Linked List Implementation’s PerformanceA Linked List Implementation’s Performance A Linked List Implementation’s PerformanceA Linked List Implementation’s Performance

List Implementation Option: A List Implementation Option: A Linked ListLinked List

List Implementation Option: A List Implementation Option: A Linked ListLinked List

Problems with the linked list implementation:Problems with the linked list implementation:– Pointers consume memory not needed with Pointers consume memory not needed with

arraysarrays– Lack of indexing necessitates repeated list Lack of indexing necessitates repeated list

traversals (e.g., binary search is impossible)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 Pointers consume memory not needed with

arraysarrays– Lack of indexing necessitates repeated list Lack of indexing necessitates repeated list

traversals (e.g., binary search is impossible)traversals (e.g., binary search is impossible)

aa11aa11

aa22aa22

aa33aa33

::::

aan-2n-2aan-2n-2

aan-1n-1aan-1n-1

aannaann

Emptying the listEmptying the listEmptying the listEmptying the listO(nO(n))O(nO(n))

Determining if the list is emptyDetermining if the list is emptyDetermining if the list is emptyDetermining if the list is emptyO(1O(1))O(1O(1))

Determining the size of the listDetermining the size of the listDetermining the size of the listDetermining the size of the listO(nO(n))O(nO(n))Determining the location of a particular Determining the location of a particular

elementelementDetermining the location of a particular Determining the location of a particular elementelement

O(nO(n))O(nO(n))

Determining the value of an element in a particular Determining the value of an element in a particular locationlocationDetermining the value of an element in a particular Determining the value of an element in a particular locationlocation

O(nO(n))O(nO(n))Inserting a new element into a particular Inserting a new element into a particular

locationlocationInserting a new element into a particular Inserting a new element into a particular locationlocation

O(1O(1))O(1O(1))

Removing a particular elementRemoving a particular elementRemoving a particular elementRemoving a particular elementO(1O(1))O(1O(1))

Outputting the listOutputting the listOutputting the listOutputting the listO(nO(n))O(nO(n))

Page 5: 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.

CS 340 Chapter 2: Algorithm Analysis 5

Performance - Compression and Caching Performance - Compression and Caching EffectsEffects

CompressionCompression– Compress bitmaps for optimal space (core Compress bitmaps for optimal space (core

memory and disk storage)memory and disk storage)– Perform bitmap operations on Perform bitmap operations on compressedcompressed

bitmaps.bitmaps.

CachingCaching– Locality of Reference principleLocality of Reference principle– Keep record of location of recent bits Keep record of location of recent bits

referencedreferenced– Cache area is relatively smallCache area is relatively small– Each bitmap has its own cache areaEach bitmap has its own cache area

Page 6: 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.

CS 340 Chapter 2: Algorithm Analysis 6

Effect of cache on loading bitmapsEffect of cache on loading bitmaps

0

2

4

6

8

10

12

14

0 20 40 60 80 100

Bitmap

CP

U s

ec

s

20mb-w ith cache

20mb-no cache

Page 7: 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.

CS 340 Chapter 2: Algorithm Analysis 7

Effect of cache on OR bitmap operationsEffect of cache on OR bitmap operations

Performance for OR operation on 100 bitmaps

0

500

1000

1500

2000

2500

0 5 10 15 20 25

Cache size

CPU Secs

22k

1mb

20m

Page 8: 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.

CS 340 Chapter 2: Algorithm Analysis 8

Effect of cache on AND bitmap Effect of cache on AND bitmap operationsoperations

Performance for AND operation on 100 bitmaps

0

2

4

6

8

10

12

0 5 10 15 20 25Cache size

CPU Secs

22k

1mb

20m

Page 9: 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.

CS 340 Chapter 3: Lists, Stacks, and Queues

9

#ifndef LIST_H#include <stdlib.h>template <class Etype> 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 Linked List Implementation in Visual C++Visual C++

Linked List Implementation in Linked List Implementation in Visual C++Visual C++

Class Template

Structure

Structure Constructor

Class Constructor

Class Destructor

Virtual Function: Derived

classes may have their

own version of this

function

ConstantReturn: Value

returned is treated as a

constant

Constant Modifier: This operator accesses but

doesn’t modify

In-Line Code

Page 10: 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.

CS 340 Chapter 3: Lists, Stacks, and Queues

10

// Member function to free all memory associated with the linked list.template <class Etype> void list<Etype>:: 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 <class Etype> inline const list<Etype>& list<Etype>:: 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

Page 11: 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.

CS 340 Chapter 3: Lists, Stacks, and Queues

11

// Increment operator: moves current pointer to next node (if possible).template <class Etype> inline const list<Etype>& list<Etype>:: operator ++ (){ if (current != NULL) current = current->next; return *this;}

// Logical “not” operator: indicates whether current pointer is non-NULL.template <class Etype> inline boolean list<Etype>:: operator ! () const{ return (current != NULL);}

// Parenthetical operator: returns value of current node (or head node if current is NULL).template <class Etype> inline const Etype& list<Etype>:: operator () () const{ if (current != NULL) return current->element; else return head->element;}

Page 12: 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.

CS 340 Chapter 3: Lists, Stacks, and Queues

12

// Member function to determine whether parameterized element is in list.template <class Etype> boolean list<Etype>:: 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 <class Etype> boolean list<Etype>:: 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;

}

Page 13: 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.

CS 340 Chapter 3: Lists, Stacks, and Queues

13

// Member function to remove first occurrence of parameterized value from list.template <class Etype> boolean list<Etype>:: 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 <class Etype> void list<Etype>:: 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 <class Etype> void list<Etype>:: insertAsFirstElement(const Etype &x){

header();insert(x);

}

#define LIST_H#endif

Page 14: 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.

CS 340 Chapter 3: Lists, Stacks, and Queues

14

#include "list.h"#include <iostream.h>

void printList(list<int> &lst);

// The main function generates a couple of integer lists// tp test the functionality of the linked list class.void main(){ list<int> 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 A Test Driver For The Linked List ImplementationImplementation

A Test Driver For The Linked List A Test Driver For The Linked List ImplementationImplementation

Page 15: 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.

CS 340 Chapter 3: Lists, Stacks, and Queues

15

// The printList function outputs the contents// of the linked list, starting at the head.void printList(list<int> &lst){ if (lst.isEmpty()) cout << "Empty list." << endl; else for (lst.first(); !lst; ++lst) cout << lst() << endl;}

Page 16: 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.

CS 340 Chapter 3: Lists, Stacks, and Queues

16

Inheritance: TheInheritance: The sortedListsortedList ClassClass

Inheritance: TheInheritance: The sortedListsortedList ClassClassIfIf EtypeEtype values can be sorted, then thevalues can be sorted, then the listlist class can be modified to class can be modified to

accommodate this sorting.accommodate this sorting.

This can be easily accomplished by deriving a This can be easily accomplished by deriving a subclasssubclass of of listlist, , and and overridingoverriding the two insertion functions the two insertion functions insertinsert andand insertAsFirstElementinsertAsFirstElement..

IfIf EtypeEtype values can be sorted, then thevalues can be sorted, then the listlist class can be modified to class can be modified to accommodate this sorting.accommodate this sorting.

This can be easily accomplished by deriving a This can be easily accomplished by deriving a subclasssubclass of of listlist, , and and overridingoverriding the two insertion functions the two insertion functions insertinsert andand insertAsFirstElementinsertAsFirstElement..

#include "list.h"template <class Etype> class sortedList: public list<Etype>{ 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 <class Etype> void sortedList<Etype>:: insert(const Etype &x){ for (node *p = head; p->next != NULL; p = p->next) { if (p->next->element > x) break; } current = p; list<Etype>::insert(x);}

Page 17: 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.

CS 340 Chapter 3: Lists, Stacks, and Queues

17

Example Linked List Application: Example Linked List Application: PolynomialsPolynomials

Example Linked List Application: Example Linked List Application: PolynomialsPolynomials

Let Etype be a two-field structure containing the coefficient and Let Etype be a two-field structure containing the coefficient and exponent of each monomial within the polynomial.exponent of each monomial within the polynomial.

Sort the nodes comprising each polynomial based upon the values of Sort the nodes comprising each polynomial based upon the values of their exponents.their exponents.

Let Etype be a two-field structure containing the coefficient and Let Etype be a two-field structure containing the coefficient and exponent of each monomial within the polynomial.exponent of each monomial within the polynomial.

Sort the nodes comprising each polynomial based upon the values of Sort the nodes comprising each polynomial based upon the values of their exponents.their exponents.

Polynomial p1(x) = 35x6 - 7x4 + 19x - 5Polynomial p1(x) = 35x6 - 7x4 + 19x - 5

3535 66 -7-7 44 1919 11 -5-5 00

Polynomial p2(x) = -3x8 + 20x3 - 4xPolynomial p2(x) = -3x8 + 20x3 - 4x

-3-3 88 2020 33 -4-4 11

The operations for this ADT could include:The operations for this ADT could include: Addition, subtraction, and multiplication of polynomialsAddition, subtraction, and multiplication of polynomials Derivatives of polynomialsDerivatives 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:The operations for this ADT could include: Addition, subtraction, and multiplication of polynomialsAddition, subtraction, and multiplication of polynomials Derivatives of polynomialsDerivatives of polynomials Evaluation of polynomials (i.e., plugging in values)Evaluation of polynomials (i.e., plugging in values)