1 / 27

Fall 2005 Data Structure

Fall 2005 Data Structure. Linked List – Single Linked List. Agenda. Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node. Agenda. Insert node Insert into Empty List Insert at Beginning

Download Presentation

Fall 2005 Data Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fall 2005 Data Structure Linked List – Single Linked List

  2. Agenda • Insert node • Insert into Empty List • Insert at Beginning • Insert in Middle • Insert at End • Delete node • Delete First Node • General Delete Node W.F. Hu@ant.comm.ccu

  3. Agenda • Insert node • Insert into Empty List • Insert at Beginning • Insert in Middle • Insert at End • Delete node • Delete First Node • General Delete Node W.F. Hu@ant.comm.ccu

  4. Insert Node • Three steps to the insertion • Allocate memory for the new node and insert data • Point the new node to its successor. • Point the new node’s predecessor to it. W.F. Hu@ant.comm.ccu

  5. Agenda • Insert node • Insert into Empty List • Insert at Beginning • Insert in Middle • Insert at End • Delete node • Delete First Node • General Delete Node W.F. Hu@ant.comm.ccu

  6. Insert into Empty List • Creat a new empty list : • Define the node’s structure • Creat a new empty list list_pointer ptr = NULL; typedef struct list_node *list_pointer; typedef struct list_node{ char data[4]; list_pointer link; } list_node; ptr NULL W.F. Hu@ant.comm.ccu

  7. Insert into Empty List • Insert a new node into empty list temp = (list_pointer) malloc (sizeof(list_node)); *ptr = temp; Address of first node ptr->data ptr->link NULL NULL ptr temp W.F. Hu@ant.comm.ccu

  8. Agenda • Insert node • Insert into Empty List • Insert at Beginning • Insert in Middle • Insert at End • Delete node • Delete First Node • General Delete Node W.F. Hu@ant.comm.ccu

  9. Insert at Beginning (1/2) ptr 20 30 NULL 10 • Allocate memory for the new node and insert data temp Before insert After insert ptr 20 30 NULL 10 • Point the new node to its successor. • Point the new node’s predecessor to it. W.F. Hu@ant.comm.ccu

  10. Insert at Beginning (2/2) ptr 20 30 NULL 10 temp #include <stdlib.h> //page 141 #include <alloc.h> list_pointer temp; temp = (list_pointer) malloc (sizeof(list_node)); temp->data = 10; if(*ptr){ temp->link = *ptr; *ptr = temp; } else{ temp->link = NULL; *ptr = temp } W.F. Hu@ant.comm.ccu

  11. Agenda • Insert node • Insert into Empty List • Insert at Beginning • Insert in Middle • Insert at End • Delete node • Delete First Node • General Delete Node W.F. Hu@ant.comm.ccu

  12. Insert in Middle (1/2) ptr 10 30 NULL Insert 20 between 10 and 30 20 temp W.F. Hu@ant.comm.ccu

  13. Insert in Middle (2/2) ptr 10 30 NULL node 20 temp void insert (list_pointer *ptr, list_pointer node) { if(*ptr){ temp->link = node->link; node->link = temp; } else{ temp->link = NULL; *ptr = temp } } W.F. Hu@ant.comm.ccu

  14. Agenda • Insert node • Insert into Empty List • Insert at Beginning • Insert in Middle • Insert at End • Delete node • Delete First Node • General Delete Node W.F. Hu@ant.comm.ccu

  15. Insert at End ptr 10 20 NULL node void insert (…, …) { … if(*ptr){ temp->link = NULL; node->link = temp; } … } 30 NULL temp W.F. Hu@ant.comm.ccu

  16. Agenda • Insert node • Insert into Empty List • Insert at Beginning • Insert in Middle • Insert at End • Delete node • Delete First Node • General Delete Node W.F. Hu@ant.comm.ccu

  17. Delete node (1/2) • Delete node : locate the node first. • Assume we have three points • ptr : point to the start of the list • node : points to the node that we wish to delete • trail : points to the predecessor trail node ptr 10 20 30 NULL W.F. Hu@ant.comm.ccu

  18. Delete node (2/2) • There are only two case: • Delete the first node • Delete any other node W.F. Hu@ant.comm.ccu

  19. Agenda • Insert node • Insert into Empty List • Insert at Beginning • Insert in Middle • Insert at End • Delete node • Delete First Node • General Delete Node W.F. Hu@ant.comm.ccu

  20. Delete First Node (1/2) NULL trail node ptr 10 20 30 NULL Free (node); W.F. Hu@ant.comm.ccu

  21. Delete First Node (2/2) NULL trail node ptr 10 20 30 NULL void delete(list_pointer *ptr, list_pointer trail, list_pointer node) { if (trail) trail->link = node->link; else *ptr = (*ptr)->link; free(node); } W.F. Hu@ant.comm.ccu

  22. Agenda • Insert node • Insert into Empty List • Insert at Beginning • Insert in Middle • Insert at End • Delete node • Delete First Node • General Delete Node W.F. Hu@ant.comm.ccu

  23. General Delete Node (1/4) • Case I : delete node between first node and last node. trail node ptr 10 20 30 NULL W.F. Hu@ant.comm.ccu

  24. General Delete Node (2/3) trail node ptr 10 20 30 NULL void delete(list_pointer *ptr, list_pointer trail, list_pointer node) { if (trail) trail->link = node->link; else *ptr = (*ptr)->link; free(node); } W.F. Hu@ant.comm.ccu

  25. General Delete Node (3/4) • Case II : delete last node trail node ptr 10 20 30 NULL NULL W.F. Hu@ant.comm.ccu

  26. General Delete Node (4/4) trail node ptr 10 20 30 NULL NULL void delete(list_pointer *ptr, list_pointer trail, list_pointer node) { if (trail) trail->link = NULL; else *ptr = (*ptr)->link; free(node); } W.F. Hu@ant.comm.ccu

  27. Q & A W.F. Hu@ant.comm.ccu

More Related