Fall 2005 Data Structure Linked List Single Linked

  • Slides: 27
Download presentation
Fall 2005 Data Structure Linked List – Single Linked List Fall 2005 W. F.

Fall 2005 Data Structure Linked List – Single Linked List Fall 2005 W. F. [email protected] comm. ccu

Agenda o Insert node n n o Insert into Empty List at Beginning in

Agenda o Insert node n n o Insert into Empty List at Beginning in Middle at End Delete node n n Fall 2005 Delete First Node General Delete Node W. F. [email protected] comm. ccu 2

Agenda o Insert node n n o Insert into Empty List at Beginning in

Agenda o Insert node n n o Insert into Empty List at Beginning in Middle at End Delete node n n Fall 2005 Delete First Node General Delete Node W. F. [email protected] comm. ccu 3

Insert Node o Three steps to the insertion n Fall 2005 Allocate memory for

Insert Node o Three steps to the insertion n Fall 2005 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. [email protected] comm. ccu 4

Agenda o Insert node n n o Insert into Empty List at Beginning in

Agenda o Insert node n n o Insert into Empty List at Beginning in Middle at End Delete node n n Fall 2005 Delete First Node General Delete Node W. F. [email protected] comm. ccu 5

Insert into Empty List o Creat a new empty list : n Define the

Insert into Empty List o Creat a new empty list : n Define the node’s structure typedef struct list_node *list_pointer; typedef struct list_node{ char data[4]; list_pointer link; } list_node; n Creat a new empty list_pointer ptr = NULL; Fall 2005 W. F. [email protected] comm. ccu ptr NULL 6

Insert into Empty List o Insert a new node into empty list temp =

Insert into Empty List o 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 ptr Fall 2005 temp W. F. [email protected] comm. ccu 7

Agenda o Insert node n n o Insert into Empty List at Beginning in

Agenda o Insert node n n o Insert into Empty List at Beginning in Middle at End Delete node n n Fall 2005 Delete First Node General Delete Node W. F. [email protected] comm. ccu 8

Insert at Beginning (1/2) ptr 20 30 NULL n. Allocate memory for the new

Insert at Beginning (1/2) ptr 20 30 NULL n. Allocate memory for the new node and insert data 10 temp Before insert After insert ptr 20 n. Point the new node’s predecessor to it. Fall 2005 10 30 NULL n. Point the new node to its successor. W. F. [email protected] comm. ccu 9

Insert at Beginning (2/2) ptr 20 30 NULL 10 temp Fall 2005 #include <alloc.

Insert at Beginning (2/2) ptr 20 30 NULL 10 temp Fall 2005 #include <alloc. h> #include <stdlib. h> //page 144 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. [email protected] comm. ccu 10

Agenda o Insert node n n o Insert into Empty List at Beginning in

Agenda o Insert node n n o Insert into Empty List at Beginning in Middle at End Delete node n n Fall 2005 Delete First Node General Delete Node W. F. [email protected] comm. ccu 11

Insert in Middle (1/2) ptr 10 30 NULL Insert 20 between 10 and 30

Insert in Middle (1/2) ptr 10 30 NULL Insert 20 between 10 and 30 20 temp Fall 2005 W. F. [email protected] comm. ccu 12

Insert in Middle (2/2) ptr 10 30 NULL node 20 temp void insert (list_pointer

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 } } Fall 2005 W. F. [email protected] comm. ccu 13

Agenda o Insert node n n o Insert into Empty List at Beginning in

Agenda o Insert node n n o Insert into Empty List at Beginning in Middle at End Delete node n n Fall 2005 Delete First Node General Delete Node W. F. [email protected] comm. ccu 14

Insert at End ptr 10 void insert (…, …) { … } if(*ptr){ temp->link

Insert at End ptr 10 void insert (…, …) { … } if(*ptr){ temp->link = NULL; node->link = temp; } … Fall 2005 20 NULL 30 NULL node temp W. F. [email protected] comm. ccu 15

Agenda o Insert node n n o Insert into Empty List at Beginning in

Agenda o Insert node n n o Insert into Empty List at Beginning in Middle at End Delete node n n Fall 2005 Delete First Node General Delete Node W. F. [email protected] comm. ccu 16

Delete node (1/2) o o Delete node : locate the node first. Assume we

Delete node (1/2) o o Delete node : locate the node first. Assume we have three points n n n 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 Fall 2005 20 W. F. [email protected] comm. ccu 30 NULL 17

Delete node (2/2) o There are only two case: n n Fall 2005 Delete

Delete node (2/2) o There are only two case: n n Fall 2005 Delete the first node Delete any other node W. F. [email protected] comm. ccu 18

Agenda o Insert node n n o Insert into Empty List at Beginning in

Agenda o Insert node n n o Insert into Empty List at Beginning in Middle at End Delete node n n Fall 2005 Delete First Node General Delete Node W. F. [email protected] comm. ccu 19

Delete First Node (1/2) trail NULL node ptr Free 10(node); Fall 2005 20 W.

Delete First Node (1/2) trail NULL node ptr Free 10(node); Fall 2005 20 W. F. [email protected] comm. ccu 30 NULL 20

Delete First Node (2/2) trail ptr NULL node 10 20 30 NULL void delete(list_pointer

Delete First Node (2/2) trail ptr NULL node 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); } Fall 2005 W. F. [email protected] comm. ccu 21

Agenda o Insert node n n o Insert into Empty List at Beginning in

Agenda o Insert node n n o Insert into Empty List at Beginning in Middle at End Delete node n n Fall 2005 Delete First Node General Delete Node W. F. [email protected] comm. ccu 22

General Delete Node (1/4) o Case I : delete node between first node and

General Delete Node (1/4) o Case I : delete node between first node and last node. trail node ptr 10 Fall 2005 20 W. F. [email protected] comm. ccu 30 NULL 23

General Delete Node (2/3) trail ptr node 10 20 30 NULL void delete(list_pointer *ptr,

General Delete Node (2/3) trail ptr node 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); } Fall 2005 W. F. [email protected] comm. ccu 24

General Delete Node (3/4) o Case II : delete last node trail node ptr

General Delete Node (3/4) o Case II : delete last node trail node ptr 10 Fall 2005 20 NULL W. F. [email protected] comm. ccu 30 NULL 25

General Delete Node (4/4) trail ptr 10 20 NULL node 30 NULL void delete(list_pointer

General Delete Node (4/4) trail ptr 10 20 NULL node 30 NULL void delete(list_pointer *ptr, list_pointer trail, list_pointer node) { if (trail) trail->link = NULL; else *ptr = (*ptr)->link; free(node); } Fall 2005 W. F. [email protected] comm. ccu 26

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

Q & A Fall 2005 W. F. [email protected] comm. ccu 27