Pointers (additional material)

Preview:

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

Pointers(additional material)

1

Department of Computer Science-BGU 23:23:23

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

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

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

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

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

Linked Lists

23:23:23

7

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.

23:23:23

8

Department of Computer Science-BGU

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”.

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

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

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

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

Linked lists - insertion

Head

Insert new item:

Previous

Next

23:23:24

14

Department of Computer Science-BGU

Linked lists - insertion

Head

Insert new item:

Previous

Next

23:23:24

15

Department of Computer Science-BGU

Linked lists - insertion

Head

Insert new item:

Previous

Next

23:23:24

16

Department of Computer Science-BGU

Adds a new item to the end of the list

head NULL

newItem

NULL

23:23:24

17

Department of Computer Science-BGU

Adds a new item to the end of the list

newItem

NULLhead

23:23:24

18

Department of Computer Science-BGU

Adds a new item to the end of the list

NULL

head

23:23:24

19

Department of Computer Science-BGU

Adds a new item to the end of the list

head

NULL

newItem

NULL

currItem

23:23:24

20

Department of Computer Science-BGU

Adds a new item to the end of the list

head

NULL

newItem

NULL

currItem

23:23:24

21

Department of Computer Science-BGU

Adds a new item to the end of the list

head

NULL

newItem

NULL

currItem

23:23:24

22

Department of Computer Science-BGU

Adds a new item to the end of the list

head

NULL

newItem

NULL

currItem

23:23:24

23

Department of Computer Science-BGU

Adds a new item to the end of the list

head

NULL

newItem

NULL

currItem

23:23:24

24

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

23:23:24

25

Department of Computer Science-BGU

Inserts into a sorted list

head NULL

newItem

NULL

23:23:24

26

Department of Computer Science-BGU

Inserts into a sorted list

newItem

NULLhead

23:23:24

27

Department of Computer Science-BGU

Inserts into a sorted list

NULL

head

23:23:24

28

Department of Computer Science-BGU

Inserts into a sorted list

8 28

head

NULL16

5

newItem

NULL

23:23:24

29

Department of Computer Science-BGU

Inserts into a sorted list

8 28

head

NULL16

5

newItem

NULL

23:23:24

30

Department of Computer Science-BGU

Inserts into a sorted list

8 28

head

NULL16

5

newItem

23:23:24

31

Department of Computer Science-BGU

Inserts into a sorted list

8 28

head

NULL16

20

newItem

NULL

23:23:24

32

Department of Computer Science-BGU

Inserts into a sorted list

8 28

head

NULL16

20

newItem

NULL

currItem

23:23:24

33

Department of Computer Science-BGU

Inserts into a sorted list

8 28

head

NULL16

20

newItem

NULL

currItem

23:23:24

34

Department of Computer Science-BGU

Inserts into a sorted list

8 28

head

NULL16

20

newItem

NULL

currItem

23:23:24

35

Department of Computer Science-BGU

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

Linked lists - searching

head?currItem

23:23:26

37

Department of Computer Science-BGU

Linked lists - searching

head?currItem

23:23:26

38

Department of Computer Science-BGU

Linked lists - searching

head?currItem

!

23:23:26

39

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

23:23:26

40

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

}

23:23:26

41

Department of Computer Science-BGU

Linked lists - delete

Head

Structure to delete

23:23:26

42

Department of Computer Science-BGU

Linked lists - delete

Head

Current

23:23:26

43

Department of Computer Science-BGU

Previous

NULL

Structure to delete

Linked lists - delete

HeadPreviou

sCurrent

23:23:26

44

Department of Computer Science-BGU

Structure to delete

Linked lists - delete

HeadPreviou

sCurrent

23:23:26

45

Department of Computer Science-BGU

Structure to delete

Linked lists - delete

HeadPreviou

sCurrent

23:23:26

46

Department of Computer Science-BGU

Linked lists - delete

HeadPreviou

sCurrent

23:23:26

47

Department of Computer Science-BGU

Linked lists - delete

HeadPreviou

s

23:23:26

48

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

23:23:26

49

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;

}}

23:23:26

50

Department of Computer Science-BGU

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

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

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

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

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

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

Recommended