Presentation is loading. Please wait.

Presentation is loading. Please wait.

Doubly-linked list library. typedef struct dllist { struct dllist *flink; struct dllist *blink; Jval val; } *Dllist; Link to next node Link to previous.

Similar presentations


Presentation on theme: "Doubly-linked list library. typedef struct dllist { struct dllist *flink; struct dllist *blink; Jval val; } *Dllist; Link to next node Link to previous."— Presentation transcript:

1 Doubly-linked list library. typedef struct dllist { struct dllist *flink; struct dllist *blink; Jval val; } *Dllist; Link to next node Link to previous node The node’s value flink blink val Next node Previous node

2 Dllist library structure Circular Doubly-linked With a sentinel (header) node flink blink val flink blink val flink blink val … flink blink val flink blink val list

3 Creating a list: new_dllist() flink blink val l Dllist l; l = new_dllist();

4 Augmenting a list: dll_append() flink blink val l Dllist l; l = new_dllist(); dll_append(l, new_jval_i(3)); flink blink val.i=3

5 Augmenting a list: dll_append() flink blink val l Dllist l; l = new_dllist(); dll_append(l, new_jval_i(3)); dll_append(l, new_jval_i(8)); flink blink val.i=3 flink blink val.i=8

6 What will this print? flink blink val l Dllist l; l = new_dllist(); dll_append(l, new_jval_i(3)); dll_append(l, new_jval_i(8)); printf(“%d\n”, l->flink->val.i); flink blink val.i=3 flink blink val.i=8

7 What will this print? flink blink val l Dllist l; l = new_dllist(); dll_append(l, new_jval_i(3)); dll_append(l, new_jval_i(8)); printf(“%d\n”, l->flink->val.i); flink blink val.i=3 flink blink val.i=8 3

8 Augmenting a list: dll_prepend() flink blink val l Dllist l; l = new_dllist(); dll_append(l, new_jval_i(3)); dll_append(l, new_jval_i(8)); dll_prepend(l, new_jval_s(“Jim”)); flink blink val.s flink blink val.i=3 flink blink val.i=8 “Jim”

9 What will this print? flink blink val l Dllist l; l = new_dllist(); dll_append(l, new_jval_i(3)); dll_append(l, new_jval_i(8)); dll_prepend(l, new_jval_s(“Jim”)); printf(“%d\n”, l->flink->val.i); flink blink val.s flink blink val.i=3 flink blink val.i=8 “Jim”

10 What will this print? flink blink val l Dllist l; l = new_dllist(); dll_append(l, new_jval_i(3)); dll_append(l, new_jval_i(8)); dll_prepend(l, new_jval_s(“Jim”)); printf(“%d\n”, l->flink->val.i); flink blink val.s flink blink val.i=3 flink blink val.i=8 “Jim” (or something similar)

11 Augmenting a list: dll_insert_b() flink blink val l Dllist l; l = new_dllist(); dll_append(l, new_jval_i(3)); dll_append(l, new_jval_i(8)); dll_insert_b(l->flink->flink, new_jval_s(“Jim”)); flink blink val.i=3 flink blink val.s flink blink val.i=8 “Jim” Inserts before a given node.

12 What will this do? flink blink val l Dllist l; l = new_dllist(); dll_append(l, new_jval_i(3)); dll_append(l, new_jval_i(8)); dll_insert_b(l, new_jval_s(“Jim”); flink blink val.i=3 flink blink val.i=8

13 What will this do? Dllist l; l = new_dllist(); dll_append(l, new_jval_i(3)); dll_append(l, new_jval_i(8)); dll_insert_b(l, new_jval_s(“Jim”); flink blink val l flink blink val.i=3 flink blink val.i=8 flink blink val.s “Jim”

14 Deleting: dll_delete_node() Dllist l; l = new_dllist(); dll_append(l, new_jval_i(3)); dll_append(l, new_jval_i(8)); dll_insert_b(l, new_jval_s(“Jim”); dll_delete_node(l->blink); flink blink val l flink blink val.i=3 flink blink val.i=8 flink blink val.s “Jim” before

15 Deleting: dll_delete_node() Dllist l; l = new_dllist(); dll_append(l, new_jval_i(3)); dll_append(l, new_jval_i(8)); dll_insert_b(l, new_jval_s(“Jim”); dll_delete_node(l->blink); after flink blink val l flink blink val.i=3 flink blink val.i=8

16 Deleting: free_dllist() Destroys a list Including the sentinel node. Frees all malloc’d nodes Does not free jvals! IS is; Dllist l; l = new_dllist(); is = new_inputstruct(NULL); while(get_line(is) >= 0) { dll_prepend(l, new_jval_s(strdup(is->text1))); } free_dllist(l);

17 Handy macros (from dllist.h) #define dll_first(d) ((d)->flink) #define dll_next(d) ((d)->flink) #define dll_last(d) ((d)->blink) #define dll_prev(d) ((d)->blink) #define dll_nil(d) (d) flink blink val l flink blink val.i=3 flink blink val.i=8

18 Traversal macros: weird but useful! #define dll_traverse(ptr, list) \ for (ptr = (list)->flink; ptr != (list); ptr = ptr->flink) #define dll_rtraverse(ptr, list) \ for (ptr = (list)->blink; ptr != (list); ptr = ptr->blink) flink blink val l flink blink val.i=3 flink blink val.i=8

19 Two programs to reverse stdin: #include “fields.h” #include “dllist.h” main() { IS is; Dllist l; is = new_inputstruct(NULL); l = new_dllist(); while(get_line(is) >= 0) { dll_prepend(l, new_jval_s(strdup(is->text1))); } dll_traverse(tmp, l) { printf(“%s”, tmp->val.s); } #1

20 Two programs to reverse stdin: #include “fields.h” #include “dllist.h” main() { IS is; Dllist l; is = new_inputstruct(NULL); l = new_dllist(); while(get_line(is) >= 0) { dll_append(l, new_jval_s(strdup(is->text1))); } dll_rtraverse(tmp, l) { printf(“%s”, tmp->val.s); } #2

21 Implementation: new_dllist() flink blink val d Dllist new_dllist() { Dllist d; d = (Dllist) malloc (sizeof(struct dllist)); d->flink = d; d->blink = d; return d; }

22 Implementation: dll_insert_b() void dll_insert_b(Dllist node, Jval v) { Dllist newnode; newnode = (Dllist) malloc (sizeof(struct dllist)); newnode->val = v; newnode->flink = node; newnode->blink = node->blink; newnode->flink->blink = newnode; newnode->blink->flink = newnode; } flink blink val.i=3 flink blink val.i=8 flink blink val.i=2 e.g. before dll_insert_b(n, new_jval_i(10)); n

23 Implementation: dll_insert_b() void dll_insert_b(Dllist node, Jval v) { Dllist newnode; newnode = (Dllist) malloc (sizeof(struct dllist)); newnode->val = v; newnode->flink = node; newnode->blink = node->blink; newnode->flink->blink = newnode; newnode->blink->flink = newnode; } flink blink val.i=3 flink blink val.i=8 flink blink val.i=2 dll_insert_b(n, new_jval_i(10)); n flink blink val newnode

24 Implementation: dll_insert_b() void dll_insert_b(Dllist node, Jval v) { Dllist newnode; newnode = (Dllist) malloc (sizeof(struct dllist)); newnode->val = v; newnode->flink = node; newnode->blink = node->blink; newnode->flink->blink = newnode; newnode->blink->flink = newnode; } flink blink val.i=3 flink blink val.i=8 flink blink val.i=2 dll_insert_b(n, new_jval_i(10)); n flink blink val.i=10 newnode

25 Implementation: dll_insert_b() void dll_insert_b(Dllist node, Jval v) { Dllist newnode; newnode = (Dllist) malloc (sizeof(struct dllist)); newnode->val = v; newnode->flink = node; newnode->blink = node->blink; newnode->flink->blink = newnode; newnode->blink->flink = newnode; } flink blink val.i=3 flink blink val.i=8 flink blink val.i=2 dll_insert_b(n, new_jval_i(10)); n flink blink val.i=10 newnode

26 Implementation: dll_insert_b() void dll_insert_b(Dllist node, Jval v) { Dllist newnode; newnode = (Dllist) malloc (sizeof(struct dllist)); newnode->val = v; newnode->flink = node; newnode->blink = node->blink; newnode->flink->blink = newnode; newnode->blink->flink = newnode; } flink blink val.i=3 flink blink val.i=8 flink blink val.i=2 dll_insert_b(n, new_jval_i(10)); n flink blink val.i=10 newnode

27 Implementation: dll_insert_b() void dll_insert_b(Dllist node, Jval v) { Dllist newnode; newnode = (Dllist) malloc (sizeof(struct dllist)); newnode->val = v; newnode->flink = node; newnode->blink = node->blink; newnode->flink->blink = newnode; newnode->blink->flink = newnode; } flink blink val.i=3 flink blink val.i=8 flink blink val.i=2 dll_insert_b(n, new_jval_i(10)); n flink blink val.i=10 newnode

28 Implementation: dll_insert_b() void dll_insert_b(Dllist node, Jval v) { Dllist newnode; newnode = (Dllist) malloc (sizeof(struct dllist)); newnode->val = v; newnode->flink = node; newnode->blink = node->blink; newnode->flink->blink = newnode; newnode->blink->flink = newnode; } flink blink val.i=3 flink blink val.i=8 flink blink val.i=2 dll_insert_b(n, new_jval_i(10)); n flink blink val.i=10 newnode

29 Implementation: dll_insert_b() void dll_insert_b(Dllist node, Jval v) { Dllist newnode; newnode = (Dllist) malloc (sizeof(struct dllist)); newnode->val = v; newnode->flink = node; newnode->blink = node->blink; newnode->flink->blink = newnode; newnode->blink->flink = newnode; } dll_insert_b(n, new_jval_i(10)); flink blink val.i=3 n flink blink val.i=10 flink blink val.i=8 flink blink val.i=2

30 Implementation: dll_insert_a() void dll_insert_a(Dllist n, Jval val) { dll_insert_b(n->flink, val); } flink blink val n flink blink val.i=3 flink blink val.i=8

31 Implementation: dll_append() void dll_append(Dllist l, Jval val) { dll_insert_b(l, val); } flink blink val l flink blink val.i=3 flink blink val.i=8

32 Implementation: dll_prepend() void dll_prepend(Dllist l, Jval val) { dll_insert_a(l, val); } flink blink val l flink blink val.i=3 flink blink val.i=8

33 Implementation: dll_delete_node() void dll_delete_node(Dllist node) { node->flink->blink = node->blink; node->blink->flink = node->flink; free(node); } flink blink val.i=3 flink blink val.i=8 flink blink val.i=2 e.g. before dll_delete_node(n); n

34 Implementation: dll_delete_node() void dll_delete_node(Dllist node) { node->flink->blink = node->blink; node->blink->flink = node->flink; free(node); } flink blink val.i=3 flink blink val.i=8 flink blink val.i=2 dll_delete_node(n); n

35 Implementation: dll_delete_node() void dll_delete_node(Dllist node) { node->flink->blink = node->blink; node->blink->flink = node->flink; free(node); } flink blink val.i=3 flink blink val.i=8 flink blink val.i=2 dll_delete_node(n); n

36 Implementation: dll_delete_node() void dll_delete_node(Dllist node) { node->flink->blink = node->blink; node->blink->flink = node->flink; free(node); } flink blink val.i=3 flink blink val.i=2 dll_delete_node(n); n

37 Implementation: dll_delete_node() void dll_delete_node(Dllist node) { node->flink->blink = node->blink; node->blink->flink = node->flink; free(node); } flink blink val.i=3 flink blink val.i=2 dll_delete_node(n); n ?

38 Implementation: free_dllist() void free_dllist(Dllist l) { while (!dll_empty(l)) { dll_delete_node(dll_first(l)); } free(l); } flink blink val l flink blink val.i=3 flink blink val.i=8

39 Implementation: dll_empty() int dll_empty(Dllist l) { return (l->flink == l); } flink blink val l flink blink val.i=3 flink blink val.i=8


Download ppt "Doubly-linked list library. typedef struct dllist { struct dllist *flink; struct dllist *blink; Jval val; } *Dllist; Link to next node Link to previous."

Similar presentations


Ads by Google