Download presentation
Presentation is loading. Please wait.
1
Linked List C and Data Structures Baojian Hua bjhua@ustc.edu.cn
2
Recap The extensible array-based implementation of linear list: may be too slow insert or delete operations involve data movement may be too space waste only a small portion of the allocated space is filled with data
3
Abstract Data Types in C // in “list.h” #ifndef LIST_H #define LIST_H typedef void *poly; typedef struct list *list; list new (); int length (list l); poly nth (list l, int n); void insert (list l, poly x, int i); poly delete (list l, int i); void foreach (list l, void (*f)(poly)); #endif
4
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 …
5
Linked List-based Implementation // Turn the above figure in C, we have: // in file “linkedList.c” #include #include “list.h” struct list { poly data; list next; }; data next data next data next head …
6
Operation: “ new ” // “new” returns an empty list, which consists of // a single head node. list new () { list l = malloc (sizeof (*l)); l->data = NULL; l->next = NULL; return l; } /\ l
7
Operation: “ length ” int length (list l) { list p = l->next; int n = 0; while (p) { p = p->next; n++; } return n; } data next data next data next l … p n==0
8
Operation: “ length ” int length (list l) { list p = l->next; int n = 0; while (p) { p = p->next; n++; } return n; } data next data next data next l … p n==1
9
Operation: “ length ” int length (list l) { list p = l->next; int n = 0; while (p) { p = p->next; n++; } return n; } data next data next data next l … p n==2
10
Operation: “ length ” int length (list l) { list p = l->next; int n = 0; while (p) { p = p->next; n++; } return n; } data next data next data next l … p n==3
11
Operation: “ nth ” poly nth (list l, int n) { list p = l->next; int i = 0; if (n =length(l)) error (“invalid index”); while (i!=n) { p = p->next; i++; } return p; }
12
Operation: “ nth ” data next data next data next l … n==2 p i==0 data next data next data next l … n==2 p i==1 data next data next data next l … n==2 p i==2
13
Operation: “ insert ” void insert (list l, poly x, int n) { // 1. change the “next” field of pointer x; // 2. change the “next” field of element (n-1) … } data next data next data next l … n==2 data next x we ’ d search this pointer p
14
Operation: “ insert ” void insert (list l, poly x, int n) { list p; if (n length(l)) error (“invalid index”); // search pointer p points to position n-1 if (n==0) p = l; else p = nth (l, n-1);
15
Operation: “ insert ” list temp = malloc (sizeof (*temp)); temp->data = x; // 1. change the “next” field of x temp->next = p->next; // 2. change the “next” field of p p->next = temp; return; }
16
Operation: “ delete ” poly delete (list l, int n) { // The key step is to search pointer p // Leave this as exercise. … } data next data next data next l … n==2 we ’ d search this pointer p
17
Operation: “ foreach ” void foreach (list l, void (*f)(poly)) { list p = l->next; while (p) { f (p->data); p = p->next; } data next data next data next l …
18
Linked List Summary Linked list: better space usage---no waste good time complexity insert or delete take linear time but have to also search the data sequential, :-( Can be further generalized circular linked list doubly linked list doubly circular linked list
19
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 head tail data next data next data next l
20
Circular Linked List--- Implementation // in file “clist.c” struct list { struct node *head; struct node *tail; }; struct node { poly data; struct node *next; } head tail data next data next data next l
21
Linear List Application#1: Polynomials Polynomials: where ci R and n Nat uniquely determined by a linear list: For this representation, all the list operations apply
22
Linear List Application: Polynomials Space waste: Consider this: 20001 items with 3 non-zeros A refined representation: ci<>0 for 0<=i<=m Ex:
23
Polynomial ADT: Interface Abstract data type: polyn represent the polynomial data type operations: polyn new (); // an empty polyn polyn add (polyn p1, polyn p2); real value (polyn p, real x0); // p(x0) polyn mult (polyn p1, polyn p2); // add an item c*x^n, which does not appear in p void insert (polyn p, real c, nat n);
24
Polynomial ADT in C: Interface // in file “polyn.h” #ifndef POLYN_H #define POLYN_H typedef struct polyn *polyn; polyn new (); polyn add (polyn p1, polyn p2); real value (polyn p, real x0); polyn mult (polyn p1, polyn p2); void insert (polyn p, real c, nat n); #endif
25
Polynomial ADT in C: Implementation // in file “polyn.c” #include “linkedList.h” #include “polyn.h” struct polyn { linkedList coefExps; }; // where “coefExps” is a list of tuples: (c, n) // one way to read “list coefExps” is: // list > coefExps // However, C does not support this style of // declaration… :-(
26
Operation: “ new ” polyn new () { polyn p = malloc (sizeof (*p)); // use a linked list internally p->coefExps = newLinkedList (); return p; }
27
Change to the Head #include #include “linkedList.h” #include “polyn.h” struct polyn { linkedList coefExps; };
28
Operation: “ insert ” void insert (polyn p, real c, nat n) { // could we use “double” and “int”, instead of // “real” and “nat”? tuple t = newTuple (c, n); linkedListInsertAtTail (p->coefExps, t); return; } // Leave other functions as assignments
29
Change to the Head #include #include “linkedList.h” #include “tuple.h” #include “polyn.h” struct polyn { linkedList coefExps; };
30
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
31
Dictionary ADT: Interface Abstract data type: dict represent the dictionary data type operations: dict new (); // an empty dict void insert (dict d, poly key, poly value); poly lookup (dict d, poly key); void delete (dict d, poly key);
32
“ dict ” ADT in C: Interface // in file “dict.h” #ifndef DICT_H #define DICT_H typedef struct dict *dict; dict new (); void insert (dict d, poly key, poly value); poly lookup (dict d, poly key); void delete (dict d, poly key); #endif
33
“ dict ” ADT in C: Implementation // in file “dict.c” #include “linkedList.h” #include “dict.h” struct dict { linkedList l; };
34
Operations: “ new ” dict new () { dict d = malloc (sizeof (*d)); d->l = newLinkedList (); return d; }
35
Operations: “ insert ” void insert (dict d, poly key, poly value) { tuple t = newTuple (key, value); linkedListInsertAtHead (d->l, t); return; } // Leave other functions as programming // assignments.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.