Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.

Similar presentations


Presentation on theme: "Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list."— Presentation transcript:

1 Linked List Lesson xx In this presentation, we introduce you to the basic elements of a linked list.

2 Objectives Linked list concept Node structure
Creating a static linked list Accessing items in a linked list Our goal is to show you what a linked list looks like pictorially, introduce you to the elements of a linked list called a node. Create a linked list and show you how to access items in a linked list.

3 Illustration of a Linked List
Head a b c d e A linked list consists of a series of records that are joined together using a pointer. Each of the records are called nodes of a linked list. You can see that record a is connected to record b which is connected to record c and so forth. Linked list are used because it is easy to insert and delete items compared to an array. Arrays are stored sequentially in memory, records, on the other hand, may be store anywhere in memory. They are joined together using pointers to make the list seem sequential.

4 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; } Here is a program that creates a basic linked list and shows how to access information in the nodes. We will go through the entire program in the next series of slides.

5 Node Structure struct entry { int value; entry* next; }; .value .next
Here is the code to set up the structure of a node in the linked list. You set up a structure that contain data members. The last item is a pointer to a structure that looks like itself. In our specific case, we have one integer data member called value. In a different example, you might have a data member to keep track of a name, an address , a city and a state. We’ll keep our structure simple for demonstration purposes. The last line of the structure is entry * next. This is a pointer to a structure of the form entry. We have drawn a picture of the node for you. The first part is called .value and contains an int, the 2nd part is called .next and is a pointer to a structure of the form entry.

6 Node Declaration entry n1, n2, n3; n1 n1.value n1.next n2 n2.value
Entry n1,n2, n3, sets up 3 nodes called n1,n2, and n3. Looking at the picture you can see that each node contains 2 parts. The parts of n1 are n1.value and n1.next. At this point in the program, the nodes contain no data.

7 Node Initialization n1.value = 100; n2.value = 200; n3.value = 300; n1
n2.next n3 n3.value n3.next 100 200 300 This portion of the program puts information into the nodes. In n1.value, we store the number 100. In n2.value we store 200 and etc. n1.value n1.next

8 Connecting Nodes n1.next = &n2; n2.next = &n3; n1 n1.value n1.next n2
100 200 300 (56c) (7fa) (88b) 7fa 88b Here is the code to connect the nodes of the linked list together. N1.next = &n2; means that n1.next should contain the address of n2. In our drawing, n1.next contains the address of n2 which is 7fa. N2.next contains the address of n3 which is 88b.

9 Different Explanation
  n1.next = &n2;   n2.next = &n3; n1 n2 n2.value n2.next n3 n3.value n3.next 100 200 300 Another way to explain the statement n1.next = &n2; is: make n1.next point to the node n2. n2.next = &n3 means make n2.next point to node n3. In C++ a pointer and an address are the same thing. n1.value n1.next

10 Print Contents of First Node
cout << n1.value << “ “ << n1.next; << endl; n1 n1.value n1.next n2 n2.value n2.next n3 n3.value n3.next 100 200 300 (56c) (7fa) (88b) 7fa 88b The statement: cout << n1.value << N1.next prints the contents of the 1st node. N1.value contains the # 100 and n1.next contains the address of n2 which is 7fa in our example.

11 Print Value of 3rd Node cout << n2.next->value << endl ; n1 n1.value n1.next n2 n2.value n2.next n3 n3.value n3.next 100 200 300 (56c) (7fa) (88b) 7fa 88b cout << n2.next->value << endl; prints the #300. Here’s why, n2.next contains 88b which is a pointer to the structure n3. Since n2.next is pointer to a structure, and the structure contains 2 parts, we have to specify which part of the structure, the value part or next part. So n2.next->value specifies the value part which contains the # 300. Of course, the simple way is: cout << n3.value; Later on we’ll see why the simple way isn’t always possible.

12 Print Value of 3rd Node Different Syntax
  cout << n1.next->next->value;   n1 n1.value n1.next n2 n2.value n2.next n3 n3.value n3.next 100 200 300 (56c) (7fa) (88b) 7fa 88b cout << n1.next->next->value also prints the #300. Here’s why, n1.next contains 7fa which is a pointer to the structure n2. N1.next->next contains 88b which is another pointer to a structure and is pointing to n3. The value member of n3 contains the # 300.

13 Summary Linked list concept Node structure
Creating a static linked list Accessing items in a linked list We have written code that demonstrates how to build a linked list. In that program we learned how to create a node using a structure and we learned how to access the various parts of the linked list using different terminology. In the next module, we’ll learn more about linked list and how to manipulate them.


Download ppt "Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list."

Similar presentations


Ads by Google