114 3/30/98 CSE 143 Collection ADTs [Chapter 4]. 115 3/30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Stacks, Queues, and Linked Lists
Linear Lists – Array Representation
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
DATA STRUCTURES USING C++ Chapter 5
Lists: An internal look
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.
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
The Stack Data Structure. Classic structure What is a Stack? An abstract data type in which accesses are made at only one end Last In First Out (LIFO)
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
1 Stacks Chapter 4. 2 Objectives You will be able to: Describe a stack as an ADT. Build a dynamic-array-based implementation of stacks. Build a linked-list.
Monday, 11/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Monday, 11/11/02  Questions? HW 04 due today at 5.  Today – Lists and an introduction to searching.
CS 240Chapter 7 - QueuesPage 29 Chapter 7 Queues The queue abstract data type is essentially a list using the FIFO (first-in-first-out) policy for adding.
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Queue Overview Queue ADT Basic operations of queue
Chapter 7: Queues QUEUE IMPLEMENTATIONS QUEUE APPLICATIONS CS
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
Data Structures Using C++ 2E
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
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.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
131 3/30/98 CSE 143 More Collection ADTs [Sections ]
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
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.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
CSCE 210 Data Structures and Algorithms
CS505 Data Structures and Algorithms
CSCI-255 LinkedList.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Lists The List ADT.
Chapter 16-2 Linked Structures
C++ Plus Data Structures
Queues.
CSC 143 Queues [Chapter 7].
Queues: Implemented using Arrays
Introduction to Programming
CSC 143 Stacks [Chapter 6].
Chapter 16 Linked Structures
General List.
Lists CMSC 202, Version 4/02.
Lists CMSC 202, Version 4/02.
Presentation transcript:

114 3/30/98 CSE 143 Collection ADTs [Chapter 4]

115 3/30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups of data  Some terms:  Order of data: Linear vs. Nonlinear (list vs. set)  Method of access: Direct vs. Sequential  Type of data: Homogenous vs. Heterogeneous  Size: Fixed vs. Variable

116 3/30/98 Classification of ADTs Collections Ordered CollectionsUnordered Collections Array List Queue Stack Record Set DirectSequential Bag HeterogeneousHomogeneous Table

117 3/30/98 Collections in C++  Direct support for records ( struct / class ) and arrays  Other ADTs must be implemented  No absolute standards  Many conventional operations and concepts  Standard (Template) Library (new)  contains many ADTs  Our Plan:  Look at each ADT  Learn the conventional operations  Consider possible implementations

118 3/30/98 Arrays as ADTs [Section 4.3]  Attributes of array type:  Linear sequence of homogeneous elements  Fixed length  Direct access  Operations on arrays  Direct indexing using [] operator  Built in arrays provide no bounds checking  No searching, sorting, insertion, etc.

119 3/30/98 Record ADT [Section 4.4]  Attributes of record type:  Nonlinear collection of heterogeneous elements (fields)  Fixed number of elements  Direct access  Operations on records  Accessing elements directly through. operator  We’ll typically use classes to implement records, providing encapsulation via access functions.  In C++, structs are classes where members are public (by default)

120 3/30/98 List ADT [Section 4.5]  Attributes of list type:  Linear sequence of homogeneous elements  Varying number of elements  Sequential access  Operations on lists  Sequential access through a “cursor”  Test if full, empty, cursor at end  Insert, delete elements

121 3/30/98 List Terminology  Head: First element in list  Tail: Last element in list  Cursor: “Current” element of list  Size: Number of elements in list aList: HeadTail Cursor elem 0 elem 1 elem 2 elem N-1... (Size=N)

122 3/30/98 Lists in C++  No predefined list type, so write our own  Use the class construct  Member functions for list operations  Private data for list representation  We’ll look at a list of integers, but later we’ll see how to write more general kinds of lists (or other ADTs).

123 3/30/98 List Interface // In IntList.h const int MAX_SIZE = 20; class IntList { public: IntList ();// Create an empty list int data ();// get data item // (the one pointed to by cursor) bool isEmpty ();// Is the list empty? bool isFull ();// can add more elements? int sizeOf(); void reset ();// move cursor to head void advance();// advance cursor bool endOfList(); // cursor past the end?

124 3/30/98 List Interface (2) // insert the item before the cursor. void insertBefore(int item); // insert the item after cursor. void insertAfter(int item); void deleteItem(); // delete the private: int items[MAX_SIZE]; int last; // position of last element int cursor; // position of current element };

125 3/30/98 List Implementation (1) #include “IntList.h” IntList::IntList() { cursor = -1; last = cursor; } bool IntList::isEmpty() { return (sizeOf() == 0); } bool IntList::isFull() { return (sizeOf() == MAX_SIZE); }

126 3/30/98 List Implementation (2) int IntList::sizeOf() { return last+1; } void IntList::reset() { cursor = 0; } void IntList::advance() { assert(!endOfList()); cursor++; } bool IntList::endOfList() { return (cursor > last); }

127 3/30/98 List Implementation (3) int IntList::data() { assert (!endOfList()); return items[cursor]; } void IntList::insertBefore(int item) { assert (!isFull()); if (last == -1) cursor = 0; else for (int i=last; i>=cursor; i--) items[i+1] = items[i]; items[cursor] = item; last++; }

128 3/30/98 List Implementation (4) void IntList::insertAfter(int item) { assert(!isFull() && !endOfList()); for (int i=last; i>= cursor+1; i--) items[i+1] = items[i]; items[cursor+1] = item; last++; cursor++; } void IntList::deleteItem() { for (int i=cursor; i < last; i++) items[i] = items[i+1]; last--; }

129 3/30/98 Client of List #include “IntList.h” void main() { IntList grades; int aGrade; while (cin >> aGrade) grades.insertAfter(aGrade); for (grades.reset(); !grades.endOfList(); grades.advance()) cout << grades.data() << endl; }

130 3/30/98 Summary  Collection ADTs  Distinguished by order, access, types of data  Arrays:  ordered, direct access by index, homogeneous  Records:  unordered,direct access by name, heterogeneous  Lists:  ordered, sequential access, homogeneous  Head, Tail, Cursor  size(), isEmpty(), isFull()  reset(), endOfList(), advance(), data()  insertBefore(), insertAfter(), deleteItem()