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.

Slides:



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

Linear Lists – Array Representation
DATA STRUCTURES USING C++ Chapter 5
Lists: An internal look
Linked Lists CENG 213 Data Structures.
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.
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}
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
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
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Lists. Container Classes Many applications in Computer Science require the storage of information for collections of entities e.g. a student registration.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
C++ Review (3) Structs, Classes, Data Abstraction.
Pointers OVERVIEW.
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.
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.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
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 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract 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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
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.
Reminder: Course Policy
-TAs and office hours announced
The Class ArrayLinearList
Lists CS 3358.
Abstract Data Types data object set or collection of instances
List Representation - Array
LinkedList Class.
The Class chain.
Chapter 16-2 Linked Structures
Stacks template<class T> class stack { public:
Data Representation Methods
The Class arrayList General purpose implementation of linear lists.
Introduction to Classes
Data Representation Methods
-TAs and office hours announced
Stacks template<class T> class stack { public:
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Stacks template<class T> class stack { public:
Introduction to Data Structure
The Class chain.
The Class chain.
Stacks template<class T> class stack { public:
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.
Presentation transcript:

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 used efficiently. Simple data types (storing data and manipulated by build-on operators) Structured data types (member variables and functions) – Structs – Classes

Linear Lists Each instance of the data structure linear list (or ordered list) is an collection of ordered elements. Instances of a linear list 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 number of elements in the list)

Linear Lists L = (e 0, e 1, e 2, e 3, …, e n-1 ) relationships e 0 is the zero’th (or front) element e n-1 is the last element

Linear List Examples/Instances StudentsinCSC307= (Jack, Jill, Abe, Henry, Mary, …, Judy) ExamsinCSC307= (exam1, exam2, exam3) DaysofWeek = (S, M, T, W, Th, F, Sa) Months = (Jan, Feb, Mar, Apr, …, Nov, Dec)

Linear List Operations—size() the number of elements in the list determine list size L = (a,b,c,d,e) L.size() is 5

Linear List Operations—get(theIndex) get element with given index L = (a,b,c,d,e) L.get(0) is a get(2) is c get(4) is e Check if the index is valid before get operation (theIndex >=0 and theIndex<=listSize) get(-1) is error get(9) is error

Linear List Operations— indexOf(theElement) determine the index of a given element L = (a,b,d,b,a) indexOf(d) is 2 indexOf(a) is 0 indexOf(z) is -1 //element is not in the list The index of a nonexistent element is defined to be –1. When theElement is in the list an index between 0 and size()-1 is the result. So –1 would be an invalid index and is used to represent the case when theElement is not in the list

Linear List Operations— erase(theIndex) Remove/delete element with given index L = (a,b,c,d,e,f,g) erase(2) removes c and L becomes (a,b,d,e,f,g) index of d,e,f, and g decrease by 1 Size of the list decrease by 1

Linear List Operations—erase Remove/delete element from the list6 L = (a,b,c,d,e,f,g) erase(indexof(c)) removes c and L becomes (a,b,d,e,f,g) L.size() becomes 5 index of d,e,f, and g decrease by 1 Size of the list decrease by 1

Linear List Operations— erase(theIndex) Remove/delete element with given index L = (a,b,c,d,e,f,g) Check if theIndex is valid before erase operation erase(-1) => error erase(20) => error

Linear List Operations— insert(theIndex, theElement) add an element so that the new element has a specified index L = (a,b,c,d,e,f,g) insert(0,h) => L = (h,a,b,c,d,e,f,g) index of a,b,c,d,e,f, and g increase by 1 Size of the list increase by 1

Linear List Operations— insert(theIndex, theElement) L = (a,b,c,d,e,f,g) insert(2,h) => L = (a,b,h,c,d,e,f,g) index of c,d,e,f, and g increase by 1 Check if theIndex is valid before insert insert(10,h) => error insert(-6,h) => error

Abstract Data Type (ADT) Abstraction: separating the design details from its use of called abstraction. E.g. how the car’s engine works is abstraction, how the car’s engine is designed is implementation Abstract Data Type (ADT): a data type that separates the logical properties from the implementation details. Classes are a convenient way to implement an ADT.

Data Structure Specification  Language independent  Abstract Data Type Abstract Data Type (ADT): a data type that separates the logical properties from the implementation details and is a model for a certain class of data structures that have similar behavior;  C++  Abstract Class is used for the class of data structures.

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; }; cannot be instantiated.

Linear list data store Array Linked list

Extending A C++ Class template class arrayList : public linearList { // code for all abstract methods of linearList must come here } If arrayList does not provide an implementation for all abstract methods of linearList, arrayList also is abstract and so cannot be instantiated.

use a one-dimensional array element[] element[15] = {a,b,c,d,e}; Linear List Array Representation abcde L = (a, b, c, d, e) Store element i of list in element[i]. 14

Data Type Of Array element[] Data type of list elements is unknown. To use this representation in C++, we must know/provide a data type and length of the array element[]. Use template type T to get a generic class that works for all data types. 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 vs size) What is different between size and length? Length/size of array element[] is 6. abcdef arrayLength = 15; T* newArray = new T[arrayLength]; 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]

Change Array Length template void changeLength1D(T*& a, int oldLength, int newLength) { if (newLength < 0) {cout << “wrong length!\n”; return -1;} T* temp = new T[newLength]; // new array int number = min(oldLength, newLength); // number to copy copy(a, a + number, temp); // function copy from algorithm.h delete [] a; // deallocate old memory a = temp; }

Class arrayList template class arrayList : public linearList { public: // constructor, copy constructor and destructor arrayList(int initialCapacity = 10); arrayList(const arrayList &); //copy constructor ~arrayList(); // ADT methods from class linearList 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;} 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 };

Constructor arrayList(int initialCapacity = 10); arrayLength = initialCapacity; element = new T[arrayLength]; listSize = 0;

Constructor template arrayList ::arrayList(int initialCapacity) {// Constructor. if (initialCapacity < 1) { cout << “wrong intinitial capacity” << endl; } arrayLength = initialCapacity; element = new T[arrayLength]; listSize = 0; }

constructor // test constructor arrayList *x = new arrayList (20); arrayList y(2), z;

Copy Constructor arrayList(const arrayList & theList); Now copy elements from old array to new one. abcdef arrayLength = theList.arrayLength; listSize = theList.listSize; element = new T[arrayLength]; copy(theList.element, theList.element + listSize, element); abcdef theList element

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

constructor // test constructor arrayList *x = new arrayList (20); arrayList y(2), z; //test copy constructor arrayList w(y);

The Method checkIndex template void arrayList ::checkIndex(int theIndex) const {// Verify that theIndex is between 0 and // listSize - 1. if (theIndex = listSize) {cout << “invalid index” << endl; return ;} }

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

get(theIndex) // test get cout << "Element with index 0 is " << y.get(0) << endl; cout << "Element with index 3 is " << y.get(3) << endl;

int indexOf(const T& theElement) const; indexOf(‘c’); find(element, element + listSize, ‘c’) //algorithm.h Find Return element+2; indexOf(‘c’) is (element+2) – element indexOf(‘h’); find(element, element + listSize, ‘h’) Return element+listSise; If indexOf(‘h’) is listSize // no element is in the list; abcdef element

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

// test indexOf int index = y.indexOf(‘a’); if (index < 0) cout << “a not found" << endl; else cout << "The index of a is " << index << endl; index = y.indexOf(‘z’); if (index < 0) cout << “z not found" << endl; else cout << "The index of z is " << index << endl;

Erase erase(int theIndex) erase(2) abdef copy(element + theIndex + 1, element + listSize, element + theIndex); element[--listSize].~T(); // invoke destructor abcdef element

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 }

// test erase y.erase(1); cout << "Element 1 erased" << endl; cout << "The list is " << y << endl; y.erase(2); cout << "Element 2 erased" << endl; cout << "The list is " << y << endl; cout << "Size of y = " << y.size() << endl; cout << "Capacity of y = " << y.capacity() << endl; if (y.empty()) cout << "y is empty" << endl; else cout << "y is not empty" << endl;

insert insert(int theIndex, const T& theElement) insert(2,’c’) abdef copy_backward(element + theIndex, element + listSize, element + listSize + 1); element[theIndex] = theElement; listSize++; abcdef element

The Method insert template void arrayList ::insert(int theIndex, const T& theElement) {// Insert theElement. checkIndex(theIndex) ; // valid index, make sure we have space if (listSize == arrayLength) {// no space, double capacity changeLength1D(element, arrayLength, 2 * arrayLength); arrayLength *= 2; } // shift elements right one position copy_backward(element + theIndex, element + listSize, element + listSize + 1); element[theIndex] = theElement; listSize++; }

// test insert y.insert(0, 2); y.insert(1, 6); y.insert(0, 1); y.insert(2, 4); y.insert(3, 5); y.insert(2, 3); cout << "Inserted 6 integers, list y should be " << endl; cout << "Size of y = " << y.size() << endl; cout << "Capacity of y = " << y.capacity() << endl; if (y.empty()) cout << "y is empty" << endl; else cout << "y is not empty" << endl; y.output(cout); cout << endl << "Testing overloaded <<" << endl; cout << y << endl;

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

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

// test output cout << "Y is printed by output :\n"; y.output(cout); cout << "Y is printed by << :\n"; cout << y;

Next class – Lab 1 assignment See lab1 presentations and material on course webpage. Submit your lab assignment 1 to class account on Next Tuesday (2/08) before 1pm.