Data Representation Methods

Slides:



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

Stacks, Queues, and Linked Lists
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
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.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
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.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
HASHING Section 12.7 (P ). HASHING - have already seen binary and linear search and discussed when they might be useful (based on complexity)
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
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.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
The Class arrayList General purpose implementation of linear lists. Unknown number of lists.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
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.
An Array-Based Implementation of the ADT List
Reminder: Course Policy
Elementary Data Structures
Review Array Array Elements Accessing array elements
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Chapter 4 The easy stuff.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Fundamentals of Programming II Working with Arrays
Stacks.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Queues Queues Queues.
Cinda Heeren / Geoffrey Tien
Data Representation Methods
Hash Tables.
The Class chain.
Chapter 16-2 Linked Structures
Stacks, Queues, and Deques
Chapter 19: Stacks and Queues.
Stacks template<class T> class stack { public:
Data Representation Methods
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
C++ Plus Data Structures
More Data Structures (Part 1)
The Class arrayList General purpose implementation of linear lists.
بردارها و ماتريس ها ساختمان داده ها
Building Java Programs
Stacks, Queues, and Deques
Building Java Programs
Linked List Intro CSCE 121 J. Michael Moore.
Arrays versus ArrayList
Stacks template<class T> class stack { public:
An Overview of Insertion Sort
Stacks template<class T> class stack { public:
CSE 143 Lecture 4 Implementing ArrayIntList
Stacks and Queues.
The Class chain.
CSE 143 Lecture 3 Implementing ArrayIntList reading:
The Class chain.
Stacks template<class T> class stack { public:
Array-Based Lists & Pointers
Queues Definition of a Queue Examples of Queues
General List.
Data Representation Methods
Data Representation Methods
Stacks, Queues, and Deques
CSE 143 Lecture 4 Implementing ArrayIntList reading:
Linked List Intro CSCE 121.
Sorting.
Dynamic Array: Implementation of Stack
Implementing ArrayIntList
Presentation transcript:

Data Representation Methods array --- Chapter 5 linked --- Chapter 6 Each representation method is illustrated using the Linear List data structure as an example.

Linear List Array Representation use a one-dimensional array element[] 1 2 3 4 5 6 a b c d e All array representations of a linear list use an array (say one-dimensional). Different array representations result when different mappings are used between list elements and array positions. The most intuitive and simple mapping is to map list element I into array position I. L = (a, b, c, d, e) Store element i of list in element[i].

Right To Left Mapping a b c d e A different mapping of linear list elements into a one-dimensional array.

Mapping That Skips Every Other Position b c d e Yet another mapping.

Wrap Around Mapping a b c d e And if that isn’t enough, here is yet another mapping.

Representation Used In Text 1 2 3 4 5 6 a b c d e listSize = 5 put element i of list in element[i] use a variable listSize to record current number of elements This is the representation used in the text. When this mapping is used, list operations such as isEmpty(), size(), get(index) Become easy to implement.

Insert/Erase An Element b c d e listSize = 5 insert(1,g) To insert an element we must physically relocate elements that are to be to the right of the newly inserted element. We must also increment listSize by 1. The slide shows insert(1,g).erase(1) is the reverse. To remove an element we must move elements to the right of the removed element left by 1 and reduce listSize by 1. a g b c d e listSize = 6

Data Type Of Array element[] Data type of list elements is unknown. Define element[] to be of template type T. 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.

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. a b c d e f First create a new and larger array T* newArray = new T[15];

Increasing Array Length Now copy elements from old array to new one. a b c d e f a b c d e f Copy() is an STL function. It copies element[0:5] to newArray[0:5]. copy(element, element + 6, newArray);

Increasing Array Length Finally, delete old array and rename new array. delete [] element; element = newArray; arrayLength = 15; a b c d e f element[0]

template<class T> 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 Q(old length). Cost of n insert operations done on an initially empty linear list increases by Q(n2). When array length is increased by 1 each time we need to resize the array element[], the cost of n add operations goes up by Theta(n2). To see this suppose we start with an empty list and element.length = 1. Each element, other than the first, that is added to the list requires an array resize to be done. When element i, i > 0 is added, the array resizing requires i steps. So the total array resizing cost is Theta(n2). If the add operations are all done at the right end, then the cost of all n adds (in the absence of array resizing) is Theta(n). The resizing time dominates the overall effort. When the additions are done at the front of the list the resizing time is of the same order as the add time.

Space Complexity element[6] newArray = new char[7]; b c d e f newArray = new char[7]; space needed = 2 * newLength – 1 = 2 * maxListSize – 1

Array Doubling Double the array length. newArray = new char[12]; f newArray = new char[12]; a b c d e f 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 Q(n). Here, by the cost of n inserts, we mean the cost of a sequence of n insert operations performed on an initially empty linear list. Resizing by an additive constant increases the cost of n add operations by Q(n2).

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

What Does C++ Do? STL class vector … c = 1.5 arrayList of text … c = 2 The STL provides a class vector that uses an array to represent a linear list. This uses max of 1 and 1.5 * currentLength. The vector class has much of the functionality of linear list.The text class arrayList does array doubling.