Sequences 9/18/2018 12:20 PM 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 Each "node" is variable of struct that’s dynamically created with new Nodes also contain pointers to other nodes Provide "links"
Node Definition struct node { int value; node * next; }; value next
Node and Linked list struct node { int value; node * next; }; An incomplete linked list value next value next value next
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 ?
End Marker Use NULL for next pointer (if next==NULL) Indicates no further "links" after this node Provide end marker for linked list
Node and Linked list struct node { int value; node * next; }; value NULL
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
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
Node and Linked list struct node { int value; node * next; }; value head value next value next value next NULL
Node and Linked list struct node { int value; node * next; }; value head value next value next value next 202 101 303 NULL
Data Fields in Node struct student { int ID; double GPA; student * next; };
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
How to Create a Linked List
How to Create a Linked List 0. Define the node
How to Create a Linked List 0. Define the node struct node { int value; node * next; };
How to Create a Linked List 1. Create a head struct node { int value; node * next; };
How to Create a Linked List 1. Create a head node * head = NULL; struct node { int value; node * next; }; head NULL
How to Create a Linked List 2. Create an instance of node struct node { int value; node * next; }; head NULL
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
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
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
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
How to Create a Linked List 3. Insert the node to the list struct node { int value; node * next; }; head value next 303 NULL
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
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
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
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
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
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
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
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
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
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
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
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
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
Node Access head->value = 404; 202 101 303 NULL head value next
Node Access head value next value next value next 404 101 303 NULL
Node Access cout << head->next->value; //What will be displayed head value next value next value next 404 101 303 NULL
Node Access head->next->next->value=505; 404 101 303 NULL
Node Access head value next value next value next 404 101 505 NULL
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
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
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
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
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
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
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
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
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
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
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
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
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
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