27
Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 Data Structure Linked List – Single Linked List

Embed Size (px)

Citation preview

Page 1: Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 Data Structure

Linked List – Single Linked List

Page 2: Fall 2005 Data Structure Linked List – Single Linked List

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

Page 3: Fall 2005 Data Structure Linked List – Single Linked List

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

Page 4: Fall 2005 Data Structure Linked List – Single Linked List

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

Page 5: Fall 2005 Data Structure Linked List – Single Linked List

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

Page 6: Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 W.F. [email protected] 6

Insert into Empty List

Creat a new empty list : Define the node’s structure

Creat a new empty listlist_pointer ptr = NULL;

typedef struct list_node *list_pointer;

typedef struct list_node{

char data[4];

list_pointer link;

} list_node; ptr

NULL

Page 7: Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 W.F. [email protected] 7

Insert into Empty List

Insert a new node into empty listtemp = (list_pointer) malloc (sizeof(list_node));

ptr = temp;

ptr

Address of first node

NULLNULL

ptr->data ptr->link

NULL

temp

Page 8: Fall 2005 Data Structure Linked List – Single Linked List

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

Page 9: Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 W.F. [email protected] 9

Insert at Beginning (1/2)

2020

ptr3030 NULLNULL

1010

temp

2020

ptr3030 NULLNULL

1010

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 insertAfter insert

Page 10: Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 W.F. [email protected] 10

Insert at Beginning (2/2)

#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}

temp

3030 NULLNULL2020

ptr

10

#include <stdlib.h> //page 144

Page 11: Fall 2005 Data Structure Linked List – Single Linked List

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

Page 12: Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 W.F. [email protected] 12

Insert in Middle (1/2)

2020

3030 NULLNULL

ptr1010

temp

Insert 20 between 10 and 30

Page 13: Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 W.F. [email protected] 13

Insert in Middle (2/2)

ptr1010

temp2020

3030 NULLNULL

node

void insert (list_pointer *ptr, list_pointer node){

if(*ptr){ temp->link = node->link; node->link = temp;

}else{

temp->link = NULL; *ptr = temp

}}

Page 14: Fall 2005 Data Structure Linked List – Single Linked List

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

Page 15: Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 W.F. [email protected] 15

Insert at End

2020

ptr1010

3030

temp

NULL

NULL

nodevoid insert (…, …) { …

if(*ptr){ temp->link = NULL; node->link = temp; } …}

Page 16: Fall 2005 Data Structure Linked List – Single Linked List

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

Page 17: Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 W.F. [email protected] 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 del

ete trail : points to the predecessor

2020 3030 NULLNULL

ptr1010

nodetrail

Page 18: Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 W.F. [email protected] 18

Delete node (2/2)

There are only two case: Delete the first node Delete any other node

Page 19: Fall 2005 Data Structure Linked List – Single Linked List

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

Page 20: Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 W.F. [email protected] 20

Delete First Node (1/2)

2020 3030 NULLNULL

ptr1010

trail

node

NULLNULL

Free (node);

Page 21: Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 W.F. [email protected] 21

Delete First Node (2/2)

ptr

1010

trail NULLNULL

2020 3030 NULLNULL

node

void delete(list_pointer *ptr, list_pointer trail, list_pointer node)

{

if (trail)

trail->link = node->link;

else

*ptr = (*ptr)->link; free(node); }

Page 22: Fall 2005 Data Structure Linked List – Single Linked List

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

Page 23: Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 W.F. [email protected] 23

General Delete Node (1/4)

Case I : delete node between first node and last node.

2020 3030 NULLNULL

ptr1010

trail node

Page 24: Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 W.F. [email protected] 24

General Delete Node (2/3)

ptr

1010

trail

2020 3030 NULLNULL

node

void delete(list_pointer *ptr, list_pointer trail, list_pointer node)

{

if (trail)

trail->link = node->link; else

*ptr = (*ptr)->link; free(node); }

Page 25: Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 W.F. [email protected] 25

General Delete Node (3/4)

Case II : delete last node

2020 3030 NULLNULL

ptr1010

nodetrail

NULL

Page 26: Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 W.F. [email protected] 26

General Delete Node (4/4)

ptr

1010

trail

2020 3030 NULLNULL

node

void delete(list_pointer *ptr, list_pointer trail, list_pointer node)

{

if (trail)

trail->link = NULL; else

*ptr = (*ptr)->link; free(node); }

NULL

Page 27: Fall 2005 Data Structure Linked List – Single Linked List

Fall 2005 W.F. [email protected] 27

Q & A