540 likes | 551 Views
This chapter introduces different ways of representing data, including formula-based, linked, indirect addressing, and simulated-pointer representations. It also covers concepts such as chains, circular lists, and doubly linked lists, along with applications like bin sort and radix sort.
E N D
Chapter 3 Data Representation Part 1
Goals • Introduce the different ways in which data may be represented • Concepts • Abstract data types • Formula-based, linked, indirect addressing, and simulated-pointer representations • Chains, circular lists, and doubly linked lists • Applications • Bin sort, radix sort • Equivalence class • Convex hull
Data objects A set of instances or values • Boolean = {false, true} • Digit = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} • Letter = {A, B, C, …, Z, a, b, … z} • Natural Number = {0, 1, 2, …} • Integer = {0, ±1, ±2, ±3, …} • String = {a, b, …, aa, ab, ac, …}
Primitive and composed data object • Primitive (or atomic) data objects • Composed data objects • Relationships among instances, e.g., order • Functions associated with data objects
Data structures • Data structure: a data object together with the relationships that exist among the instances, usually expressed by functions/operations • Data structure = data + functions • Standard data types - int, float, bool • User defined data types - using enumeration, grouping facility such as class, array, and pointers, e.g., char s[MaxSize];
Linear Lists • Data object in the form (e1, e2, …, en) where n is a finite natural number • elements - ei’s • length - n • empty list - when n = 0 • Order - e1precedes e2, e2precedes e3, and so on
Examples of linear lists • An alphabetized list of students in a class • a list of exam scores in nondecreasing order • an alphabetized list of members of Congress • a list of gold-medal winners in the Olympics men’s basketball event
Formula-based representation • Use an array to represent the instances of a linear list • Cell (or node) holds an instance of the data object • Locations of each element is given by a mathematical formula, e.g., location(i) = i-1
Evaluation • Merits • Fast: Search, Delete, and Insert functions have a worst complexity that is linear • Shortcomings • Inefficient use of space
Insertion (continue)Cost: a single insertion may require as many as MaxSize-1 moves
Linked representation • Each node keeps an explicit information (link or pointer) about the the location of other relevant nodes • Singly linked list, also called chain
Delete the fourth node • Locate the third and fourth nodes • Link the third node to the fifth • Free the fourth node
Extensions to the Class ChainErase: delete the nodes in the chain
last = 0; } Chain
, *last ; Chain (continue)
In case we need to visit elements of the whole chain Chain<int> L; …….. int len = L.Length(); int x, sum =0; for (int i = 1; i <= len; i++) { L.Find(i, x); sum = sum + x; } • The complexity of this code is Θ(n2) • It is necessary to define a function which traverse the chain in Θ(n) time
Location Initialize() next() Chain Iterator Class first Chain ChainIterator
Traverse the chain using Iterator Chain<int> L; ……. ….. int *p; ChainIterator<int> c; p = c.Initialize(L); // c is iterator of chain L while (p != NULL) { sum = sum + (*p); p = c.Next(); // advance p to point to next // node in the chain }
Circular List • singly linked circular list • add the head node at the front of the list