Extensible Array C and Data Structures Baojian Hua

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists.
C and Data Structures Baojian Hua
CSEB324 Data Structures & Algorithms
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
1 Symbol Tables Chapter Sedgewick. 2 Symbol Tables Searching Searching is a fundamental element of many computational tasks looking up a name.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Data Structure & Abstract Data Type
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
C Module System C and Data Structures Baojian Hua
M180: Data Structures & Algorithms in Java
Extensible Array C and Data Structures Baojian Hua
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
1 Problem Solving Abstraction Oftentimes, different real-world problems can be modeled using the same underlying idea Examples: Runtime storage, Undo operation.
Map, Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Queue C and Data Structures Baojian Hua
Stack C and Data Structures Baojian Hua
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Dynamically Extensible Data Structures Discrete Mathematics and Its Applications Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.
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.
Relation Discrete Mathematics and Its Applications Baojian Hua
String C and Data Structures Baojian Hua
Graph C and Data Structures Baojian Hua
Stack C and Data Structures Baojian Hua
C and Data Structures Baojian Hua
Graph C and Data Structures Baojian Hua
Hash Discrete Mathematics and Its Applications Baojian Hua
Queue C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Linked List C and Data Structures Baojian Hua
Tree C and Data Structures Baojian Hua
Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Functional List C and Data Structures Baojian Hua
String C and Data Structures Baojian Hua
Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Graph Discrete Mathematics and Its Applications Baojian Hua
Hash C and Data Structure Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
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.
Induction & Recursion Discrete Mathematics and Its Applications Baojian Hua
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
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.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Hash C and Data Structure Baojian Hua
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Extensible Tree Discrete Mathematics and Its Applications Baojian Hua
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
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.
Click to edit Master text styles Stacks Data Structure.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 2/17/
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
Dynamic Allocation Review Structure and list processing
Abstract Data Types in C
Data Structures and Algorithms
Data Structures and Algorithms
Presentation transcript:

Extensible Array C and Data Structures Baojian Hua

Linear Data Structures A linear list (list) consists of: a collection of data elements (e1, e2, …, en) elements are ordered: e1  e2  …  en ei is called an predecessor of e_{i+1} e_{i+1} is called a successor of ei every element has at most one successor and predecessor

Linear Data Structures Typical operations on linear list : // create an empty list newList (); // the length of a list l length (l); // insert element x at position i in l, 0<=i<=n+1 insert (l, x, i); // return the i-th element nth (l, i); // delete the element at position i in l, 0<=i<=n delete (l, i); // apply function f to each element in l foreach (l, f);

Abstract Data Types in C // in “list.h” #ifndef LIST_H #define LIST_H typedef struct list *list; list new (); int length (list l); poly nth (list l, int n); // note the type “poly” void insert (list l, poly x, int i); poly delete (list l, int i); void foreach (list l, void (*f)(poly)); #endif

Implementation Using Array The straightforward method to implement this interface is to use an array and the array may not be full, so we must keep a “ tail ” tag to record its tail (the position of its last elements) 0 n-1

Implementation Using Array The straightforward method to implement this interface is to use an array and the array may not be full, so we must keep a “ tail ” tag to record its tail (the position of its last elements) 0 n-1 tail

Array-based Implementation // Combine these above observations, we have: // in file “arrayList.c” #include #include “list.h” #define initLength 32; #define extFactor 2; struct list { poly *array; int max; int tail; }; 0 n-1 array max tail l

Operation: “ new ” list new () { list l = malloc (sizeof (*l)); l->array = malloc (initLength * sizeof(poly)); l->length = initLength; l->tail = -1; return l; }

Operation: “ new ” list new () { list l = malloc (sizeof (*l)); l->array = malloc (initLength * sizeof(poly)); l->length = initLength; l->tail = -1; return l; } $#%& l

Operation: “ new ” list new () { list l = malloc (sizeof (*l)); l->array = malloc (initLength * sizeof(poly)); l->length = initLength; l->tail = -1; return l; } 0 n-1 array l

Operation: “ new ” list new () { list l = checkedMalloc (sizeof (*l)); l->array = checkedMalloc (initLength * sizeof(void *)); l->max = initLength; l->tail = -1; return l; } 0 n-1 array l

Operation: “ new ” list new () { list l = checkedMalloc (sizeof (*l)); l->array = checkedMalloc (initLength * sizeof(void *)); l->max = initLength; l->tail = -1; return l; } 0 n-1 array max tail l

Operation: “ length ” int length (list l) { // note that we omit such checks in the next // for clarity. You should always do such kind // of checks in your code. assert(l); return (l->tail)+1; } 0 n-1 array max tail l

Operation: “ nth ” poly nth (list l, int i) { if (i =length(l)) error (“invalid index”); poly temp; temp = *((l->array)+i); return temp; } 0 n-1 array max tail l

Operation: “ nth ” poly nth (list l, int i) { if (i =length(l)) error (“invalid index”); void *temp; temp = *((l->array)+i); return temp; } 0 n-1 array max tail l i temp

Operation: “ insert ” void insert (list l, poly x, int i) { if (i length(l)) error (“invalid index”); //move the data …; } 0 n-1 array max tail l i

Operation: “ insert ” void insert (list l, poly x, int i) { if (i length(l)) error (“invalid index”); for (int j=l->tail; j>=i; j--) (l->array)[j+1] = (l->array)[j]; …; } 0 n-1 array max tail l i j

Operation: “ insert ” void insert (list l, poly x, int i) { if (i length(l)) error (“invalid index”); for (int j=l->tail; j>=i; j--) (l->array)[j+1] = (l->array)[j]; …; } 0 n-1 array max tail l i j

Operation: “ insert ” void insert (list l, poly x, int i) { if (i length(l)) error (“invalid index”); for (int j=l->tail; j>=i; j--) (l->array)[j+1] = (l->array)[j]; …; } 0 n-1 array max tail l i j

Operation: “ insert ” void insert (list l, poly x, int i) { if (i length(l)) error (“invalid index”); for (int j=l->tail; j>=i; j--) (l->array)[j+1] = (l->array)[j]; …; } 0 n-1 array max tail l i j

Operation: “ insert ” void insert (list l, poly x, int i) { if (i length(l)) error (“invalid index”); for (int j=l->tail; j>=i; j--) (l->array)[j+1] = (l->array)[j]; …; } 0 n-1 array max tail l i j

Operation: “ insert ” void insert (list l, void *x, int i) { if (i length(l)+1) error (“invalid index”); for (int j=l->tail; j>=i; j--) (l->array)[j+1] = (l->array)[j]; (l->array)[i]=x; } x 0 n-1 array max tail l i j

Operation: “ insert ” void insert (list l, void *x, int i) { if (i length(l)+1) error (“invalid index”); for (int j=l->tail; j>=i; j--) (l->array)[j+1] = (l->array)[j]; (l->array)[i] = x; (l->tail)++; } x 0 n-1 array max tail l i j

Perfect? No, It ’ s Wrong! What if the initial input arguments look like this one? direct data movement will incur an out-of-bound error! 0 n-1 array max tail l i

Extensible Array void insert (list l, poly x, int i) { if (i length(l)) error (“invalid index”); // if l is full, extend l->array by a factor 2 if (l->tail+1==l->max) { l->array = realloc (l->array, extFacotr* (l->max)*sizeof(poly)); l->max *= extFactor; } // data movement as discussed above…; }

Extensible Array 0 n-1 array max tail l i 02n-2 i l->array = realloc (l->array, extFactor*(l->max)); n-1

Extensible Array 0 n-1 array max tail l i 02n-1 i l->array = realloc (l->array, extFactor*(l->max)); n-1

Extensible Array 0 n-1 array max tail l i 02n-1 i l->array = realloc (l->array, extFactor *(l->max)); l->max *= extFactor;

Extensible Array array max tail l 02n-1 i

Operation: “ delete ” The “ delete ” operation is reverse operation of the “ insert ” operation also involves data movement should we shrink the extensible array, when there are few elements in it (say ½ data item left)? See the programming assignment

Operation: “ foreach ” void foreach (list l, void (*f)(poly)) { for (int i=0; i tail; i++) f (*(l->array + i)); return; } 0 n-1 array max tail l

Summary Linear list ADT: a collection of ordered data element each item has no more than one successor or predecessor Extensible array-based implementation maintain internally a dynamically extensible array bad performance with insert or delete space waste