1 / 69

Linked Lists

Linked Lists. Linked List. Group of nodes connected by pointers A node consists of Data Pointer to next node. 6. 5. 3. 8. Head. Null. Insertion into a Linked List. Insert 9 after 5. 6. 5. 3. 8. Head. Null. 9. Deletion from a Linked List. Delete 3 from the list.

rholmes
Download Presentation

Linked Lists

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. Linked Lists

  2. Linked List • Group of nodes connected by pointers • A node consists of • Data • Pointer to next node 6 5 3 8 Head Null

  3. Insertion into a Linked List • Insert 9 after 5 6 5 3 8 Head Null 9

  4. Deletion from a Linked List • Delete 3 from the list Free this space 6 5 3 8 Head Null 9

  5. Declaration of a node struct node { int info; struct node *next; }; typedef struct node node; 6 5 3 8 Head Null

  6. Linked List Structure 6 5 3 8 Head Null Some address In memory 6 5 3 8 Head Points back to Internal node

  7. Dynamic allocation of a node node *ptr; ptr = (node *)malloc(sizeof(node)); ptr ? free(ptr)

  8. Dynamic allocation of a node node *ptr; ptr = (node *)malloc(sizeof(node)); ptr = (node *)malloc(sizeof(node)); ptr = (node *)malloc(sizeof(node)); free(ptr); Memory Problems ? ? ? ptr

  9. Dynamic allocation of a node Fox01>valgrind ./memoryleakexample ==19325== HEAP SUMMARY: ==19325== in use at exit: 32 bytes in 2 blocks ==19325== total heap usage: 3 allocs, 1 frees, 48 bytes allocated ==19325== ==19325== LEAK SUMMARY: ==19325== definitely lost: 32 bytes in 2 blocks ==19325== indirectly lost: 0 bytes in 0 blocks ==19325== possibly lost: 0 bytes in 0 blocks ==19325== still reachable: 0 bytes in 0 blocks ==19325== suppressed: 0 bytes in 0 blocks ==19325== Rerun with --leak-check=full to see details of leaked memory ==19325== ==19325== For counts of detected and suppressed errors, rerun with: -v ==19325== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

  10. Dynamic allocation of a node node *ptr, *ptr2, *ptr3; ptr = (node *)malloc(sizeof(node)); ptr2 = (node *)malloc(sizeof(node)); ptr3 = (node *)malloc(sizeof(node)); ? ptr ? ptr2 ? ptr3

  11. Dynamic allocation of a node ptr -> info = 3; ptr2 -> info = 5; ptr3 -> info = 7; 3 ptr 5 ptr2 7 ptr3

  12. Dynamic allocation of a node ptr -> next = ptr2; ptr2 -> next = ptr3; ptr3 -> next = NULL; 3 ptr 5 ptr2 7 NULL ptr3

  13. Dynamic allocation of a node node *ptr; ptr = (node *)malloc(sizeof(node)); ptr-> next = (node *)malloc(sizeof(node)); ptr-> next ->next = (node *)malloc(sizeof(node)); ptr -> next -> next ->next = NULL; ? ptr ? ? NULL

  14. Allocate a new node Insert new element Make new node point to old head Update head to point to new node Inserting at the Head 6 5 3 8 Head Null 6 5 3 8 2 Head Null

  15. void inserthead(node *head, int a) { node *ptr; ptr->info = a; ptr->next = head; head = ptr; } Inserting at the Head inserthead(head,2); Memory Problems 6 5 3 8 Null Head 6 5 3 8 2 Head Null

  16. void inserthead(node *head, int a) { node *ptr; ptr = (node*)malloc(sizeof(node)); ptr->info = a; ptr->next = head; head = ptr; } Inserting at the Head inserthead(head,2); Can not modify head 6 5 3 8 Null Head 6 5 3 8 2 Head Null

  17. node *inserthead(node *head, int a) { node *ptr; ptr = (node*)malloc(sizeof(node)); ptr->info = a; ptr->next = head; return(ptr); } Inserting at the Head head = inserthead(head,2); 6 5 3 8 Null Head 6 5 3 8 2 Head Null

  18. Removing at the Head • Update head to point to next node in the list • Allow garbage collector to reclaim the former first node 6 5 3 8 Head Null 5 3 8 Null Head

  19. Removing at the Head void deletehead(node *head) { head = head->next; return; } deletehead(head); Memory Leak 6 5 3 8 Null Head 5 3 8 Null Head

  20. Removing at the Head void deletehead(node *head) { node * ptr; ptr = head; head = head->next; free(ptr); return; } deletehead(head); Can not modify head 6 5 3 8 Null Head 5 3 8 Null Head

  21. Removing at the Head node* deletehead(node *head) { node * ptr; ptr = head; head = head->next; free(ptr); return(head); } head = deletehead(head); 6 5 3 8 Null Head 5 3 8 Null Head

  22. Inserting at the Tail • Allocate a new node • Insert new element • Have new node point to null • Have old last node point to new node 6 5 3 8 Null Head 6 5 3 8 1 Null Head

  23. Inserting at the Tail node *inserttail(node *head, int a) { node *ptr; node *ptr2 = head; ptr = (node*)malloc(sizeof(node)); ptr->info = a; ptr->next = NULL; if (head == NULL) return (ptr); else if (head->next == NULL) { head->next = ptr; return (head); } while (head->next != NULL) head = head->next; head->next = ptr; return(ptr2); } 6 5 3 8 Null Head

  24. Print a linked list void printlist (node *head) { while (head !=NULL) { printf("%d ",head->info); head = head->next; } printf("\n"); } printlist(head); 6 5 3 8 Head Null

  25. Find length of a linked list int length(node *head) { int len = 0; while (head != NULL) { head = head->next; len = len + 1; } return(len); } x=length(head); 6 5 3 8 Head Null

  26. Free a linked list void freelist(node *head) { node *ptr=head; while(head != NULL) { head = head->next; free(ptr); ptr = head; } } freelist(head); 6 5 3 8 Head Null

  27. Find the sum of elements in a linked list int sum(node *head) { int total = 0; while (head !=NULL) { total = total + head-> info; head = head->next; } return (total); } 6 5 3 8 Head Null

  28. Find the last element in a linked list int last(node *head) { if (head == NULL) return (-1); else if (head->next == NULL) return(head-> info); while (head->next != NULL) head = head -> next; return (head->info); } 6 5 3 8 Head Null

  29. Find if an element appears in a linked list int exists(node *head, int x) { if (head == NULL) return (0); while (head != NULL) { if (head->info == x) return (1); head = head -> next; } return (0); } 6 5 3 8 Head Null

  30. Find if a linked list is sorted in increasing order int sorted(node *head) { if (head == NULL) return (1); else if (head->next == NULL) return (1); while (head->next != NULL) { if (head->info > head->next->info) return (0); head = head -> next; } return (1); } 6 5 3 8 Head Null

  31. Review of Recursion

  32. Recursive Functions • A function that invokes itself is a recursive function. long fact(int k) { if (k == 0) return 1; else return k*fact(k-1) } k!=k*(k-1)! Available on class webpage as lecture3e6.c

  33. Recursive Factorial Function fact(4) 24 4*fact(3) 6 3*fact(2) 2 2*fact(1) 1 1*fact(0)

  34. Fibonacci Numbers • Sequence {f0,f1,f2,…}. First two values (f0,f1) are 1, each succeeding number is the sum of previous two numbers. • 1 1 2 3 5 8 13 21 34 • F(0)=1, F(1) = 1 • F(i) = F(i-1)+F(i-2)

  35. Fibonacci Numbers int fibonacci(int k) { int term; term = 1; if (k>1) term = fibonacci(k-1)+fibonacci(k-2); return term; } Available on class webpage as lecture3e7.c Iterative version available on class webpage as lecture3e8.c

  36. Recursive Fibonacci Function Fibonacci(3) 2 1 Fibonacci(2) Fibonacci(1) 1 1 Fibonacci(1) Fibonacci(0)

  37. Exercise • Write a function to compute the following Recursion Step

  38. Solution long add(int a, int b) { if (b == a) return a; else return b+add(a,b-1); }

  39. Linked Lists and Recursion

  40. Find the length of a linked list int length(node *head) { int count=0; while (head != NULL) { head = head->next; count++; } return (count); } x=length(head); 6 5 3 8 Head Null

  41. Recursive: Find the length of a linked list int length(node *head) { if (head == NULL) return 0; else return 1 + length(head->next); } x=length(head); 6 5 3 8 Head Null

  42. Find the sum of elements in a linked list int sum(node *head) { int total = 0; while (head !=NULL) { total = total + head-> info; head = head->next; } return (total); } 6 5 3 8 Head Null

  43. Recursive: Find the sum of elements in a linked list int sum2(node *head) { if (head == NULL) return (0); else return (head->info + sum2(head->next)); } 6 5 3 8 Head Null

  44. Find the last element in a linked list int last(node *head) { if (head == NULL) return (-1); else if (head->next == NULL) return(head-> info); while (head->next != NULL) head = head -> next; return (head->info); } 6 5 3 8 Head Null

  45. Recursive: Find the last element in a linked list int last2(node *head) { if (head == NULL) return (-1); else if (head->next == NULL) return(head->info); else return(last2(head->next)); } 6 5 3 8 Head Null

  46. Find if an element appears in a linked list int exists(node *head, int x) { if (head == NULL) return (0); while (head != NULL) { if (head->info == x) return (1); head = head -> next; } return (0); } 6 5 3 8 Head Null

  47. Recursive: Find if an element appears in a linked list int exists2(node *head, int x) { if (head == NULL) return (0); if (head->info == x) return 1; else return(exists2(head->next, x)); } 6 5 3 8 Head Null

  48. Find if a linked list is sorted in increasing order int sorted(node *head) { if (head == NULL) return (1); else if (head->next == NULL) return (1); while (head->next != NULL) { if (head->info > head->next->info) return (0); head = head -> next; } return (1); } 6 5 3 8 Head Null

  49. Recursive: Find if a linked list is sorted in increasing order int sorted2(node *head) { if (head == NULL) return(1); else if (head->next == NULL) return (1); else { if (head->info > head->next->info) return (0); else return(sorted2(head->next)); } } 6 5 3 8 Head Null

  50. Find maximum in a linked list int max(node *head) { int maximum; if (head == NULL) return(-1); maximum = head->info; while (head != NULL) { if (head->info > maximum) maximum = head->info; head = head->next; } return(maximum); } 6 5 3 8 Head Null

More Related