Double Linked List Operations Dr. David Tsai 2010/4/12.
-
Upload
gaige-lovell -
Category
Documents
-
view
221 -
download
3
Transcript of Double Linked List Operations Dr. David Tsai 2010/4/12.
![Page 1: Double Linked List Operations Dr. David Tsai 2010/4/12.](https://reader036.fdocuments.in/reader036/viewer/2022081518/5518d123550346b31f8b5cc7/html5/thumbnails/1.jpg)
Double Linked List Operations
Dr. David Tsai2010/4/12
![Page 2: Double Linked List Operations Dr. David Tsai 2010/4/12.](https://reader036.fdocuments.in/reader036/viewer/2022081518/5518d123550346b31f8b5cc7/html5/thumbnails/2.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081518/5518d123550346b31f8b5cc7/html5/thumbnails/3.jpg)
60
first
NULLNULL
before
50
newnode
NULL
40
NULL
![Page 4: Double Linked List Operations Dr. David Tsai 2010/4/12.](https://reader036.fdocuments.in/reader036/viewer/2022081518/5518d123550346b31f8b5cc7/html5/thumbnails/4.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081518/5518d123550346b31f8b5cc7/html5/thumbnails/5.jpg)
now
60
first
NULL
50
NULL
40
back
![Page 6: Double Linked List Operations Dr. David Tsai 2010/4/12.](https://reader036.fdocuments.in/reader036/viewer/2022081518/5518d123550346b31f8b5cc7/html5/thumbnails/6.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081518/5518d123550346b31f8b5cc7/html5/thumbnails/7.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081518/5518d123550346b31f8b5cc7/html5/thumbnails/8.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081518/5518d123550346b31f8b5cc7/html5/thumbnails/9.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081518/5518d123550346b31f8b5cc7/html5/thumbnails/10.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081518/5518d123550346b31f8b5cc7/html5/thumbnails/11.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081518/5518d123550346b31f8b5cc7/html5/thumbnails/12.jpg)
50
first
40
newnode
=
=
=
60
ptr
=55
To Insert a New Node into the Middle of the List