770 likes | 866 Views
Pointers (additional material). 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 !!.
E N D
Pointers(additional material) Department of Computer Science-BGU
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
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
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
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
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
Linked Lists Department of Computer Science-BGU
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
Linked lists ? A better alternative might be using a linked list, by “self reference”. head NULL Structures Department of Computer Science-BGU
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
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
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
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
Linked lists - insertion Previous Next Head … Insert new item: Department of Computer Science-BGU
Linked lists - insertion Previous Next Head … Insert new item: Department of Computer Science-BGU
Linked lists - insertion Previous Next Head … Insert new item: Department of Computer Science-BGU
Adds a new item to the end of the list head NULL newItem NULL Department of Computer Science-BGU
Adds a new item to the end of the list newItem NULL head Department of Computer Science-BGU
Adds a new item to the end of the list head NULL Department of Computer Science-BGU
Adds a new item to the end of the list currItem head NULL newItem NULL Department of Computer Science-BGU
Adds a new item to the end of the list currItem head NULL newItem NULL Department of Computer Science-BGU
Adds a new item to the end of the list currItem head NULL newItem NULL Department of Computer Science-BGU
Adds a new item to the end of the list currItem head NULL newItem NULL Department of Computer Science-BGU
Adds a new item to the end of the list currItem head NULL newItem NULL Department of Computer Science-BGU
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
Inserts into a sorted list head NULL newItem NULL Department of Computer Science-BGU
Inserts into a sorted list newItem NULL head Department of Computer Science-BGU
Inserts into a sorted list head NULL Department of Computer Science-BGU
Inserts into a sorted list head 8 16 28 NULL newItem 5 NULL Department of Computer Science-BGU
Inserts into a sorted list head 8 16 28 NULL newItem 5 NULL Department of Computer Science-BGU
Inserts into a sorted list head 8 16 28 NULL newItem 5 Department of Computer Science-BGU
Inserts into a sorted list head 8 16 28 NULL newItem 20 NULL Department of Computer Science-BGU
Inserts into a sorted list currItem head 8 16 28 NULL newItem 20 NULL Department of Computer Science-BGU
Inserts into a sorted list currItem head 8 16 28 NULL newItem 20 NULL Department of Computer Science-BGU
Inserts into a sorted list currItem head 8 16 28 NULL newItem 20 NULL Department of Computer Science-BGU
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
Linked lists - searching ? currItem head … Department of Computer Science-BGU
Linked lists - searching ? currItem head … Department of Computer Science-BGU
Linked lists - searching ? currItem head ! … Department of Computer Science-BGU
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
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
Linked lists - delete Head … Structure to delete Department of Computer Science-BGU
Linked lists - delete Previous NULL Current Head … Structure to delete Department of Computer Science-BGU
Linked lists - delete Previous Current Head … Structure to delete Department of Computer Science-BGU
Linked lists - delete Previous Current Head … Structure to delete Department of Computer Science-BGU
Linked lists - delete Previous Current Head … Department of Computer Science-BGU
Linked lists - delete Previous Current Head … Department of Computer Science-BGU
Linked lists - delete Previous Head … Department of Computer Science-BGU
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
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