Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists (continued)

Similar presentations


Presentation on theme: "Linked Lists (continued)"— Presentation transcript:

1 Linked Lists (continued)
October 11, 2017 Hassan Khosravi / Geoffrey Tien

2 Example Linked list for hockey players Recall: nodes defined as structures with attributes, and a pointer to the next node struct Player { int jersey_number; char* name; struct Player* next; }; typedef struct Player Player; int main() { Player* head_list1 = NULL; Player* head_list2 = NULL; Player gretzky = {99, "Wayne Gretzky", NULL}; // example 1 head_list1 = &gretzky; displayList(head_list1); // example 2 head_list2 = insertAtHead(head_list2, 22, "Daniel Sedin"); 33, "Henrik Sedin"); displayList(head_list2); } Note that head_list1 only needs a pointer type, so syntactically we could assign it a pointer to a local Player variable October 11, 2017 Hassan Khosravi / Geoffrey Tien

3 Hockey example continued
insertAtHead insertAtHead should insert a new node at the head of a (possibly empty) list, and return the node as the new head will this code work? Why or why not? Player* insertAtHead(Player* head, int player_number, char* player_name) { Player new_node; new_node->jersey_number = player_number; new_node->name = player_name; new_node->next = head; // point to current head of list printf("Node was added.\n\n"); return &new_node; // the new node becomes the new head of list } October 11, 2017 Hassan Khosravi / Geoffrey Tien

4 Hockey example continued
insertAtHead insertAtHead should insert a new node at the head of a (possibly empty) list, and return the node as the new head is this better? Player* insertAtHead(Player* head, int player_number, char* player_name) { Player* new_node; new_node = (Player*) malloc(sizeof(Player)); new_node->jersey_number = player_number; new_node->name = player_name; new_node->next = head; // point to current head of list printf("Node was added.\n\n"); return new_node; // the new node becomes the new head of list } October 11, 2017 Hassan Khosravi / Geoffrey Tien

5 Hockey example continued
A function to iterate through a (possibly empty) linked list, starting from the head of the list, and printing out information from each node void displayList(Player* node) { int k = 0; while (node) { // loop breaks when node becomes NULL printf("Node %d is: %s, Jersey number %d\n", k, node->name, node->jersey_number); node = node->next; k++; } printf("There are %d node(s) in the list.\n\n", k); See hockey_players_linked_list_V1.c October 11, 2017 Hassan Khosravi / Geoffrey Tien

6 Hassan Khosravi / Geoffrey Tien
Quick exercise Inserting into a list Consider the following linked list, and possible commands in the box W: current->next = new; X: current = current->next; Y: new->next = current->next; Z: current = new; Assuming we wish to keep the list ordered, which of the command sequences correctly inserts the new node into the list? X X X Y W X X X X W Y X X X W Y X X X W Z Y None of the above 1 2 3 4 6 7 current 5 new October 11, 2017 Hassan Khosravi / Geoffrey Tien

7 Hassan Khosravi / Geoffrey Tien
Quick exercise Removing from a list Consider the following linked list, and possible commands in the box V: current = current->next; W: prev = prev->next; X: prev->next = current->next; Y: current->next = prev->next; Z: free(current); current = NULL; Which of the command sequences correctly removes the node containing "3" from the list? V W V Y Z W V W X Z V W V X Z V V W W Y Z None of the above 1 2 3 4 6 7 prev current October 11, 2017 Hassan Khosravi / Geoffrey Tien

8 Comparison of worst case complexities
Assume we know the number of entries in the arrays also assume current position (in lists) is known Operation Array (unordered) Array (ordered) Linked list (unordered) Linked list (ordered Insert at front Insert at back, using head ptr Insert after current position Search for a value Remove at current position October 11, 2017 Hassan Khosravi / Geoffrey Tien

9 Hassan Khosravi / Geoffrey Tien
Extra pointers Notice how the operations at the end of the list have (relatively) poor complexity, involving a complete traversal of the list we can give ourselves a tail pointer, for very little additional overhead head 1 2 3 4 6 7 tail 9 With a tail pointer, what is the complexity inserting into an ordered list, if the item is known to be larger than the largest so far? What if the item is not necessarily larger than the largest item? new October 11, 2017 Hassan Khosravi / Geoffrey Tien

10 Readings for this lesson
Thareja Chapter 6 Next class: other flavours of linked lists October 11, 2017 Hassan Khosravi / Geoffrey Tien


Download ppt "Linked Lists (continued)"

Similar presentations


Ads by Google