Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sequences 9/18/2018 12:20 PM C201: Linked List.

Similar presentations


Presentation on theme: "Sequences 9/18/2018 12:20 PM C201: Linked List."— Presentation transcript:

1 Sequences 9/18/ :20 PM C201: Linked List

2 Learning Objectives Nodes and Linked Lists Operations on Linked List
Insert Search Remove

3 Nodes and Linked Lists Why do we need linked list? Linked list
Simple example of "dynamic data structure" Composed of nodes Each "node" is variable of struct that’s dynamically created with new Nodes also contain pointers to other nodes Provide "links"

4 Node Definition struct node { int value; node * next; }; value next

5 Node and Linked list struct node { int value; node * next; };
An incomplete linked list value next value next value next

6 Node and Linked list struct node { int value; node * next; }; ?
We need something to indicate the end of the list value next value next value next ?

7 End Marker Use NULL for next pointer
(if next==NULL) Indicates no further "links" after this node Provide end marker for linked list

8 Node and Linked list struct node { int value; node * next; }; value
NULL

9 Node and Linked list struct node { int value; node * next; }; ?
We still need a handle to access the nodes the list ? value next value next value next NULL

10 Head Pointer Node * head; A simple pointer to Node
Set to point to 1st node in list head is used to "maintain" start of list Also used as argument to functions value next head node

11 Node and Linked list struct node { int value; node * next; }; value
head value next value next value next NULL

12 Node and Linked list struct node { int value; node * next; }; value
head value next value next value next 202 101 303 NULL

13 Data Fields in Node struct student { int ID;
double GPA; student * next; };

14 Data Fields in Node struct student { int ID;
double GPA; student * next; }; head ID GPA next ID GPA next ID GPA next 1001 3.5 3003 3.3 2002 2.5 NULL

15 How to Create a Linked List

16 How to Create a Linked List
0. Define the node

17 How to Create a Linked List
0. Define the node struct node { int value; node * next; };

18 How to Create a Linked List
1. Create a head struct node { int value; node * next; };

19 How to Create a Linked List
1. Create a head node * head = NULL; struct node { int value; node * next; }; head NULL

20 How to Create a Linked List
2. Create an instance of node struct node { int value; node * next; }; head NULL

21 How to Create a Linked List
2. Create an instance of node node * n= new node; struct node { int value; node * next; }; n head value next NULL

22 How to Create a Linked List
2. Dynamically create an instance of node n->value = 303; n->next = NULL; struct node { int value; node * next; }; n head value next 303 NULL NULL

23 How to Create a Linked List
3. Insert the node to (the head of) the list n->next = head; head = n; struct node { int value; node * next; }; n head value next 303 NULL NULL

24 How to Create a Linked List
3. Insert the node to the list struct node { int value; node * next; }; n head value next 303 NULL

25 How to Create a Linked List
3. Insert the node to the list struct node { int value; node * next; }; head value next 303 NULL

26 How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes struct node { int value; node * next; }; head value next 303 NULL

27 How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes node * n= new node; n->value = 101; n->next = NULL; struct node { int value; node * next; }; n value next head 101 NULL value next 303 NULL

28 How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes n->next = head; head = n; struct node { int value; node * next; }; n value next head 101 NULL value next 303 NULL

29 How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes n->next = head; head = n; struct node { int value; node * next; }; n value next head 101 value next 303 NULL

30 How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes n->next = head; head = n; struct node { int value; node * next; }; n value next head 101 value next 303 NULL

31 How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes struct node { int value; node * next; }; value next head 101 value next 303 NULL

32 How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes struct node { int value; node * next; }; head value next value next 101 303 NULL

33 How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes node * n= new node; n->value = 101; n->next = NULL; struct node { int value; node * next; }; n value next 101 NULL head value next value next 101 303 NULL

34 How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes n->next = head; head = n; ; struct node { int value; node * next; }; n value next 101 NULL head value next value next 101 303 NULL

35 How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes n->next = head; head = n; struct node { int value; node * next; }; n value next 101 head value next value next 101 303 NULL

36 How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes n->next = head; head = n; struct node { int value; node * next; }; n value next 101 head value next value next 101 303 NULL

37 How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes struct node { int value; node * next; }; value next 101 head value next value next 101 303 NULL

38 How to Create a Linked List
5. A linked list is created struct node { int value; node * next; }; head value next value next value next 101 101 303 NULL

39 Node Access head->value = 404; 202 101 303 NULL head value next

40 Node Access head value next value next value next 404 101 303 NULL

41 Node Access cout << head->next->value;
//What will be displayed head value next value next value next 404 101 303 NULL

42 Node Access head->next->next->value=505; 404 101 303 NULL

43 Node Access head value next value next value next 404 101 505 NULL

44 Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL

45 Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp

46 Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp

47 Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp

48 Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp

49 Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp

50 Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp

51 Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp

52 Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp

53 Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp

54 Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp

55 Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp

56 Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } //finished accessing the node head value next value next value next 404 101 505 NULL temp

57 Linked List Functions Insert a node Search a node Remove a node
Unordered Ordered Search a node Remove a node Sort a list Not covered in C201


Download ppt "Sequences 9/18/2018 12:20 PM C201: Linked List."

Similar presentations


Ads by Google