56
Pointers (additional material) 1 Department of Computer Science-BGU 22:36:58

Pointers (additional material)

  • Upload
    kasen

  • View
    37

  • Download
    1

Embed Size (px)

DESCRIPTION

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 !!. - PowerPoint PPT Presentation

Citation preview

Page 1: Pointers (additional material)

Pointers(additional material)

1

Department of Computer Science-BGU 23:23:23

Page 2: 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 !!

23:23:23Department of Computer Science-BGU

2

Page 3: Pointers (additional material)

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 !!

23:23:23Department of Computer Science-BGU

3

Page 4: Pointers (additional material)

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.

23:23:23Department of Computer Science-BGU

4

Page 5: Pointers (additional material)

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 */

23:23:23Department of Computer Science-BGU

5

Page 6: Pointers (additional material)

Complete solution(cont.)

// sorting do{

flag =0;for(i=0 ; i< n-1 ; i++)

// names + i , names +i+1if( 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;

}

23:23:23Department of Computer Science-BGU

6

Page 7: Pointers (additional material)

Linked Lists

23:23:23

7

Department of Computer Science-BGU

Page 8: Pointers (additional material)

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.

23:23:23

8

Department of Computer Science-BGU

Page 9: Pointers (additional material)

Linked lists?

head

NULL

Structures

23:23:24

9

Department of Computer Science-BGU

A better alternative might be using a linked list, by “self reference”.

Page 10: Pointers (additional material)

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;

23:23:24

10

Department of Computer Science-BGU

Page 11: Pointers (additional material)

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 */

} ;

23:23:24Department of Computer Science-BGU

11

Page 12: Pointers (additional material)

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…

23:23:24

12

Department of Computer Science-BGU

Page 13: Pointers (additional material)

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

23:23:24

13

Department of Computer Science-BGU

Page 14: Pointers (additional material)

Linked lists - insertion

Head

Insert new item:

Previous

Next

23:23:24

14

Department of Computer Science-BGU

Page 15: Pointers (additional material)

Linked lists - insertion

Head

Insert new item:

Previous

Next

23:23:24

15

Department of Computer Science-BGU

Page 16: Pointers (additional material)

Linked lists - insertion

Head

Insert new item:

Previous

Next

23:23:24

16

Department of Computer Science-BGU

Page 17: Pointers (additional material)

Adds a new item to the end of the list

head NULL

newItem

NULL

23:23:24

17

Department of Computer Science-BGU

Page 18: Pointers (additional material)

Adds a new item to the end of the list

newItem

NULLhead

23:23:24

18

Department of Computer Science-BGU

Page 19: Pointers (additional material)

Adds a new item to the end of the list

NULL

head

23:23:24

19

Department of Computer Science-BGU

Page 20: Pointers (additional material)

Adds a new item to the end of the list

head

NULL

newItem

NULL

currItem

23:23:24

20

Department of Computer Science-BGU

Page 21: Pointers (additional material)

Adds a new item to the end of the list

head

NULL

newItem

NULL

currItem

23:23:24

21

Department of Computer Science-BGU

Page 22: Pointers (additional material)

Adds a new item to the end of the list

head

NULL

newItem

NULL

currItem

23:23:24

22

Department of Computer Science-BGU

Page 23: Pointers (additional material)

Adds a new item to the end of the list

head

NULL

newItem

NULL

currItem

23:23:24

23

Department of Computer Science-BGU

Page 24: Pointers (additional material)

Adds a new item to the end of the list

head

NULL

newItem

NULL

currItem

23:23:24

24

Department of Computer Science-BGU

Page 25: Pointers (additional material)

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;}  

23:23:24

25

Department of Computer Science-BGU

Page 26: Pointers (additional material)

Inserts into a sorted list

head NULL

newItem

NULL

23:23:24

26

Department of Computer Science-BGU

Page 27: Pointers (additional material)

Inserts into a sorted list

newItem

NULLhead

23:23:24

27

Department of Computer Science-BGU

Page 28: Pointers (additional material)

Inserts into a sorted list

NULL

head

23:23:24

28

Department of Computer Science-BGU

Page 29: Pointers (additional material)

Inserts into a sorted list

8 28

head

NULL16

5

newItem

NULL

23:23:24

29

Department of Computer Science-BGU

Page 30: Pointers (additional material)

Inserts into a sorted list

8 28

head

NULL16

5

newItem

NULL

23:23:24

30

Department of Computer Science-BGU

Page 31: Pointers (additional material)

Inserts into a sorted list

8 28

head

NULL16

5

newItem

23:23:24

31

Department of Computer Science-BGU

Page 32: Pointers (additional material)

Inserts into a sorted list

8 28

head

NULL16

20

newItem

NULL

23:23:24

32

Department of Computer Science-BGU

Page 33: Pointers (additional material)

Inserts into a sorted list

8 28

head

NULL16

20

newItem

NULL

currItem

23:23:24

33

Department of Computer Science-BGU

Page 34: Pointers (additional material)

Inserts into a sorted list

8 28

head

NULL16

20

newItem

NULL

currItem

23:23:24

34

Department of Computer Science-BGU

Page 35: Pointers (additional material)

Inserts into a sorted list

8 28

head

NULL16

20

newItem

NULL

currItem

23:23:24

35

Department of Computer Science-BGU

Page 36: Pointers (additional material)

Inserts into a sorted list

// while keeping it sorted ascending by keyitem * 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;}   

23:23:25

36

Department of Computer Science-BGU

Page 37: Pointers (additional material)

Linked lists - searching

head?currItem

23:23:26

37

Department of Computer Science-BGU

Page 38: Pointers (additional material)

Linked lists - searching

head?currItem

23:23:26

38

Department of Computer Science-BGU

Page 39: Pointers (additional material)

Linked lists - searching

head?currItem

!

23:23:26

39

Department of Computer Science-BGU

Page 40: Pointers (additional material)

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 keyreturn NULL;

23:23:26

40

Department of Computer Science-BGU

Page 41: Pointers (additional material)

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');

}

23:23:26

41

Department of Computer Science-BGU

Page 42: Pointers (additional material)

Linked lists - delete

Head

Structure to delete

23:23:26

42

Department of Computer Science-BGU

Page 43: Pointers (additional material)

Linked lists - delete

Head

Current

23:23:26

43

Department of Computer Science-BGU

Previous

NULL

Structure to delete

Page 44: Pointers (additional material)

Linked lists - delete

HeadPreviou

sCurrent

23:23:26

44

Department of Computer Science-BGU

Structure to delete

Page 45: Pointers (additional material)

Linked lists - delete

HeadPreviou

sCurrent

23:23:26

45

Department of Computer Science-BGU

Structure to delete

Page 46: Pointers (additional material)

Linked lists - delete

HeadPreviou

sCurrent

23:23:26

46

Department of Computer Science-BGU

Page 47: Pointers (additional material)

Linked lists - delete

HeadPreviou

sCurrent

23:23:26

47

Department of Computer Science-BGU

Page 48: Pointers (additional material)

Linked lists - delete

HeadPreviou

s

23:23:26

48

Department of Computer Science-BGU

Page 49: Pointers (additional material)

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;}

23:23:26

49

Department of Computer Science-BGU

Page 50: Pointers (additional material)

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;

}}

23:23:26

50

Department of Computer Science-BGU

Page 51: Pointers (additional material)

Recursive freeing

A perhaps simpler way to free a list is recursively.

void free_list(Student *head){

if (head== NULL) /* Finished freeing. Empty list */return;

free_list(head->next); /* Recursively free what’s ahead */free(head);

}

23:23:26

51

Department of Computer Science-BGU

Page 52: Pointers (additional material)

Split linked list

typedef struct stam item;struct stam{

int value;item * next;

}void main(){

item * head=*odd=*even = NULL;//// /* build list */// Now split it in two lists with odd and even membersvoid Split(head, odd, even);print_list(odd);print_list(even);

}

23:23:26Department of Computer Science-BGU

52

Page 53: Pointers (additional material)

Split linked list

// Split the nodes to these 'a' and 'b' listsvoid Split(item * source, item * od, item * ev) {

item * current = source;while (current != NULL) {

temp = current;if(temp->value %2 != 0)od = add_last(od,current);else ev = add_last(ev,current);current = current -> next;temp->next = NULL;

}}

23:23:26Department of Computer Science-BGU

53

Page 54: Pointers (additional material)

Split linked list

typedef struct stam item;struct stam{

int value;item * next;

}void main(){

item * head=*odd=*even = NULL;//// /* build list */// Now split it in two lists with odd and even membersvoid Split(head, &odd, &even);print_list(odd);print_list(even);

}

23:23:26Department of Computer Science-BGU

54

Page 55: Pointers (additional material)

Split linked list

void Split(item * source, item ** od, item ** ev) {item * current = source;item * a =*b = NULL;while (current != NULL) {

temp = current;if(temp->value %2 != 0)

a = add_last(a,current);else b = add_last(b,current);current = current -> next;temp->next = NULL;

}*od = a;*ev = b;

}

23:23:26Department of Computer Science-BGU

55

Page 56: Pointers (additional material)

Split linked list

// Split the nodes to these 'a' and 'b' listsvoid Split(item * source, item ** od , item ** ev) {

item * a = NULL; item * b = NULL;item * current = source;while (current != NULL) {

MoveItem(&a, &current); // Move a node to 'a'if (current != NULL) { MoveItem(&b, &current); // Move a node to 'b'{

{*od= a;*ev = b;

{

23:23:26Department of Computer Science-BGU

56