Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Linked Lists. Linked Lists / Slide 2 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert, find, delete,

Similar presentations


Presentation on theme: "1 Linked Lists. Linked Lists / Slide 2 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert, find, delete,"— Presentation transcript:

1 1 Linked Lists

2 Linked Lists / Slide 2 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert, find, delete, print, etc. * Variations of linked lists n Circular linked lists n Doubly linked lists

3 Linked Lists / Slide 3 3 Linked Lists * A linked list is a series of connected nodes * Each node contains at least n A piece of data (any type) n Pointer to the next node in the list * Head: pointer to the first node The last node points to NULL A Head BCA datapointer node

4 Linked Lists / Slide 4 4 A Simple Linked List Class * Linked lists are made up of Nodes Declare Node class for the nodes data : double -type data in this example next : a pointer to the next node in the list * struct node { * int data; * struct node *next; /* pointer to next element in list */ * } LLIST;

5 Linked Lists / Slide 5 5 Operations on Linked List * LLIST *list_add(LLIST **p, int i); * void list_remove(LLIST **p); * LLIST **list_search(LLIST **n, int i); * void list_print(LLIST *n); *

6 Linked Lists / Slide 6 6 Inserting a new node Insert a node with data equal to x after the index th elements. (i.e., when index = 0, insert the node as the first element; when index = 1, insert the node after the first element, and so on) n If the insertion is successful, return the inserted node. Otherwise, return NULL. (If index is length of the list, the insertion will fail.) * Steps 1. Locate index th element 2. Allocate memory for the new node 3. Point the new node to its successor 4. Point the new nodes predecessor to the new node newNode indexth element

7 Linked Lists / Slide 7 7 Inserting a new node Possible cases of InsertNode 1. Insert into an empty list 2. Insert in front 3. Insert at back 4. Insert in middle * But, in fact, only need to handle two cases n Insert as the first node (Case 1 and Case 2) n Insert in the middle or at the end of the list (Case 3 and Case 4)

8 Linked Lists / Slide 8 8 * LLIST *list_add(LLIST **p, int i) * { * if (p == NULL) * return NULL; * * LLIST *n = (LLIST *) malloc(sizeof(LLIST)); * if (n == NULL) * return NULL; * * n->next = *p; /* the previous element (*p) now becomes the "next" element */ * *p = n; /* add new empty element to the front (head) of the list */ * n->data = i; * * return *p; * } *

9 Linked Lists / Slide 9 9 Finding a node Search for a node with the value equal to x in the list. n If such a node is found, return its position. Otherwise, return 0. * LLIST **list_search(LLIST **n, int i) * { * if (n == NULL) * return NULL; * * while (*n != NULL) * { * if ((*n)->data == i) * { * return n; * } * n = &(*n)->next; * } * return NULL; * } *

10 Linked Lists / Slide Deleting a node * int DeleteNode(double x) n Delete a node with the value equal to x from the list. n If such a node is found, return its position. Otherwise, return 0. * Steps n Find the desirable node (similar to FindNode ) n Release the memory occupied by the found node n Set the pointer of the predecessor of the found node to the successor of the found node * Like InsertNode, there are two special cases n Delete first node n Delete the node in middle or at the end of the list

11 Linked Lists / Slide * void list_remove(LLIST **p) /* remove head */ * { * if (p != NULL && *p != NULL) * { * LLIST *n = *p; * *p = (*p)->next; * free(n); * } *

12 Linked Lists / Slide Printing all the elements * void List_Print(void) n Print the data of all the elements n Print the number of the nodes in the list

13 Linked Lists / Slide * void list_print(LLIST *n) * { * if (n == NULL) * { * printf("list is empty\n"); * } * while (n != NULL) * { * printf("print %p %p %d\n", n, n->next, n->data); * n = n->next; * }

14 Linked Lists / Slide Using all these routines * * int main(void) * { * LLIST *n = NULL; * * list_add(&n, 0); /* list: 0 */ * list_add(&n, 1); /* list: 1 0 */ * list_add(&n, 2); /* list: */ * list_add(&n, 3); /* list: */ * list_add(&n, 4); /* list: */ * list_print(n); * list_remove(&n); /* remove first (4) */ * list_remove(&n->next); /* remove new second (2) */ * list_remove(list_search(&n, 1)); /* remove cell containing 1 (first) */ * list_remove(&n->next); /* remove second to last node (0) */ * list_remove(&n); /* remove last (3) */ * list_print(n); * * return 0; * }

15 Linked Lists / Slide Variations of Linked Lists * Circular linked lists n The last node points to the first node of the list n How do we know when we have finished traversing the list? (Tip: check if the pointer of the current node is equal to the head.) A Head BC

16 Linked Lists / Slide Variations of Linked Lists * Doubly linked lists n Each node points to not only successor but the predecessor There are two NULL: at the first and last nodes in the list n Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards A Head B C

17 Linked Lists / Slide Array versus Linked Lists * Linked lists are more complex to code and manage than arrays, but they have some distinct advantages. n Dynamic: a linked list can easily grow and shrink in size. We dont need to know how many nodes will be in the list. They are created in memory as needed. In contrast, the size of a C++ array is fixed at compilation time. n Easy and fast insertions and deletions To insert or delete an element in an array, we need to copy to temporary variables to make room for new elements or close the gap caused by deleted elements. With a linked list, no need to move other nodes. Only need to reset some pointers.


Download ppt "1 Linked Lists. Linked Lists / Slide 2 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert, find, delete,"

Similar presentations


Ads by Google