CS 261: Data Structures. Double Linked List Deque. Double Linked List Bag

CS 261: Data Structures Double Linked List Deque Double Linked List Bag 1 Double Links • Allow access to both next and previous link struct dlink { ...
Author: Derek Bennett
14 downloads 0 Views 145KB Size
CS 261: Data Structures Double Linked List Deque Double Linked List Bag 1

Double Links • Allow access to both next and previous link struct dlink { TYPE value; struct dlink * next; struct dlink * previous; };

val

links

val

links



links

val 2

Deque struct dlink{ TYPE value; struct dlink *next; struct dlink *prev; };

struct listDeque{ int size; struct dlink * frontSentinel; struct dlink * backSentinel; };

backSentinel

Deque frontSentinel prev

prev

next

Sentinel

Link

prev next



prev

prev next

next

Link next

Sentinel

Remove from Front or Back void removeFrontDeque (struct listDeque *dq) { _removeDeque(dq, dq->frontSentinel->next); }

void removeBackDeque (struct listDeque *dq) { _removeDeque(dq, dq->backSentinel->prev); }

4

Remove Double Link from Deque void _removeDeque (struct linkedList *dq, struct dlink *lnk) { assert(!isEmptyDeque(dq));

} Deque

backSentinel frontSentinel

prev

prev next

Link

prev next

prev next

Link next

Remove Double Link from Deque void _removeDeque (struct linkedList *dq, struct dlink *lnk) { assert(!isEmptyDeque(dq)); lnk->prev->next = lnk->next;

} Deque

backSentinel frontSentinel

prev

prev next

prev

Link

prev

Link next

next

next

Remove Double Link from Deque void _removeDeque (struct linkedList *dq, struct dlink *lnk) { assert(!isEmptyDeque(dq)); lnk->prev->next = lnk->next; lnk->next->prev = lnk->prev;

} Deque

backSentinel frontSentinel

prev

prev next

prev

Link

prev

Link next

next

next

Remove Double Link from Deque void _removeDeque (struct linkedList *dq, struct dlink *lnk) { assert(!isEmptyDeque(dq)); lnk->prev->next = lnk->next; lnk->next->prev = lnk->prev; free(lnk); dq->size--; } backSentinel Deque frontSentinel prev

prev

prev next

Link next

prev

next

next

D-Bag Arbitrary ordering of elements in the collection

db prev

prev next

frontSentinel

Link

prev next



prev

prev next

Link

next

next

backSentinel 9

D-Bag Structure struct dlink { TYPE value; struct dlink * struct dlink * }; struct listDBag { int size; struct dlink * struct dlink * };

next; prev;

frontSentinel; backSentinel;

db prev

prev next

frontSentinel

Link

prev next



prev

prev next

Link

next

next

backSentinel 10

Bag Interface void initBag();

/* Initialize the bag*/

int isEmptyBag();

/* Check if the bag is empty*/

void addBag();

/* Add value*/

void removeBag();

/* Remove value*/

void containsBag();/* Check if a value is in the bag*/

11

initDBag void initDBag (struct listDBag *db) { assert(db); db->frontSentinel = (struct dlink *) malloc(sizeof(struct dlink)); assert(db->frontSentinel != 0); db->backSentinel = (struct dlink *) malloc(sizeof(struct dlink)); assert(db->backSentinel != 0); ... }

db

frontSentinel

backSentinel 12

initDBag void initDBag (struct listDBag *db) { assert(db); db->frontSentinel = (struct dlink *) malloc(sizeof(struct dlink)); assert(db->frontSentinel != 0); db->backSentinel = (struct dlink *) malloc(sizeof(struct dlink)); assert(db->backSentinel != 0); db->frontSentinel->next = db->backSentinel; db->backSentinel->prev = db->frontSentinal; db->size = 0; db }

?

prev

frontSentinel

prev next

?

next

backSentinel

isEmpty void isEmptyDBag (struct listDBag *db){ assert(db); return db->size == 0; }

db prev

frontSentinel

prev next

next 14

backSentinel

AddDBag void addDBag (struct listDBag *db, TYPE e) { assert(db); _addDList(db, db->frontSentinel->next, e); }

backSentinel

db frontSentinel

prev

prev next

lnk

next

AddDList void _addDList (struct listDBag *db, struct dlink *lnk, TYPE e) {

VOLUNTEERS! }

backSentinel

db frontSentinel

prev

prev next

lnk

next

AddDList void _addDList (struct listDBag *db, struct dlink *lnk, TYPE e) { struct dlink *new; new =(struct dlink *) malloc(sizeof(struct dlink)); assert(new); ...

}

new

1st step:

prev next

memory allocation

backSentinel

db frontSentinel

prev

prev next

lnk

next

AddDList void _addDList (struct listDBag *db, struct dlink *lnk, TYPE e) { struct dlink *new; new =(struct dlink *) malloc(sizeof(struct dlink)); assert(new);

new->value = e; new->next = lnk; new->prev = lnk->prev; ... }

2st step:

putting the new link in the bag db frontSentinel

prev next

new

backSentinel prev next

prev

lnk

next

AddDList void _addDList (struct listDBag *db, struct dlink *lnk, TYPE e) { struct dlink *new; new =(struct dlink *) malloc(sizeof(struct dlink)); assert(new); new->value = e; new->next = lnk; new->prev = lnk->prev;

lnk->prev->next = new; lnk->prev = new; }

3st step: re-connecting the linked list

backSentinel

db frontSentinel

prev next

prev

newlink

next

prev

lnk

next

AddDList void _addDList (struct listDBag *db, struct dlink *lnk, TYPE e) { struct dlink *new; new =(struct dlink *) malloc(sizeof(struct dlink)); assert(new); new->value = e; new->next = lnk; new->prev = lnk->prev; lnk->prev->next = new; lnk->prev = new;

db->size++; }

4th step: increment the size

backSentinel

db frontSentinel

prev next

prev

newlink

next

prev

lnk

next

Contains Bag int containsDBag (struct listDBag *db, TYPE e) { struct dlink *lnk; assert(!isEmptyBag(db)); /*Find the link of e in the d-linked list bag*/ lnk = _containsDList(db,e); if (lnk) return 1; return 0; } 21

Contains Returning the Pointer struct dlink* _containsDList(struct listDBag *db, TYPE e) { struct dlink *current = db->frontSentinel->next; while(current != db->backSentinel){ if(current->value == e) return current; current = current->next; } return NULL; /* e not found */ }

db

backSentinel

prev prev … prev Link Link Link next next next next next prev

frontSentinel

prev

22

Remove Bag void removeDBag (struct listDBag *db, TYPE e) { struct dlink *lnk assert(!isEmptyBag(db)); lnk = _containsDList(db,e); ...

}

db

backSentinel

prev prev … prev Link Link Link next next next next next prev

frontSentinel

prev

23

Remove Bag void removeDBag (struct listDBag *db, TYPE e) { struct dlink *lnk assert(!isEmptyBag(db)); lnk = _containsDList(db,e); /* Removes only 1 copy */ _removeDLink(db, lnk); }

24

Remove Link void _removeLink (struct listDBag *db, struct dlink *lnk) { /* already checked that db is not empty */ ...

db

}

next

frontSentinel

Link

prev next

Link =

prev

lnk

prev next

backSentinel prev

Link

next 25

Remove Link void _removeLink (struct listDBag *db, struct dlink *lnk) { /* already checked that db is not empty */ lnk->prev->next = lnk->next; ...

db

} prev next

frontSentinel

Link

prev next

lnk

prev next

backSentinel prev

Link

next 26

Remove Link void _removeLink (struct listDBag *db, struct dlink *lnk) { /* already checked that db is not empty */ lnk->prev->next = lnk->next; lnk->next->prev = lnk->prev; ... } db

prev next

frontSentinel

Link

prev next

backSentinel prev

prev

lnk

next

Link

next 27

Remove Link void _removeLink (struct listDBag *db, struct dlink *lnk) { /* already checked that db is not empty */ lnk->prev->next = lnk->next; lnk->next->prev = lnk->prev; free(lnk); db->size--; db

}

frontSentinel

prev

prev

prev next

backSentinel

Link

next

Link

next 28

Remove Bag All Copies void removeDBag (struct listDBag *db, TYPE e) { struct dlink *lnk assert(!isEmptyBag(db)); lnk = _containsDList(db,e); while(lnk){ _removeDLink(db, lnk); lnk = _containsDList(db,e); } }

29