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.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Stacks, Queues, and Linked Lists
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
DATA STRUCTURES USING C++ Chapter 5
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
CS 240Chapter 6 - StacksPage 21 Chapter 6 Stacks The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Data Structures: A Pseudocode Approach with C
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
C++ Classes and Data Structures Jeffrey S. Childs
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
Pointers OVERVIEW.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
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.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
Chapter 3: Lists, Stacks, and Queues Abstract Data Types Lists and Sorted Lists CS 340 Page 34 Stacks and Stack Applications Queues and Queue Applications.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
LINKED LISTS.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
CSCE 210 Data Structures and Algorithms
CSE 1342 Programming Concepts
C++ Programming:. Program Design Including
Chapter 4 The easy stuff.
Lists CS 3358.
Homework 4 questions???.
Chapter 4 Linked Lists.
Chapter 4 Linked Lists
Doubly linked lists.
Linked Lists.
Chapter 4 Link Based Implementations
Queues.
Chapter 4 Linked Lists.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 4 Linked Lists.
Pointers & Dynamic Data Structures
Lists.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lists CMSC 202, Version 4/02.
Lab 03 – Linked List.
Presentation transcript:

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.

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

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)

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)

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

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

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

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

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

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

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; }

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; }

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

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 )" << 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 )" << endl; printList(lst2); lst2.remove(3); cout << "(This should be )" << endl; printList(lst2); cout << "(but this should still be )" << endl; printList(lst1); } A Test Driver For The Linked List Implementation

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; }

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); }

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 x Polynomial p 2 (x) = -3x x 3 - 4x 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)