Linear Lists – Array Representation

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

COMP171 Fall 2005 Lists.
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Linear Lists – Linked List Representation
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
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.
CSE Lecture 12 – Linked Lists …
Review Pseudo Code Basic elements of Pseudo code
Array-Based Lists List Length of a list
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Skip List & Hashing CSE, POSTECH.
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.
Queues CSE, POSTECH 2 2 Queues Like a stack, special kind of linear list One end is called front Other end is called rear Additions (insertions or enqueue)
Data Structure Dr. Mohamed Khafagy.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Stacks.
Hash Tables1 Part E Hash Tables  
Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.
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.
Data Structures Using C++ 2E
Linear Lists – Linked List Representation CSE, POSTECH.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
L.Chen1 Chapter 3 DATA REPRESENTATION(2) L.Chen A Chain Iterator Class A Chain Iterator Class ( 遍历器类 )  An iterator permits.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,
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.
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.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
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.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
© 2004 Goodrich, Tamassia Hash Tables1  
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.
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.
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.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
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.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
114 3/30/98 CSE 143 Collection ADTs [Chapter 4] /30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
TAs Fardad Jalili: Aisharjya Sarkar: Soham Das:
The Class arrayList General purpose implementation of linear lists. Unknown number of lists.
CS212: Object Oriented Analysis and Design
CSCE 210 Data Structures and Algorithms
Abstract Data Types data object set or collection of instances
List Representation - Array
Linked List Yumei Huo Department of Computer Science
Chapter 16-2 Linked Structures
Data Structures ADT List
Data Structures ADT List
Chapter 16 Linked Structures
Data Structures ADT List
Queues Jyh-Shing Roger Jang (張智星)
Data Abstraction: The Walls
Lists CMSC 202, Version 4/02.
Presentation transcript:

Linear Lists – Array Representation CSE, POSTECH

Introduction Data Object a set of instances or values Examples: Boolean = {false, true} Digit = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} Letter = {A, B, C, …, Z, a, b, c, …, z} String = {a, b, …, aa, ab, ac,…} An individual instance is either primitive or composite. An element is the individual component of a composite instance.

Introduction Data Structure data object together with the relationships among instances and elements that comprise an instance Among instances of integer 369 < 370 280+4 = 284 Among elements that comprise an instance 369 3 is more significant than 6 3 is immediately to the left of 6 9 is immediately to the right of 6

Introduction Abstract Data Type (ADT) ADT is a collection of data and a set of operations that can be performed on the data. It enables us to think abstractly about the data … We can separate concepts from implementation. Typically, we choose a data structure and algorithms that provide an implementation of an ADT.

Linear List Definitions Examples Linear list is a data object whose instances are of the form (e1,e2,…,en) ei is an element of the list. e1 is the first element, and en is the last element. n is the length of the list. When n = 0, it is called an empty list. e1 comes before e2, e2 comes before e3, and so on. Examples student names order by their alphabets a list of exam scores sorted by descending order

ADT for Linear List AbstractDataType LinearList { instances ordered finite collections of zero or more elements operations Create(): create an empty linear list Destroy(): erase the list IsEmpty(): return true if empty, false otherwise Length(): return the list size Find(k,x): return the kth element of the list in x Search(x): return the position of x in the list Delete(k,x): delete the kth element and return it in x Insert(k,x): insert x just after the kth element Output(out): put the list into the output stream out }

Implementations of Linear List Array-based (Formula-based) Uses a mathematical formula to determine where (i.e., the memory address) to store each element of a list Linked list (Pointer-based) The elements of a list may be stored in any arbitrary set of locations Each element has an explicit pointer (or link) to the next element Indirect addressing Maintain a table such that the ith table entry tells us where the ith element is stored Simulated pointer Similar to linked representation but integers replace the C++ pointers

Array-based Representation of Linear List It uses an array to store the elements of linear list. Individual element is located in the array using a mathematical formula. typical formula location(i) = i – 1  ith element of the list is in position i-1 of the array See Figure 5.1 for different ways to store 5 2 4 8 1 element [0] [1] [2] [3] [4] MaxSize-1 length=5

Array-based Class ‘LinearList’ template <class T> class LinearList { public: LinearList(int MaxListSize = 10); ~LinearList() { delete [] element; } bool isEmpty() const { return length == 0; } int Length() const { return length; } bool Find(int k, T& x) const; int Search(const T& x) const; LinearList<T>& Delete(int k, T& x); LinearList<T>& Insert(int k, const T& x); void Output(ostream& out) const; private: int length; int MaxSize; T *element; };

Constructor ‘LinearList’ template<class T> LinearList<T>::LinearList(int MaxListSize) { // Constructor for array-based linear list MaxSize = MaxListSize; element = new T[MaxSize]; length = 0; } The time complexity is Q(1)

Operation ‘Find’ The time complexity is Q(1) template<class T> bool LinearList<T>::Find(int k, T& x) const { // Set x to the k’th element in the list if it exists if (k < 1 || k > length) return false; x = element[k-1]; return true; } The time complexity is Q(1)

Operation ‘Search’ The time complexity is O(length) template<class T> int LinearList<T>::Search(const T& x) const { // Locate x and return the position of x if found for (int i = 0; i < length; i++) if (element[i] == x) return ++i; return 0; } The time complexity is O(length)

Operation ‘Delete’ The time complexity is template<class T> LinearList<T>& LinearList<T>::Delete(int k, T& x) { // Delete the k’th element if it exists. if (Find(k, x)) { for (int i = k, i < length; i++) element[i-1] = element[i]; length--; return *this; } else throw OutOfBounds(); The time complexity is O((length-k) s) where s is the size of each element.

Operation ‘Insert’ The time complexity is template<class T> LinearList<T>& LinearList<T>::Insert(int k, const T& x) { // Insert x after the k’th element. if (k < 0 || k > length) throw OutOfBounds(); if (length == MaxSize) throw NoMem(); for (int i = length-1; i >= k; i--) element[i+1] = element[i]; element[k] = x; length++; return *this; } The time complexity is O((length-k) s) where s is the size of each element. See Figure 5.2 for deleting & inserting an element

Operation ‘Output’ The time complexity is Q(length) template<class T> void LinearList<T>::Output(ostream& out) const { // print out the list for (int i = 0; i < length; i++) out << element[i] << “ ”; } The time complexity is Q(length)

Changing the Length of 1D Array What does it mean to change the length of an array? In what situations do you need to do this? How can you change the length of one dimensional array? Read 5.3.2 and see Program 5.2 Read Chapter 5

Do you have a Role Model? Role model refers to a person who fills his or her role as a good example for others e.g., Bill Gates, 손정의, 안철수 Think about a role model for you by next class and submit a page describing Name of the person Why you chose that person