Presentation is loading. Please wait.

Presentation is loading. Please wait.

Traversing a Linked List

Similar presentations


Presentation on theme: "Traversing a Linked List"— Presentation transcript:

1 Traversing a Linked List
Linked Lists Introduction Building a Linked List Traversing a Linked List Deleting a Node Dr. Hyrum D. Carroll (with some material from Dale & Weems’s Programming and Problem Solving with C++ slides)

2 Declarations for a Dynamic Linked List
// Type declarations struct NodeType { char info; NodeType* link; }; // Variable DECLARATIONS NodeType* head; NodeType* ptr; . info link ‘A’ x600

3 ptr is a pointer to a node
. info link ‘A’ x600 ptr

4 *ptr is the entire node pointed to by ptr
‘A’ 0x600 . info link *ptr

5 ptr->info is a node member
. info link ‘A’ 0x600 ptr->info or (*ptr).info // Equivalent

6 ptr->link is a node member
. info link ‘A’ 0x600 ptr->link or (*ptr).link // Equivalent

7 Pointer Dereferencing and Member Selection
ptr . info link ‘A’ x600 ptr ptr . info link ‘A’ x600 *ptr ptr . info link (*ptr).info or ptr->info ‘A’ x600

8 Building a Linked List

9 Building a Linked List newNodePtr head newNodePtr = new NodeType;
newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

10 Building a Linked List newNodePtr 0x200 head
newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

11 Building a Linked List newNodePtr 0x200 head ‘L’
newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

12 Building a Linked List newNodePtr 0x200 head ‘L’ NULL
newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

13 Building a Linked List newNodePtr 0x200 head ‘L’ NULL
newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

14 Building a Linked List newNodePtr 0x500 0x200 head ‘L’ NULL
newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

15 Building a Linked List newNodePtr 0x500 0x200 head ‘I’ ‘L’ NULL
newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

16 Building a Linked List newNodePtr 0x500 0x200 head ‘I’ 0x200 ‘L’ NULL
newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

17 Building a Linked List newNodePtr 0x500 0x200 head ‘I’ 0x200 ‘L’ NULL
newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

18 Building a Linked List ‘I’ 0x200 ‘L’ NULL newNodePtr 0x300 0x500 0x200
head newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

19 Building a Linked List ‘T’ ‘I’ 0x200 ‘L’ NULL newNodePtr
0x x x200 head newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

20 Building a Linked List ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL newNodePtr
0x x x200 head newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

21 Building a Linked List ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL newNodePtr
0x x x200 head newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;

22 Traversing a Linked List

23 Traversing a Linked List
ptr ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x x x200 head 0x300 ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

24 Traversing a Linked List
ptr 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x x x200 head 0x300 ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

25 Traversing a Linked List
ptr 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x x x200 head 0x300 ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

26 Traversing a Linked List
ptr 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x x x200 head 0x300 Output: ‘T’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

27 Traversing a Linked List
ptr 0x500 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x x x200 head 0x300 Output: ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

28 Traversing a Linked List
ptr 0x500 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x x x200 head 0x300 Output: ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

29 Traversing a Linked List
ptr 0x500 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x x x200 head 0x300 Output: ‘I’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

30 Traversing a Linked List
ptr 0x200 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x x x200 head 0x300 Output: ‘I’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

31 Traversing a Linked List
ptr 0x200 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x x x200 head 0x300 Output: ‘I’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

32 Traversing a Linked List
ptr 0x200 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x x x200 head 0x300 Output: ‘L’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

33 Traversing a Linked List
ptr NULL ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x x x200 head 0x300 Output: ‘L’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

34 Traversing a Linked List
ptr NULL ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x x x200 head 0x300 Output: ‘L’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

35 Traversing a Linked List
ptr NULL ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x x x200 head 0x300 Output: ‘L’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }

36 Deleting a Node

37 Deleting Node ’I’ prev curr ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL
0x x x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

38 Deleting Node ’I’ prev curr NULL 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL
0x x x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

39 Deleting Node ’I’ prev curr NULL 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL
0x x x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

40 Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL
0x x x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

41 Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL
0x x x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

42 Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL
0x x x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

43 Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL
0x x x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

44 Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x200 ‘I’ 0x200 ‘L’ NULL
0x x x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

45 Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x200 ? ? ‘L’ NULL
0x x x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

46 Deleting Node ’I’ prev curr 0x300 NULL 0x300 0x200 head
‘T’ 0x ‘L’ NULL 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

47 Deleting Node ’I’ prev curr 0x300 NULL 0x300 0x200 head
‘T’ 0x ‘L’ NULL 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer

48 Deleting Node ’I’ prev curr 0x300 NULL ‘T’ 0x200 ‘L’ NULL 0x300 0x200
head 0x x ‘L’ NULL // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer


Download ppt "Traversing a Linked List"

Similar presentations


Ads by Google