1 / 57

C201: Linked List

C201: Linked List. Learning Objectives. Nodes and Linked Lists Operations on Linked List Insert Search Remove. Nodes and Linked Lists. Why do we need linked list? Linked list Simple example of "dynamic data structure" Composed of nodes

messick
Download Presentation

C201: Linked List

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 node

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

  6. Node and Linked list value next struct node{ int value; node * next;}; node We need something to indicate the end of the list value value value next next 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 value next struct node{ int value; node * next;}; node value value value next next next NULL

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

  10. Head Pointer value next 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 head node

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

  12. Node and Linked list value next struct node{ int value; node * next;}; node head value value value next next next 303 202 101 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 next ID next ID next GPA GPA GPA 2002 2.5 1001 3.5 3003 3.3 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 struct node{int value; node * next;}; 1. Create a head

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

More Related