Download presentation
Presentation is loading. Please wait.
Published byReynaldo Gamlin Modified over 10 years ago
1
Fall 2005 Data Structure Linked List – Single Linked List
2
Fall 2005W.F. Hu@ant.comm.ccu2 Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node
3
Fall 2005W.F. Hu@ant.comm.ccu3 Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node
4
Fall 2005W.F. Hu@ant.comm.ccu4 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.
5
Fall 2005W.F. Hu@ant.comm.ccu5 Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node
6
Fall 2005W.F. Hu@ant.comm.ccu6 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
7
Fall 2005W.F. Hu@ant.comm.ccu7 Insert into Empty List Insert a new node into empty list temp = (list_pointer) malloc (sizeof(list_node)); ptr = temp; ptr Address of first node NULL ptr->dataptr->link NULL temp
8
Fall 2005W.F. Hu@ant.comm.ccu8 Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node
9
Fall 2005W.F. Hu@ant.comm.ccu9 Insert at Beginning (1/2) 20 ptr 30 NULL 10 temp 20 ptr 30 NULL 10 Allocate memory for the new node and insert data Point the new node to its successor. Point the new node s predecessor to it. Before insert After insert
10
Fall 2005W.F. Hu@ant.comm.ccu10 Insert at Beginning (2/2) #include 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 } temp 30 NULL 20 ptr 10 #include //page 144
11
Fall 2005W.F. Hu@ant.comm.ccu11 Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node
12
Fall 2005W.F. Hu@ant.comm.ccu12 Insert in Middle (1/2) 20 30 NULL ptr 10 temp Insert 20 between 10 and 30
13
Fall 2005W.F. Hu@ant.comm.ccu13 Insert in Middle (2/2) ptr 10 temp 20 30 NULL node void insert (list_pointer *ptr, list_pointer node) { if(*ptr){ temp->link = node->link; node->link = temp; } else{ temp->link = NULL; *ptr = temp } }
14
Fall 2005W.F. Hu@ant.comm.ccu14 Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node
15
Fall 2005W.F. Hu@ant.comm.ccu15 Insert at End 20 ptr 10 30 temp NULL node void insert ( …, … ) { … if(*ptr){ temp->link = NULL; node->link = temp; } … }
16
Fall 2005W.F. Hu@ant.comm.ccu16 Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node
17
Fall 2005W.F. Hu@ant.comm.ccu17 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 20 30 NULL ptr 10 nodetrail
18
Fall 2005W.F. Hu@ant.comm.ccu18 Delete node (2/2) There are only two case: Delete the first node Delete any other node
19
Fall 2005W.F. Hu@ant.comm.ccu19 Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node
20
Fall 2005W.F. Hu@ant.comm.ccu20 Delete First Node (1/2) 20 30 NULL ptr 10 trail node NULL Free (node);
21
Fall 2005W.F. Hu@ant.comm.ccu21 Delete First Node (2/2) ptr 10 trail NULL 20 30 NULL node void delete(list_pointer *ptr, list_pointer trail, list_pointer node) { if (trail) trail->link = node->link; else *ptr = (*ptr)->link; free(node); }
22
Fall 2005W.F. Hu@ant.comm.ccu22 Agenda Insert node Insert into Empty List Insert at Beginning Insert in Middle Insert at End Delete node Delete First Node General Delete Node
23
Fall 2005W.F. Hu@ant.comm.ccu23 General Delete Node (1/4) Case I : delete node between first node and last node. 20 30 NULL ptr 10 trailnode
24
Fall 2005W.F. Hu@ant.comm.ccu24 General Delete Node (2/3) ptr 10 trail 20 30 NULL node void delete(list_pointer *ptr, list_pointer trail, list_pointer node) { if (trail) trail->link = node->link; else *ptr = (*ptr)->link; free(node); }
25
Fall 2005W.F. Hu@ant.comm.ccu25 General Delete Node (3/4) Case II : delete last node 20 30 NULL ptr 10 nodetrail NULL
26
Fall 2005W.F. Hu@ant.comm.ccu26 General Delete Node (4/4) ptr 10 trail 20 30 NULL node void delete(list_pointer *ptr, list_pointer trail, list_pointer node) { if (trail) trail->link = NULL; else *ptr = (*ptr)->link; free(node); } NULL
27
Fall 2005W.F. Hu@ant.comm.ccu27 Q & A
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.