Fall 2005 Data Structure Linked List Single Linked
- Slides: 27
Fall 2005 Data Structure Linked List – Single Linked List Fall 2005 W. F. Hu@ant. comm. ccu
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. Hu@ant. comm. ccu 2
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. Hu@ant. comm. ccu 3
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. Hu@ant. comm. ccu 4
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. Hu@ant. comm. ccu 5
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. Hu@ant. comm. ccu ptr NULL 6
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. Hu@ant. comm. ccu 7
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. Hu@ant. comm. ccu 8
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. Hu@ant. comm. ccu 9
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. Hu@ant. comm. ccu 10
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. Hu@ant. comm. ccu 11
Insert in Middle (1/2) ptr 10 30 NULL Insert 20 between 10 and 30 20 temp Fall 2005 W. F. Hu@ant. comm. ccu 12
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. Hu@ant. comm. ccu 13
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. Hu@ant. comm. ccu 14
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. Hu@ant. comm. ccu 15
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. Hu@ant. comm. ccu 16
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. Hu@ant. comm. ccu 30 NULL 17
Delete node (2/2) o There are only two case: n n Fall 2005 Delete the first node Delete any other node W. F. Hu@ant. comm. ccu 18
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. Hu@ant. comm. ccu 19
Delete First Node (1/2) trail NULL node ptr Free 10(node); Fall 2005 20 W. F. Hu@ant. comm. ccu 30 NULL 20
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. Hu@ant. comm. ccu 21
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. Hu@ant. comm. ccu 22
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. Hu@ant. comm. ccu 30 NULL 23
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. Hu@ant. comm. ccu 24
General Delete Node (3/4) o Case II : delete last node trail node ptr 10 Fall 2005 20 NULL W. F. Hu@ant. comm. ccu 30 NULL 25
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. Hu@ant. comm. ccu 26
Q & A Fall 2005 W. F. Hu@ant. comm. ccu 27
- Pengertian single linked list
- In linked list the successive elements
- Introduction to linked list
- Singly linked list in data structure
- Polynomial addition in data structure
- Polynomial addition using linked list in c
- Circular single linked list adalah
- Single linked list non circular
- Algorithm for singly linked list
- Sisd processor
- Dataxin
- Linked data structure
- Static data structures
- Generic linked list java
- Jenis linked list
- Singly vs doubly linked list
- Tan is a lover of linked list
- Xor linked list c
- Traverse a linked list c++
- Linked list in mips
- Linked list definition
- Linked list diagram in java
- Advantages and disadvantages of circular linked list
- C linked list malloc
- Concurrent linked list
- Adtlist
- Difference between linked list and queue
- Circular linked list advantages