130 likes | 352 Views
Linked List Lesson xx. Objectives. Linked list concept Node structure Creating a static linked list Accessing items in a linked list. Illustration of a Linked List. Head. a. b. c. d. e 0. Linked List Program. #include < iostream > using std:: cout ; using std:: endl ;
E N D
Objectives • Linked list concept • Node structure • Creating a static linked list • Accessing items in a linked list
Illustration of a Linked List Head a b c d e 0
Linked List Program #include <iostream> using std::cout; using std::endl; struct entry { int value; entry* next; }; int main() { entry n1, n2, n3; n1.value = 100; n2.value = 200; n3.value = 300; n1.next = &n2; n2.next = &n3; cout << n1.value << “ “ << n1.next << endl; cout << n1.next->value << endl; // cout <<(*n1.next).value ;cout << n1.next->next->value << endl ; cout << n2.next->value << endl ; return 0; }
Node Structure struct entry { int value; entry* next; }; .value .next
Node Declaration entry n1, n2, n3; n1 n2 n3 n1.value n2.value n3.value n1.next n2.next n3.next
Node Initialization n1.value = 100; n2.value = 200; n3.value = 300; n2 n3 n1 200 300 100 n2.value n3.value n2.next n3.next n1.value n1.next
Connecting Nodes n1.next = &n2; n2.next = &n3; n2 n3 (7fa) (88b) n1 (56c) 88b 200 300 100 7fa n2.value n3.value n2.next n3.next n1.value n1.next
Different Explanation n1.next = &n2; n2.next = &n3; n2 n3 n1 200 300 100 n2.value n3.value n2.next n3.next n1.value n1.next
Print Contents of First Node cout << n1.value << “ “ << n1.next; << endl; n2 n3 (7fa) (88b) n1 (56c) 88b 200 300 100 7fa n2.value n3.value n2.next n3.next n1.value n1.next
Print Value of 3rd Node cout << n2.next->value << endl ; n2 n3 (7fa) (88b) n1 (56c) 88b 200 300 100 7fa n2.value n3.value n2.next n3.next n1.value n1.next
Print Value of 3rd Node Different Syntax cout << n1.next->next->value; n2 n3 (7fa) (88b) n1 (56c) 88b 200 300 100 7fa n2.value n3.value n2.next n3.next n1.value n1.next
Summary • Linked list concept • Node structure • Creating a static linked list • Accessing items in a linked list