Functional List C and Data Structures Baojian Hua

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
C and Data Structures Baojian Hua
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Linked List Variations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Data Structure Lecture-5
Data Structure & Abstract Data Type
C Module System C and Data Structures Baojian Hua
Data Structure & Abstract Data Type C and Data Structures Baojian Hua
CHAPTER 7 HASHING What is hashing for? For searching But we already have binary search in O( ln n ) time after sorting. And we already have algorithms.
Extensible Array C and Data Structures Baojian Hua
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Linked Lists. Outline Why linked lists? Linked lists basics Implementation Basic primitives ­Searching ­Inserting ­Deleting.
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
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
Functions C and Data Structures Baojian Hua
C and Data Structures Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
C and Data Structures Baojian Hua
Relation Discrete Mathematics and Its Applications Baojian Hua
1 Review of Class on Nov 30:. 2 Chapter 12: Structures and ADTs  Outline  Declaring Structures  Accessing a Member in a structure variable  Initialization.
Breath First Searching & Depth First Searching C and Data Structures Baojian Hua
String 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
Extensible Array C and Data Structures Baojian Hua
Linked List C and Data Structures Baojian Hua
Tree C and Data Structures Baojian Hua
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Linked List and Namespace ~ include dynamic objects.
Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
String C and Data Structures Baojian Hua
String C and Data Structures Baojian Hua
Object-oriented Languages Compiler Baojian Hua
Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Polymorphism C and Data Structures Baojian Hua
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
§3 Separate Chaining ---- keep a list of all keys that hash to the same value struct ListNode; typedef struct ListNode *Position; struct HashTbl; typedef.
1 Chapter 5 Hashing General ideas Methods of implementing the hash table Comparison among these methods Applications of hashing Compare hash tables with.
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
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Exception Compiler Baojian Hua
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.
Hash C and Data Structure Baojian Hua
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
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
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
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.
Introduction to Linked Lists
Data Structures and Algorithms
Lists.
Linked Lists Chapter 4.
Chapter 16-2 Linked Structures
Discrete Mathematics and
Data Structures and Algorithms
Chapter 16 Linked Structures
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Linked lists Low-level (concrete) data structure, used to implement higher-level structures Used to implement sequences/lists (see CList in Tapestry) Basis.
Presentation transcript:

Functional List C and Data Structures Baojian Hua

Recap The extensible array-based implementation of linear list: may be too slow insert or delete operations involve data movement may waste too much space only a small portion of the allocated space is occupied with data General computer science idea: “ pay as you go ”

Polymorphic Abstract Data Types in C // recall the “List_t” ADT: #ifndef LIST_H #define LIST_H typedef void *poly; typedef struct List_t *List_t; List_t List_new (); int List_length (List_t l); poly List_nth (List_t l, int n); void List_insert (List_t l, poly x, int i); poly List_delete (List_t l, int i); void List_foreach (List_t l, void (*f)(poly)); #endif

Functional List A functional list is inductively defined: case #1: it is empty, or case #2: a head element and a tail of another smaller list (x1, (x2, (x3, … ))) head: x1; tail: (x2, (x3, … )) Next, we sometimes say “ list ” for simplicity

Mathematical Notation A functional list is inductively defined: case #1: it is empty, or case #2: a head element and a tail of another smaller list

Data Structure We use a pointer “ p ” to point to a list: case #1: empty, p==0 (or NULL) case #2: head::tail, x0x1x2x3 p …

Implementation // Turn the above figure into C, we have: // in file “list.c” #include #include “list.h” struct List_t { poly data; List_t next; }; data next data next data next data next list …

Create a list // “List_new” returns an empty list List_t List_new () { return 0; }

Add a new Head List_t List_concat (poly data, List_t l) { List_t temp = malloc (sizeof(*temp)); temp->data = data; temp->next = l; return temp; } data next data next data next data next temp … l

Operation: “ foreach ” void List_foreach (List_t l, void (*f)(poly)) { switch ((int)l) { case 0: return; default: f (l->data); List_foreach (l->next, f); // tail recursion }

Operation: “ length ” data next data next data next data next l …

Operation: “ length ” int List_length (List_t l) { switch((int)l) { case 0: return 0; default: return 1 + List_length (l->next); } data next data next data next data next l …

Operation: “ nth ” data next data next data next data next l …

Operation: “ nth ” poly List_nth (List_t l, int n) { switch ((int)l) { case 0: error (“empty list”); default: if (0==n) return l->data; else return List_nth (l->next, n-1); } data next data next data next data next l …

Operation: “ delete(l, n) ” data next data next data next data next l …

Operation: “ delete ” List_t List_delete (List_t l, int n) { if (0==l || n<0) error (“…”); if (0==n) return l->next; return List_concat (l->data, List_delete (l->next, n-1)); } …

Operation: “ insert(l, x, n) ” data next data next data next data next l …

Operation: “ insert ” List_t List_insert (List_t l, poly x, int n) { if (0==n) return List_concat(x, l); if (0==l) error (“…”); return List_concat(l->data, List_insert(l->next, x, n-1); } data next data next data next data next l …

Summary Functional programming: long history, even older than C stem from mathematics John Macathy ’ s LISP (60 ’ s) and later Scheme, ML, Haskell, JavaScript, etc. for long time considered impractical efficiency issue renew industry ’ s interest in recent decade e.g., F# from Microsoft good for parallel, multi-core, etc.

Summary Pros: good for programmer data structures are persistent elegant, easy to write, easy to maintain, easy to debug Cons: (maybe) bad for machine code maybe too slow both MS vc and GCC are bad in dealing this demand better compiler supports