690 likes | 726 Views
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.
E N D
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 Free this space 6 5 3 8 Head Null 9
Declaration of a node struct node { int info; struct node *next; }; typedef struct node node; 6 5 3 8 Head Null
Linked List Structure 6 5 3 8 Head Null Some address In memory 6 5 3 8 Head Points back to Internal node
Dynamic allocation of a node node *ptr; ptr = (node *)malloc(sizeof(node)); ptr ? free(ptr)
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
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)
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
Dynamic allocation of a node ptr -> info = 3; ptr2 -> info = 5; ptr3 -> info = 7; 3 ptr 5 ptr2 7 ptr3
Dynamic allocation of a node ptr -> next = ptr2; ptr2 -> next = ptr3; ptr3 -> next = NULL; 3 ptr 5 ptr2 7 NULL ptr3
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Recursive Factorial Function fact(4) 24 4*fact(3) 6 3*fact(2) 2 2*fact(1) 1 1*fact(0)
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)
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
Recursive Fibonacci Function Fibonacci(3) 2 1 Fibonacci(2) Fibonacci(1) 1 1 Fibonacci(1) Fibonacci(0)
Exercise • Write a function to compute the following Recursion Step
Solution long add(int a, int b) { if (b == a) return a; else return b+add(a,b-1); }
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
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
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
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
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
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
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
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
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
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
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