Télécharger la présentation

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Pointers(additional material) Department of Computer Science-BGU

2. Array of pointers • Problem : Write program that receive strings from a user and print these strings in a lexicography order. • Solution : Array of pointers to char (malloc) char * names[256] or char ** names; This is not the same declaration !! Department of Computer Science-BGU

3. Array of pointers • Declaration: char ** names; int n,i; scanf(“%d”,&n); names = (char **)malloc(n * sizeof(char *)); for(i=o; i<n; i++){ char[i] = (char*)malloc(256*sizeof(char)); } // bubble on the pointers !! Department of Computer Science-BGU

4. Complete problem • We use exactly the size for the input strings. • Assuming that the strings have at most 255 letters. • We need receive a sorted array of strings from the function. • All the inputs are from the user. Department of Computer Science-BGU

5. Complete solution char ** sorted_list(){ char ** names, temp[256], * temp2; int n, i, flag; scanf(“%d”,&n); names = (char **)malloc(n * sizeof(char *)); for(i=0; i<n; i++){ gets(temp); char[i] = (char*)malloc(strlen(temp)+1); strcpy(char[i],temp); // char + i } /* Sorting pointers by lexicography string order */ Department of Computer Science-BGU

6. Complete solution(cont.) // sorting do{ flag =0; for(i=0 ; i< n-1 ; i++) // names + i , names +i+1 if( strcmp(names[i],names[i+1]) >0){ temp = names+i; names +i = names +i+1; names+i+1 = temp; flag = 1; } }while(flag); return names; } Department of Computer Science-BGU

7. Linked Lists Department of Computer Science-BGU

8. Problems with dynamic arrays • Problems: • Adding/ delete member. • Reallocation. • Building sort list . • Merging . • Solution: Linked list • Simple add/delete member. • No need reallocation. • Building sorting. • Simple merging. Department of Computer Science-BGU

9. Linked lists ? A better alternative might be using a linked list, by “self reference”. head NULL Structures Department of Computer Science-BGU

10. Linking students typedef struct Student_t { char ID[ID_LENGTH]; char Name[NAME_LENGTH]; int grade; struct Student_t *next; /* A pointer to the next item on the list */ } item; Department of Computer Science-BGU

11. Different definition • A different use of typedef: typedef struct Student_t item ; struct Student_t{ char ID[ID_LENGTH]; char Name[NAME_LENGTH]; int grade; item *next; /* A pointer to the next item on the list without use of “struct” in the pointer definition */ } ; Department of Computer Science-BGU

12. Creating a new kind of student • Usually when using linked lists we don’t know how many elements will be in the list • Therefore we would like to be able to dynamically allocate new elements when the need arises. • A possible implementation follows… Department of Computer Science-BGU

13. Creating a new kind of student item*create_student(char * name, char * ID, int grade) { item *std; std = (item *)malloc(item)); if (std == NULL) { printf(“Memory allocation error!\n”); exit(1); } strcpy(std->Name, name); strcpy(std->ID, ID); std->grade = grade; std->next = NULL; return std; } std NULL Department of Computer Science-BGU

14. Linked lists - insertion Previous Next Head … Insert new item: Department of Computer Science-BGU

15. Linked lists - insertion Previous Next Head … Insert new item: Department of Computer Science-BGU

16. Linked lists - insertion Previous Next Head … Insert new item: Department of Computer Science-BGU

17. Adds a new item to the end of the list head NULL newItem NULL Department of Computer Science-BGU

18. Adds a new item to the end of the list newItem NULL head Department of Computer Science-BGU

19. Adds a new item to the end of the list head NULL Department of Computer Science-BGU

20. Adds a new item to the end of the list currItem head NULL newItem NULL Department of Computer Science-BGU

21. Adds a new item to the end of the list currItem head NULL newItem NULL Department of Computer Science-BGU

22. Adds a new item to the end of the list currItem head NULL newItem NULL Department of Computer Science-BGU

23. Adds a new item to the end of the list currItem head NULL newItem NULL Department of Computer Science-BGU

24. Adds a new item to the end of the list currItem head NULL newItem NULL Department of Computer Science-BGU

25. Adds a new item to the end of the list item * add_last(item *head, item* newItem){ item *currItem; if (!head) return newItem; currItem = head; while(currItem->next) currItem = currItem->next; currItem->next = newItem; return head; } Department of Computer Science-BGU

26. Inserts into a sorted list head NULL newItem NULL Department of Computer Science-BGU

27. Inserts into a sorted list newItem NULL head Department of Computer Science-BGU

28. Inserts into a sorted list head NULL Department of Computer Science-BGU

29. Inserts into a sorted list head 8 16 28 NULL newItem 5 NULL Department of Computer Science-BGU

30. Inserts into a sorted list head 8 16 28 NULL newItem 5 NULL Department of Computer Science-BGU

31. Inserts into a sorted list head 8 16 28 NULL newItem 5 Department of Computer Science-BGU

32. Inserts into a sorted list head 8 16 28 NULL newItem 20 NULL Department of Computer Science-BGU

33. Inserts into a sorted list currItem head 8 16 28 NULL newItem 20 NULL Department of Computer Science-BGU

34. Inserts into a sorted list currItem head 8 16 28 NULL newItem 20 NULL Department of Computer Science-BGU

35. Inserts into a sorted list currItem head 8 16 28 NULL newItem 20 NULL Department of Computer Science-BGU

36. Inserts into a sorted list // while keeping it sorted ascending by key item * insert(item *head, item *newNode) { item *currItem; if (!head) return newNode; //check if newNode's key is smaller than all keys and should be first if (newNode->key < head->key) { newNode->next = head; return newNode; } currItem = head; while (currItem->next && newNode->key > currItem->next->key) currItem = currItem->next; //put newNode between currItem and currItem->next //(if currItem is last then currItem->next == NULL) newNode->next = currItem->next; currItem->next = newNode; return head; } Department of Computer Science-BGU

37. Linked lists - searching ? currItem head … Department of Computer Science-BGU

38. Linked lists - searching ? currItem head … Department of Computer Science-BGU

39. Linked lists - searching ? currItem head ! … Department of Computer Science-BGU

40. Searching for an item //searches for an item with passed key. //returns NULL if didn't find it. item *search(item *head, int key) { item *currItem = head; if (!head) return NULL; while (currItem) { //loop through the list if (currItem->key == key) return currItem; currItem = currItem->next; } //didn't find the item with the requested key return NULL; } Department of Computer Science-BGU

41. Print list’s members //prints keys of items of the list, key after key. void printKeys(item *head) { item *curr = head; while (curr) { printf("%d ", curr->key); curr = curr->next; } putchar('\n'); } Department of Computer Science-BGU

42. Linked lists - delete Head … Structure to delete Department of Computer Science-BGU

43. Linked lists - delete Previous NULL Current Head … Structure to delete Department of Computer Science-BGU

44. Linked lists - delete Previous Current Head … Structure to delete Department of Computer Science-BGU

45. Linked lists - delete Previous Current Head … Structure to delete Department of Computer Science-BGU

46. Linked lists - delete Previous Current Head … Department of Computer Science-BGU

47. Linked lists - delete Previous Current Head … Department of Computer Science-BGU

48. Linked lists - delete Previous Head … Department of Computer Science-BGU

49. Remove the item with a given value item *remove(int value, item* head){ item * curr= head,*prev=NULL; int found=0; if(!head) printf("The LL is empty\n"); else{ while(curr) if(value==curr->value){ prev ?prev->next=curr->next:head=head->next; free(curr); found=1; break; } else{ prev=curr; curr=curr->next; } if(!found) printf("The record with key %d was not found\n",value); } return head; } Department of Computer Science-BGU

50. Freeing students • After we’re done, we need to free the memory we’ve allocated • One implementation is as we saw in class void free_list(Student *head) { Student *to_free = head; while (to_free != NULL) { head = head->next; free(to_free); to_free = head; } } Department of Computer Science-BGU