Pointers and Arrays in C An array name



























- Slides: 27

Pointers and Arrays in C • An array name by itself is an address, or pointer in C. In general, a pointer is a variable that assumes addresses as values. • An array name is a particular fixed address that can be thought of as a fixed or constant pointer. • When an array is declared, the compiler allocates sufficient space beginning with some base address to accommodate every element in the array. • The base address of the array is the address of the first element in the array (index position 0). 1

Pointers and Arrays in C (cont. ) • Suppose we define the following array and pointer: int a[100], *ptr; Assume that the system allocates memory bytes 400, 404, 408, . . . , 796 to the array. Recall that integers are allocated 32 bits = 4 bytes. – The two statements: ptr = a; and ptr = &a[0]; are equivalent and would assign the value of 400 to ptr. • Pointer arithmetic provides an alternative to array indexing in C. – The two statements: ptr = a + 1; and ptr = &a[1]; are equivalent and would assign the value of 404 to ptr. 2

Pointers and Arrays in C (cont. ) • Assuming the elements of the array have been assigned values, the following code would sum the elements of the array: sum = 0; for (ptr = a; ptr < &a[100]; ++ptr) sum += *ptr; • In general, if i is of type int, then ptr + i is the ith offset from the address of ptr. • Using this, here is another way to sum the array: sum = 0; for (i = 0; i < 100; ++i) sum += *(a + i); 3

Linked List Implementation/Coding Issues in C • We can define structures with pointer fields that refer to the structure type containing them struct list { int data; struct list *next; data next } • The pointer variable next is called a link. Each structure is linked to a succeeding structure by way of the field next. The pointer variable next contains an address of either the location in memory of the successor struct list element or the special value NULL. 4

Example struct list a, b, c; a. data = 1; b. data = 2; c. data = 3; a. next = b. next = c. next = NULL; a b c 1 NULL 2 NULL 3 data next data Page 5 NULL next 5

Example continues • • • a. next b. next a = &b; = &c; -> data has value 2 -> next -> data has value 3 -> next -> data error !! b 1 c 2 3 Page 6 6

Dynamic Memory Allocation • Creating and maintaining dynamic data structures requires dynamic memory allocation – the ability for a program to obtain more memory space at execution time to hold new values, and to release space no longer needed. • In C, functions malloc and free, and operator sizeof are essential to dynamic memory allocation. Page 7 7

Dynamic Memory Operators sizeof and malloc • Unary operator sizeof is used to determine the size in bytes of any data type. sizeof(double) sizeof(int) • Function malloc takes as an argument the number of bytes to be allocated and return a pointer of type void * to the allocated memory. (A void * pointer may be assigned to a variable of any pointer type. ) It is normally used with the sizeof operator. Page 8 8

Dynamic Memory Operators in C Example struct node{ int data; struct node *next; }; struct node *ptr; ptr = (struct node *) /*type casting */ malloc(sizeof(struct node)); ptr ? ? Page 9 9

The Free Operator in C • Function free deallocates memory- i. e. the memory is returned to the system so that the memory can be reallocated in the future. free(ptr); ptr ? Page 10 10

Examples of the Nodes of a Linked List • A node in a linked list is a structure that has at least two fields. One of the fields is a data field; the other is a pointer that contains the address of the next node in the sequence. • A node with one data field: struct node{ int number; struct node * link; }; Page 11 number link 11

The Nodes of a Linked List – Examples • A node with three data fields: struct student{ char name[20]; int id; double grd. Pts; struct student *next_student; Name Page 12 id grd. Pts next_student 12

The Nodes of a Linked List – Examples • A structure in a node: struct person{ char name[20]; char address[30]; char phone[10]; }; struct person_node{ data next struct person data; struct person_node }; name address phone next *next; data Page 13 13

Basic Operations on a Linked List 1. 2. 3. 4. Add a node. Delete a node. Search for a node. Traverse (walk) the list. Useful for counting operations or aggregate operations. Page 14 14

Adding Nodes to a Linked List Adding a Node There are four steps to add a node to a linked list: • • Allocate memory for the new node. Determine the insertion point (you need to know only the new node’s predecessor (p. Pre) Point the new node to its successor. Point the predecessor to the new node. Pointer to the predecessor (p. Pre) can be in one of two states: • it can contain the address of a node (i. e. you are adding somewhere after the first node – in the middle or at the end) • it can be NULL (i. e. you are adding either to an empty list or at the beginning of the list) Page 15 15

Adding Nodes to an Empty Linked List Before: p. New Code: p. New -> next = p. Head; // set link to NULL 39 p. Head = p. New; // point list to first node p. Head p. Pre After: p. New 39 p. Head p. Pre Page 16 16

Adding a Node to the Beginning of a Linked List Before: Code (same): p. New -> next = p. Head; // set link to NULL p. New p. Head = p. New; // point list to first node 39 p. Head 75 124 p. Pre After: p. New 39 p. Head 75 124 p. Pre Page 17 17

Adding a Node to the Middle of a Linked List Before: Code p. New -> next = p. Pre -> next; p. New p. Pre -> next = p. New; 64 55 124 p. Pre After: p. New 64 55 124 p. Pre Page 18 18

Adding a Node to the End of a Linked List Before: Code p. New -> next = NULL; p. New p. Pre -> next = p. New; 144 55 124 p. Pre After: p. New 144 55 124 p. Pre Page 19 19

Inserting a Node Into a Linked List • Given the head pointer (p. Head), the predecessor (p. Pre) and the data to be inserted (item). Memory must be allocated for the new node (p. New) and the links properly set. //insert a node into a linked list struct node *p. New; p. New = (struct node *) malloc(sizeof(struct node)); p. New -> data = item; if (p. Pre == NULL){ //add before first logical node or to an empty list p. New -> next = p. Head; p. Head = p. New; } else { //add in the middle or at the end p. New -> next = p. Pre -> next; p. Pre -> next = p. New; } Page 20 20

Deleting a Node from a Linked List • Deleting a node requires that we logically remove the node from the list by changing various links and then physically deleting the node from the list (i. e. , return it to the heap). • Any node in the list can be deleted. Note that if the only node in the list is to be deleted, an empty list will result. In this case the head pointer will be set to NULL. • To logically delete a node: – First locate the node itself (p. Cur) and its logical predecessor (p. Pre). – Change the predecessor’s link field to point to the deleted node’s successor (located at p. Cur -> next). – Recycle the node using the free() function. Page 21 21

Deleting the First Node from a Linked List Before: Code: p. Head 75 p. Head = p. Cur -> next; free(p. Cur); 124 p. Cur p. Pre After: p. Head p. Pre Recycled 124 p. Cur Page 22 22

Deleting a Node from a Linked List – General Case Before: Code: 75 96 p. Pre -> next = p. Cur -> next; free(p. Cur); 124 p. Cur p. Pre After: Recycled 75 p. Pre 124 p. Cur Page 23 23

Deleting a Node From a Linked List • Given the head pointer (p. Head), the node to be deleted (p. Cur), and its predecessor (p. Pre), delete p. Cur and free the memory allocated to it. //delete a node from a linked list if (p. Pre == NULL) //deletion is on the first node of the list p. Head = p. Cur -> next; else //deleting a node other than the first node of the list p. Pre -> next = p. Cur -> next; free(p. Cur). Page 24 24

Searching a Linked List • Notice that both the insert and delete operations on a linked list must search the list for either the proper insertion point or to locate the node corresponding to the logical data value that is to be deleted. //search the nodes in a linked list p. Pre = NULL; p. Cur = p. Head; //search until the target value is found or the end of the list is reached while (p. Cur != NULL && p. Cur -> data != target) { p. Pre = p. Cur; p. Cur = p. Cur -> next; } //determine if the target is found or ran off the end of the list if (p. Cur != NULL) found = 1; else found = 0; Page 25 25

Traversing a Linked List • List traversal requires that all of the data in the list be processed. Thus each node must be visited and the data value examined. //traverse a linked list Struct node *p. Walker; p. Walker = p. Head; printf(“List contains: n”); while (p. Walker != NULL){ printf(“%d ”, p. Walker -> data); p. Walker = p. Walker -> next; } Page 26 26

Practice Problems Using a Linked List • Create C functions to solve the following problems with linked lists. We’ll create solutions in the next class, but you try them first! 1. Write a recursive function that will search a list for a specific item, returning NULL if it is not found a pointer to the node containing the value if it is found. 2. Write a function that will insert a node pointed to by newnode before the node pointed to by target. head target newnode Page 27 27