Chapter 4 The easy stuff.

Slides:



Advertisements
Similar presentations
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
Advertisements

1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
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.
Chapter 6 Queues and Deques.
1 Linked List Position (1). 2 Linked List Position (2)
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
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.
Review of Lists, Stacks, and Queues CS 400/600 – Data Structures.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Abstract Data Types and a review of C++ programming concepts CS 400/600 – Data Structures.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
CSCE 210 Data Structures and Algorithms
CSE 1342 Programming Concepts
Unit – I Lists.
Cpt S 122 – Data Structures Abstract Data Types
CSCE 210 Data Structures and Algorithms
CS505 Data Structures and Algorithms
Understanding Algorithms and Data Structures
Data Structure By Amee Trivedi.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
September 29 – Stacks and queues
CSCI-255 LinkedList.
Chapter 15 Lists Objectives
Data Structures and Algorithms
Homework 4 questions???.
Stacks and Queues.
Cinda Heeren / Geoffrey Tien
Stacks Stack: restricted variant of list
Monday, February 26, 2018 Announcements… For Today…
Introduction to Data Structure
CST230: Applied Data Structures
structures and their relationships." - Linus Torvalds
Lists.
Chapter 15 Lists Objectives
Queues.
Basics of Algorithm Analysis
Lists List: finite sequence of data elements
Further Data Structures
CSC 143 Queues [Chapter 7].
CSCI 333 Data Structures Chapter 4 13 and 16 September 2002.
CMSC 341 Skip Lists.
Data Structures and Algorithms
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Introduction to Data Structure
File Organization.
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Dynamic allocation (continued)
Stacks, Queues, and Deques
CMSC 341 Skip Lists.
structures and their relationships." - Linus Torvalds
Abstract Data Types Stacks CSCI 240
5.3 Implementing a Stack Chapter 5 – The Stack.
Stacks and Linked Lists
Data Structures & Programming
Presentation transcript:

Chapter 4 The easy stuff

Lists, Stacks, and Queues If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need to search or some other more intensive operation on the data would a more complex structure be needed

Goals for this Chapter Present some basic data structures Give examples of separating a logical representation in the form of an ADT from a physical implementation in a Data Structure Illustrate the use of algorithm analysis in the context of simple operations

Lists A list is a finite, ordered sequence of data items know as elements Each element has a position This is a sequence as defined in Chapter 2 It’s empty when it contains no elements The number of current elements is its length The first element is the head The last element is the tail

Basic Operations Must be able to insert and delete anywhere along the list Should be able to gain access to an elements value Must be able to create and clear the list Convenient to gain access to the next element from the current one

List ADT In C++ we will use the notion of an abstract class for the List We will increase the flexibility by making it a template The abstract class does not specify how the operations are implemented Given that we want to support the concept of a sequence, the key decision embodied in the ADT is the notion of position

The Fence The list will be partitioned into two sets with a fence between them We will use a vertical bar to indicate where the fence is when we need notation to show the fence <4, 5, 6 | 8, 10, 11> After an insert <4, 5, 6 | 13, 8, 10, 11>

The List Abstract Class template <class Elem> class List{ public: virtual void clear() = 0; virtual bool insert(const Elem&) = 0; virtual bool append(const Elem&) = 0; virtual bool remove(Elem&) = 0; virtual void setStart() = 0; virtual void setEnd() = 0; virtual void prev() = 0; virtual void next() = 0; virtual int leftLength() = 0; virtual int rightLength() = 0; virtual bool setPos(int pos) = 0; virtual bool getValue(Elem&) const = 0; virtual void print() const = 0; };

Array Based Implementation Contains 4 private variables One for maxSize, listSize, fence, and the listArray Stores the elements in contiguous memory locations Shifting can occur with insert and deletes Inserting and deleting in the average case is Θ(n)

Linked Implementation Key design choice is how to implement the fence Two choices: The first node on the right side The last node on the left side A header node can eliminate some the special cases that arise from choosing the second option. It does introduce some overhead

Comparision Array list size is predetermined When list is small, large amount of space can be wasted Linked list waste space with overhead No limit to the number of elements in list

Breakeven Comparison n number of elements in list P size of pointer storage E size of data element D maximum number of elements stored in array Space required for Array DE Space required for Linked n(P+E) n> DE/(P+E) When P = E we get D/2 as break even point

Dictionary ADT The most common objective of a computer program is to store and retrieve data The dictionary is defined to provide the fundamental operations of storing records, finding records and removing records from a database. We must define concepts of a key and comparable objects

The Dictionary ADT template <class Key, class Elem, class KEComp, class EEComp> class Dictionary { public: virtual void clear() = 0; virtual bool insert(const Elem&) = 0; virtual bool remove(const Key&, Elem&) = 0; virtual bool removeAny(Elem&) = 0; virtual bool find(const Key&, Elem& ) = 0; virtual int size() = 0; };

What are all those classes for? We know we need to be able to find records in the dictionary. We could simply use the basic relational operators. This doesn’t work for complex types We could simply overload the basic relational operators This is hidden in the code We could require that the Elem class is inheiret off of a class that implements a comparable method What about multiple keys?

Best Solution Allowing the user to supply their own definitions for comparing keys to records and records to records By making the class a template parameter, we now have the requirement in the interface The design is known as a “Strategy” design pattern, since the strategy is provided explicitly by the client

Stacks The stack is a list-like structure in which elements may be inserted or removed from only one end. A stack follows a LIFO access pattern Elements are stored and removed in reverse order of their arrival Elements are pushed on the stack Elements are popped off the stack

Queues The queue is a list-like structure that provides restricted access to its elements Elements are enqueued at the back Elements are dequed from the front Queues follow a FIFO access pattern