double linked list operations dr. david tsai 2010/4/12

12
Double Linked List Operations Dr. David Tsai 2010/4/12

Upload: gaige-lovell

Post on 30-Mar-2015

221 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Double Linked List Operations Dr. David Tsai 2010/4/12

Double Linked List Operations

Dr. David Tsai2010/4/12

Page 2: Double Linked List Operations Dr. David Tsai 2010/4/12

void createDList (int len, int*array) { int i; DList newnode, before; first = (DList) malloc (sizeof (DNode) ); first->data = array[0]; first->previous = first->next = NULL; before = first /*now = first; */ for (i = 1; i < len; i++) { newnode = (DList) malloc (sizeof(Dnode) ); newnode->data = array[i]; newnode->next = NULL; newnode->previous = before; before->next = newnode; before = newnode; }}

修改自教科書:資料結構理論與實務以 C語言實作 /陳會安

Page 3: Double Linked List Operations Dr. David Tsai 2010/4/12

60

first

NULLNULL

before

50

newnode

NULL

40

NULL

Page 4: Double Linked List Operations Dr. David Tsai 2010/4/12

void printDList () { DList now = first; while (now! = NULL) { back = now; printf (%d, now->data); now = now->next; } printf (\n); now = back->previous; while (now! = NULL) { back = now; printf (%d, now->data); now = now->previous; } printf (\n);}

Page 5: Double Linked List Operations Dr. David Tsai 2010/4/12

now

60

first

NULL

50

NULL

40

back

Page 6: Double Linked List Operations Dr. David Tsai 2010/4/12

void deleteDNode (DList ptr) { if (ptr->previous == NULL) { first = first->next; first->previous = NULL; } else { if (ptr->next == NULL) { ptr->previous->next = NULL; } else{ ptr->previous->next = ptr->next; ptr->next->previous = ptr->previous; } } free (ptr); }

Page 7: Double Linked List Operations Dr. David Tsai 2010/4/12

ptr

60

first

NULL

50

NULL

30 2040

NULL

To Delete First Node

Page 8: Double Linked List Operations Dr. David Tsai 2010/4/12

ptr

60

first

NULL

50

NULL

30 2040

To Delete Last Node

NULL

Page 9: Double Linked List Operations Dr. David Tsai 2010/4/12

ptr

60

first

NULL

50

NULL

30 2040

To Delete a Middle Node

Page 10: Double Linked List Operations Dr. David Tsai 2010/4/12

void insertDNode (DList ptr, int d) { DList newnode = (DList) malloc (sizeof (DNode) ) ; newnode->data = d; newnode->next = newnode->previous = NULL; if (first == NULL) { first = newnode; }

if (ptr == NULL) { newnode->previous = NULL; newnode->next = first; first->previous = newnode; first = newnode; } else { if (ptr->next == NULL) { ptr->next = newnode; newnode->previous = ptr; newnode->next = NULL; } else { ptr->next->previous = newnode; newnode->next = ptr->next; newnode->previous = ptr; ptr->next = newnode; } }}

Page 11: Double Linked List Operations Dr. David Tsai 2010/4/12

50

first

=40

newnode

==

==

60

ptr

= =

To Create a Double Linked ListTo Insert a New Node at Front of the First NodeTo Insert a New Node at End of the Last Node

Page 12: Double Linked List Operations Dr. David Tsai 2010/4/12

50

first

40

newnode

=

=

=

60

ptr

=55

To Insert a New Node into the Middle of the List