The Class arrayList General purpose implementation of linear lists.

Slides:



Advertisements
Similar presentations
Linear Lists – Array Representation
Advertisements

Lists: An internal look
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,
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
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.
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.
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)
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
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.
J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.
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.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
The Class Chain. next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
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??
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
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.
Templates. C++ 2 Outline Function templates  Function template definition  Function template overloading Class templates  Class template definition.
TAs Fardad Jalili: Aisharjya Sarkar: Soham Das:
The Class arrayList General purpose implementation of linear lists. Unknown number of lists.
CSCE 210 Data Structures and Algorithms
Pointers and Dynamic Arrays
Reminder: Course Policy
CS505 Data Structures and Algorithms
CS 106X Arrays; Implementing a Collection (ArrayList)
-TAs and office hours announced
The Class ArrayLinearList
Lists The List ADT.
List Representation - Array
The Class chain.
Top Ten Words that Almost Rhyme with “Peas”
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Stacks template<class T> class stack { public:
Data Representation Methods
Lecture 2: Implementing ArrayIntList reading:
Data Representation Methods
Programming in Java Lecture 11: ArrayList
-TAs and office hours announced
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Stacks template<class T> class stack { public:
Stacks template<class T> class stack { public:
Elements are always copied when they are put into a container
Stacks public interface Stack { public boolean empty();
Lists - I The List ADT.
Lists - I The List ADT.
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
The Class chain.
Linked Lists.
The Class chain.
Stacks template<class T> class stack { public:
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Data Representation Methods
Data Representation Methods
Lists CMSC 202, Version 4/02.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lists CMSC 202, Version 4/02.
Chapter 3 Lists, Stacks, and Queues
Data Structures & Programming
Presentation transcript:

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

Create An Empty List arrayList<int> a(100), b; arrayList<double> c(10), d; linearList<int> *d = new arrayList<int>(1000); linearList<char> *f = new arrayList<char>(20); For the 3rd and 4th examples, the class arrayList must extend the abstract class linearList.

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<int> * x[4]; x[0] = new arrayList<int>(20); x[1] = new chain<int>(); x[2] = new chain<int>(); x[3] = new arrayList<int>(15); for (int i = 0; i < 4; i++) x[i].insert(0, i); x[0].insert(…) and x[3].insert(…) invoke the insert method of arrayList while x[1].insert and x[2].insert result in the invocation of the insert method of chain.

The Class arrayList // include statements come here using namespace std; template<class T> class arrayList : public linearList<T> { public: // constructor, copy constructor and destructor arrayList(int initialCapacity = 10); arrayList(const arrayList<T>&); ~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<class T> arrayList<T>::arrayList(int initialCapacity) {// Constructor. if (initialCapacity < 1) {ostringstream s; s << "Initial capacity = " << initialCapacity << " Must be > 0"; throw illegalParameterValue(s.str()); } arrayLength = initialCapacity; element = new T[arrayLength]; listSize = 0; Note that code should verify input data and throw exceptions when this data is incorrect.

Copy Constructor template<class T> arrayList<T>::arrayList(const arrayList<T>& theList) {// Copy constructor. arrayLength = theList.arrayLength; listSize = theList.listSize; element = new T[arrayLength]; copy(theList.element, theList.element + listSize, element); } this(10) invokes the previous constructor.

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

The Method get template<class T> T& arrayList<T>::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<class T> int arrayList<T>::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<class T> void arrayList<T>::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<class T> void arrayList<T>::insert(int theIndex, const T& theElement) {// Insert theElement. if (theIndex < 0 || 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 copy_backward(element + theIndex, // shift elements right one position copy_backward(element + theIndex, element + listSize, element + listSize + 1);   element[theIndex] = theElement; listSize++; }

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

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