C and Data Structures Baojian Hua

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists.
JAVA & Linked List Implementation
C and Data Structures Baojian Hua
Linked List Variations
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
Data Structure & Abstract Data Type C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Map, Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Abstract Data Type C and Data Structures Baojian Hua
Queue C and Data Structures Baojian Hua
Stack C and Data Structures Baojian Hua
Dynamically Extensible Data Structures Discrete Mathematics and Its Applications Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
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
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
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
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
Polymorphism C and Data Structures Baojian Hua
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
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.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
(1 - 1) Introduction to C Data Structures & Abstract Data Types Instructor - Andrew S. O’Fallon CptS 122 (August 26, 2015) Washington State University.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
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
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
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.
CSE 1342 Programming Concepts
Linked List :: Basic Concepts
Introduction to Linked Lists
Lectures linked lists Chapter 6 of textbook
UNIT-3 LINKED LIST.
Data Structures and Algorithms
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
Introduction to Linked Lists
Linked Lists Chapter 4.
Discrete Mathematics and
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Data Structures and Algorithms
Presentation transcript:

C and Data Structures Baojian Hua bjhua@ustc.edu.cn Linked List C and Data Structures Baojian Hua bjhua@ustc.edu.cn

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 poly 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

Implementation Using Linked List Linked list is a self-reference structure: to simplify operations, we add a unique head node “head” “head” does not belong to the list may hold meta information of the list head …

Linked List-based Implementation // Turn the above figure into C, we have: // in file “linkedList.c” #include <stdlib.h> #include “list.h” struct List_t { poly data; list next; }; data next head …

Operation: “newList” // “new” returns an empty list, which consists of // a single head node. List_t List_new () { List_t l = malloc (sizeof (*l)); l->data = 0; // Why this? l->next = 0; return l; } /\ l

Operation: “length” int List_length (List_t l) { List_t p = l->next; int n = 0; while (p) { p = p->next; n++; } return n; n==0 data next l … p

Operation: “length” int List_length (List_t l) { List_t p = l->next; int n = 0; while (p) { p = p->next; n++; } return n; n==1 data next l … p

Operation: “length” int List_length (List_t l) { List_t p = l->next; int n = 0; while (p) { p = p->next; n++; } return n; n==2 data next l … p

Operation: “length” int List_length (List_t l) { List_t p = l->next; int n = 0; while (p) { p = p->next; n++; } return n; n==3 data next l … p

Operation: “nth” poly List_nth (List_t l, int n) { List_t p = l->next; int i = 0; if (n<0 || n>=List_length(l)) error (“invalid index”); while (i!=n) { p = p->next; i++; } return p;

Operation: “nth” n==2 i==0 l … p data next n==2 i==1 l … data p next

Operation: “insert” void List_insert (List_t l, poly x, int n) { // 1. change the “next” field of pointer t; // 2. change the “next” field of element (n-1) …; } n==2 we’d search pointer p l … data next data next data next x next t

Operation: “insert” void List_insert (List_t l, poly x, int n) { List_t p; if (n<0 || n>List_length(l)) error (“invalid index”); // search pointer p points to position n-1 p = n? (List_nth (l, n-1)) : l;

Operation: “insert” // continued… // Step #1: cook list node: List_t temp = malloc (sizeof (*temp)); temp->data = x; // Step #2: temp points to n-th data item temp->next = p->next; // Step #3: link temp onto list p->next = temp; return; }

Operation: “delete” poly List_delete (List_t l, int n) { // The key step is to search pointer p // Leave this as exercise. // See Lab #3. …; } n==2 we’d search pointer p l … data next data next data next

Operation: “foreach” void List_foreach (List_t l, void (*f)(poly)) { List_t p = l->next; while (p) { f (p->data); p = p->next; } l … data next data next data next

Linked List Summary Linked list: Several other variants: better space usage---no waste good time complexity insert or delete take linear time but have to search the data sequential, :-( Several other variants: circular linked list doubly linked list doubly circular linked list

Circular Linked List All the pointers forms a circle Note that the first node has two fields head: points to the head of the list tail: points to the tail of the list l head tail data next data next data next

Circular Linked List---Implementation // in file “clist.c” struct Clist_t { struct node *head; struct node *tail; }; struct node poly data; struct node *next; } head tail data next l

Linear List Application #1: Polynomials where ciR and n Nat uniquely determined by a linear list: For this representation, all the list operations apply

Linear List Application: Polynomials Space waste: Consider this: 20001 items with 3 non-zeros A refined representation: ci<>0 for 0<=i<=m Ex:

Polynomial ADT: Interface Abstract data type: Polyn_t represent the polynomial data type operations: Polyn_t Polyn_new (); // an empty polyn Polyn_t Polyn_add (Polyn_t p1, Polyn_t p2); real Polyn_value (Polyn_t p, real x0); // p(x0) Polyn_t Polyn_mult (Polyn_t p1, Polyn_t p2); // add an item c*x^n, which does not appear in p void Polyn_insert (Polyn_t p, real c, int n);

Polynomial ADT in C: Interface // in file “polyn.h” #ifndef POLYN_H #define POLYN_H typedef struct Polyn_t * Polyn_t; Polyn_t Polyn_new (); Polyn_t Polyn_add (Polyn_t p1, Polyn_t p2); Polyn_t Polyn_value (Polyn_t p, real x0); Polyn_t Polyn_mult (Polyn_t p1, Polyn_t p2); void Polyn_insert (Polyn_t p, real c, int n); #endif

Polynomial ADT in C: Implementation // in file “polyn.c” #include “linkedList.h” #include “polyn.h” struct Polyn_t { List_t coefExps; }; // where “coefExps” is a list of tuples: (c, n) // one way to read “list coefExps” is: // list<tuple<double, nat>> coefExps // However, C does not support this style of // declaration… :-(

Operation: “newPolyn” Polyn_t Polyn_new () { Polyn_t p = malloc (sizeof (*p)); // use a linked list internally p->coefExps = List_new (); return p; }

Operation: “insert” void Polyn_insert (Polyn_t p, real c, nat n) { // could we use “double” and “int”, instead of // “real” and “nat”? Tuple_t t = Tuple_new (c, n); List_insertAtTail (p->coefExps, t); return; } // Leave other functions as exercises.

Change to the Head #include <stdlib.h> #include “linkedList.h” #include “tuple.h” #include “polyn.h” struct Polyn_t { List_t coefExps; };

Linear List Application#2: Dictionary Dictionay: where ki are keys and vi are value all ki are comparable and distinct How can dict’ be represented in computers? many ideas (we’d discuss some in future) for now, we make use of a linear list

Dictionary ADT: Interface Abstract data type: Dict_t represent the dictionary data type operations: Dict_t Dict_new (); void Dict_insert (Dict_t d, poly key, poly value); poly Dict_lookup (Dict_t d, poly key); poly Dict_delete (Dict_t d, poly key);

“dict” ADT in C: Interface // in file “dict.h” #ifndef DICT_H #define DICT_H typedef struct Dict_t *Dict_t; Dict_t Dict_new (); void Dict_insert (Dict_t d, poly key, poly value); poly Dict_lookup (Dict_t d, poly key); poly Dict_delete (Dict_t d, poly key); #endif

“dict” ADT in C: Implementation // in file “dict.c” #include “linkedList.h” #include “dict.h” struct Dict_t { List_t l; };

Operations: “new” Dict_t Dict_new () { Dict_t d = malloc (sizeof (*d)); d->l = List_new (); return d; }

Operations: “insert” void Dict_insert (Dict_t d, poly key, poly value) { Tuple_t t = Tuple_new (key, value); List_insertAtHead (d->l, t); return; } // Leave other functions as programming // exercises.