570 likes | 585 Views
Learn about nodes and linked lists, why they are needed, and how to create a linked list in C. Includes operations like insert, search, and remove. Practical examples and node struct definition provided.
E N D
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
Node and Linked list value next struct node{ int value; node * next;}; node An incomplete linked list value value value next next next
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 ?
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 value next struct node{ int value; node * next;}; node value value value next next next NULL
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
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
Node and Linked list value next struct node{ int value; node * next;}; node head value value value next next next NULL
Node and Linked list value next struct node{ int value; node * next;}; node head value value value next next next 303 202 101 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 next ID next ID next GPA GPA GPA 2002 2.5 1001 3.5 3003 3.3 NULL
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 struct node{int value; node * next;}; 1. Create a head
How to Create a Linked List struct node{int value; node * next;}; 1. Create a head node * head = NULL; head NULL
How to Create a Linked List struct node{int value; node * next;}; 2. Create an instance of node head NULL
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
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
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
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
How to Create a Linked List struct node{int value; node * next;}; 3. Insert the node to the list head value next 303 NULL
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
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
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
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
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
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
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
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
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
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
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
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
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
Node Access head->value = 404; head value value value next next next 303 202 101 NULL
Node Access head value value value next next next 303 404 101 NULL
Node Access cout << head->next->value; //What will be displayed head value value value next next next 303 404 101 NULL
Node Access head->next->next->value=505; head value value value next next next 303 404 101 NULL
Node Access head value value value next next next 505 404 101 NULL
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
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
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
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
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
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
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