list class.head null _class cell { void *object; cell *next; public:... } _class list { cell *head;...

41
List class .head NULL _class Cell { void *object; Cell *next; public: ... } _class List { Cell *head; public: ... }

Post on 15-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class

.head

NULL_class Cell { void *object; Cell *next;

public: ... }

_class List { Cell *head;

public: ... }

Page 2: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

Insert objects into a List

Page 3: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class

.object

NULL

head

Page 4: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class

.object

.cell = new Cell();NULL

head

Page 5: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class

NULL

.object

.cell

cell->next = NULL;cell->object = object;

NULL head

Page 6: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class

.object

.cell

head = cell;

NULL head

Page 7: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class

head NULL

Page 8: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class

NULL

cell = new Cell();

object

head

Page 9: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class

NULL

cell

head

cell->next = NULL;cell->object = object;

object

NULL

Page 10: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class head->next = cell;

NULL

cell

object

head

Page 11: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class

head->next = cell;

NULL

head

Page 12: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

Find and remove an object

Page 13: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class - find an object and remove it head

NULL

Page 14: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class - find an object and remove it

NULL

if (find(head->object, object)) {...

head

Page 15: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class - find an object and remove it

NULL

if (find(ptr->next->object, object)) {

.ptr

head

Page 16: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class - find an object and remove it

NULL

ptr = ptr->next;

.ptr

head

Page 17: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class - find an object and remove it

NULL

if (find(ptr->next->object, object)) {

.ptr

head

Page 18: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class - find an object and remove it

NULL

void *object = ptr->next->object

_object

head

.ptr

Page 19: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class - find an object and remove it

NULL

Cell *tmp = ptr->next;

.ptr

_object

tmp

head

Page 20: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class - find an object and remove it

NULL

ptr->next = ptr->next->next;

_object

tmp

head

.ptr

Page 21: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class - find an object and remove it

NULL

delete tmp;

_object

tmp

head

.ptr

Page 22: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

List class - find an object and remove it

NULL

return object;

_object

head

Page 23: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

Doubly Linked Lists

Page 24: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

Doubly Linked Lists

headNULL

.tail

Page 25: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

Doubly Linked Lists

head

NULLNULL

.tail

Page 26: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

Doubly Linked Lists

head

NULL

.tail

NULL

Page 27: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

Doubly Linked Lists

head

NULL

tail

NULL

Page 28: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

A Queue

Page 29: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

.tail

Circularly linked list

Page 30: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

.tail

Queue: insert item at rear, remove at front

Page 31: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

.tail

Queue: remove from front

void *object = tail->next->object;

_object

Page 32: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

.tail

Queue: remove from front

tmp = tail->next;

_object

Temp

tmp

Page 33: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

.tail

Queue: remove from front

_tail->next = tail->next->next;

_object

Temp

tmp

Page 34: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

.tail

Queue: remove from front

delete tmp;

_object

Temp

tmp

Page 35: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

.tail

Queue: remove from front

_return object;

_object

Temp

Page 36: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

.tail

Queue: remove from front

_

Temp

Page 37: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

.tail

Queue: insert at rear

_

Temp

Page 38: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

.tail

Queue: insert at rear

_cell = new Cell(object);

Temp

_cell

NULL

Page 39: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

.tail

Queue: insert at rear

_cell->next = tail->next;

Temp

_cell

Page 40: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

.tail

Queue: insert at rear

_tail->next = cell;

Temp

_cell

Page 41: List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }

.tail

Queue: insert at rear

_tail = tail->next;

Temp

_cell