Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.

Slides:



Advertisements
Similar presentations
Linear Lists – Array Representation
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Lists: An internal look
CSE Lecture 12 – Linked Lists …
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
List Representation - Chain Fall 2010 CSE, POSTECH.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Abstract Data Types data object set or collection of instances integer = {0, +1, -1, +2, -2, +3, -3, …} daysOfWeek = {S,M,T,W,Th,F,Sa}
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
CMPT 225 ADT List using Dynamic arrays A dynamic data structure is one that changes size, as needed, as items are inserted or removed The Java ArrayList.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
Data Structures data object set or collection of instances integer = {0, +1, -1, +2, -2, +3, -3, …} daysOfWeek = {S,M,T,W,Th,F,Sa}
Stacks CSE, POSTECH. 2 2 Stacks Linear list One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Linear Lists – Linked List Representation CSE, POSTECH.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
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.
Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
Lecture DS & Algorithms:09 Abstract Data Types. Lecture DS & Algorithms:09 2 Abstract Data Types Data Type: A data type is a collection of values and.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
ArrayList, Multidimensional Arrays
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
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.
Pointers OVERVIEW.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
CS1101: Programming Methodology Recitation 6 – Arrays and Collections.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
SNU IDB Lab. Ch5. Linear Lists – Array Representation © copyright 2006 SNU IDB Lab.
Stacks Chapter 8. Objectives In this chapter, you will: Learn about stacks Examine various stack operations Learn how to implement a stack as an array.
C arrays are limited: -they are represented by pointers (which may or may not be valid); -Indexes not checked (which means you can overrun your array);
Linear List Array representation Data structures A data structure is a particular way of storing and manipulating data in a computer so that it can be.
ITERATORS. Iterator An iterator in C++ is a concept that refines the iterator design pattern into a specific set of behaviors that work well with the.
The Class Chain. next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
TAs Fardad Jalili: Aisharjya Sarkar: Soham Das:
Linear (or Ordered) Lists instances are of the form (e 0, e 1, e 2, …, e n-1 ) where e i denotes a list element n >= 0 is finite list size is n.
The Class arrayList General purpose implementation of linear lists. Unknown number of lists.
An Array-Based Implementation of the ADT List
Reminder: Course Policy
CS 106X Arrays; Implementing a Collection (ArrayList)
-TAs and office hours announced
The Class ArrayLinearList
Abstract Data Types data object set or collection of instances
List Representation - Array
LinkedList Class.
Data Representation Methods
The Class chain.
Stacks template<class T> class stack { public:
Data Representation Methods
The Class arrayList General purpose implementation of linear lists.
Data Representation Methods
Programming in Java Lecture 11: ArrayList
-TAs and office hours announced
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Stacks template<class T> class stack { public:
Stacks template<class T> class stack { public:
The Vector Class An object of class Vector is similar to an array in that it stores multiple values However, a vector only stores objects does not have.
Stacks public interface Stack { public boolean empty();
The Class chain.
The Class chain.
Stacks template<class T> class stack { public:
Data Representation Methods
Data Representation Methods
Chapter 3 Lists, Stacks, and Queues
Presentation transcript:

Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class

Linear List Abstract Data Type AbstractDataType LinearList { instances ordered finite collections of zero or more elements operations empty(): return true iff the list is empty, false otherwise size(): return the list size (i.e., number of elements in the list) get(index): return the indexth element of the list indexO f(x): return the index of the first occurrence of x in the list, return -1 if x is not in the list erase(index): remove the indexth element, elements with higher index have their index reduced by 1 insert(theIndex, x): insert x as the indexth element, elements with theIndex >= index have their index increased by 1 output(): output the list elements from left to right }

Linear List As C++ Abstract Class template class linearList { public: virtual ~linearList() {}; virtual bool empty() const = 0; virtual int size() const = 0; virtual T& get(int theIndex) const = 0; virtual int indexOf(const T& theElement)const = 0; virtual void erase(int theIndex) = 0; virtual void insert(int theIndex, const T& theElement) = 0; virtual void output(ostream& out) const = 0; };

Extending A C++ Class template class arrayList : public linearList { // code for all abstract methods of linearList must come here }

Data Type Of Array element[] Data type of list elements is unknown. Define element[] to be of template type T.

Length of Array element[] Don’t know how many elements will be in list. Must pick an initial length and dynamically increase as needed. Use variable arrayLength to store current length of array element[].

Increasing Array Length Length of array element[] is 6. abcdef T* newArray = new T[15]; First create a new and larger array

Increasing Array Length Now copy elements from old array to new one. abcdefabcdef copy(element, element + 6, newArray);

Increasing Array Length Finally, delete old array and rename new array. delete [] element; element = newArray; arrayLength = 15; abcdef element[0]

template void changeLength1D(T*& a, int oldLength, int newLength) { if (newLength < 0) throw illegalParameterValue(); T* temp = new T[newLength]; // new array int number = min(oldLength, newLength); // number to copy copy(a, a + number, temp); delete [] a; // deallocate old memory a = temp; }

How Big Should The New Array Be? At least 1 more than current array length. Cost of increasing array length when array is full is  (old length). Cost of n insert operations done on an initially empty linear list increases by  (n 2 ).

Space Complexity newArray = new char[7]; space needed = 2 * newLength – 1 = 2 * maxListSize – 1 element[6] abcdef

Array Doubling Double the array length. abcdef newArray = new char[12]; abcdef Time for n inserts goes up by Q (n). Space needed = 1.5*newLength. Space needed <= 3*maxListSize – 3

How Big Should The New Array Be? Resizing by any constant factor new length = c * old length increases the cost of n inserts by  (n). Resizing by an additive constant increases the cost of n add operations by  (n 2 ).

How Big Should The New Array Be? Resizing by an additive constant c requires at most (maxListSize – 1) + (maxListSize – 1 + c) = 2 * (maxListSize – 1) + c space. Resizing by any constant factor new length = c * old length requires at most (1+c) * (maxListSize -1) space.

What Does C++ Do? STL class vector … c = 1.5 arrayList of text … c = 2

The Class arrayList General purpose implementation of linear lists. Unknown number of lists.

Create An Empty List arrayLinear a(100), b; arrayList c(10), d; linearList *d = new arrayList (1000); linearList *f = new arrayList (20);

Using A Linear List cout << a.size() << endl; a.insert(0, 2); d->insert(0, 4); a.output(); cout << a << endl; a.erase(0); if (a.empty()) a.insert(0, 5);

Array Of Linear Lists linearList * x[4]; x[0] = new arrayList (20); x[1] = new chain (); x[2] = new chain (); x[3] = new arrayList (15); for (int i = 0; i < 4; i++) x[i].insert(0, i);

The Class arrayList // include statements come here using namespace std; template class arrayList : public linearList { public: // constructor, copy constructor and destructor arrayList(int initialCapacity = 10); arrayList(const arrayList &); ~arrayList() {delete [] element;}

The Class arrayList // ADT methods bool empty() const {return listSize == 0;} int size() const {return listSize;} T& get(int theIndex) const; int indexOf(const T& theElement) const; void erase(int theIndex); void insert(int theIndex, const T& theElement); void output(ostream& out) const; // additional method int capacity() const {return arrayLength;}

The Class arrayList protected: void checkIndex(int theIndex) const; // throw illegalIndex if theIndex invalid T* element; // 1D array to hold list elements int arrayLength; // capacity of the 1D array int listSize; // number of elements in list };

A Constructor template arrayList ::arrayList(int initialCapacity) {// Constructor. if (initialCapacity < 1) {ostringstream s; s << "Initial capacity = " 0"; throw illegalParameterValue(s.str()); } arrayLength = initialCapacity; element = new T[arrayLength]; listSize = 0; }

Copy Constructor template arrayList ::arrayList(const arrayList & theList) {// Copy constructor. arrayLength = theList.arrayLength; listSize = theList.listSize; element = new T[arrayLength]; copy(theList.element, theList.element + listSize, element); }

The Method checkIndex template void arrayList ::checkIndex(int theIndex) const {// Verify that theIndex is between 0 and // listSize - 1. if (theIndex = listSize) {ostringstream s; s << "index = " << theIndex << " size = " << listSize; throw illegalIndex(s.str()); }

The Method get template T& arrayList ::get(int theIndex) const {// Return element whose index is theIndex. // Throw illegalIndex exception if no such // element. checkIndex(theIndex); return element[theIndex]; }

The Method indexOf template int arrayList ::indexOf(const T& theElement) const {// Return index of first occurrence of theElement. // search for theElement int theIndex = (int) (find(element, element + listSize, theElement) - element); // check if theElement was found if (theIndex == listSize) return -1; // not found else return theIndex; }

The Method erase template void arrayList ::erase(int theIndex) {// Delete the element whose index is theIndex. checkIndex(theIndex); // valid index, shift elements with higher // index copy(element + theIndex + 1, element + listSize,element + theIndex); element[--listSize].~T(); // invoke destructor }

The Method insert template void arrayList ::insert(int theIndex, const T& theElement) {// Insert theElement. if (theIndex listSize) {// invalid index // code to throw an exception comes here } // valid index, make sure we have space if (listSize == arrayLength) {// no space, double capacity changeLength1D(element, arrayLength, 2 * arrayLength); arrayLength *= 2; }

The Method insert // shift elements right one position copy_backward(element + theIndex, element + listSize, element + listSize + 1); element[theIndex] = theElement; listSize++; }

The Method output template void arrayList ::output(ostream& out) const {// Put the list into the stream out. copy(element, element + listSize, ostream_iterator (out, " ")); }

Overloading << // overload << template ostream& operator<<(ostream& out, const arrayList & x) {x.output(out); return out;}