Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Dale Roberts, Lecturer Data Structure Review
Dale Roberts Pointer Review (1) int i, *pi; i pi ?? pi = &i; i pi ?1000 *pi i = 10 or *pi = 10 i pi *pi
Dale Roberts Pointer Review (2) typedef struct list_node *list_pointer; typedef struct list_node { int data; list_pointer link; } list_pointer ptr = NULL; ptr 1000 NULL ptr = malloc(sizeof(list_node)); ptr *ptr ptr->data (*ptr).data data link
Dale Roberts Pointer Review (3) void delete(list_pointer *ptr, list_pointer trail, list_pointer node) ptr: a pointer point to a pointer point to a list node ptr *ptr(*ptr)->link trail (node): a pointer point to a list node trail *trail data link trail->link (*trail).link
Dale Roberts Pointer Review (4) element delete(stack_pointer *top) top delete(&st) vs. delete(st) top st st top 400 Does not change.
Dale Roberts Dynamic Storage Question Question: Dynamically allocate an integer, and then return the memory back to the operating system.
Dale Roberts Dynamic Storage Answer Answer: int *pi; pi = (int *) malloc(sizeof(int)); free(pi); request memoryreturn memory
Dale Roberts bat cat sat vat NULL Linked List Insertion Question: Given the following sorted linked list, draw the diagram for inserting the word “mat” into the list.
Dale Roberts bat cat sat vat NULL mat Linked List Insertion (Answer) Answer: Scan the list for where to insert mat (linear search). Make mat point to where cat points, and then make cat point to mat.
Dale Roberts Linked List Deletion Question: Given the following list, delete the word mat. bat cat sat vat NULL mat
Dale Roberts bat cat sat vat NULL mat dangling reference Linked List Deletion Answer: Make cat point to where mat points, then free mat. Make sure to not lose your only reference to mat when you change cat’s link.
Dale Roberts Creating a linked list of words Question: Create a linked list with one node with the value of “bat”. Definition typedef struct list_node, *list_pointer; typedef struct list_node { char data [4]; list_pointer link; }; Creation list_pointer ptr =NULL; Testing #define IS_EMPTY(ptr) (!(ptr)) Allocation ptr=(list_pointer) malloc (sizeof(list_node));
Dale Roberts b a t \0 NULL address of first node ptr data ptr link ptr Reminder: e -> name (*e).name strcpy(ptr -> data, “bat”); ptr -> link = NULL; Creating a linked list of words (Answer)
Dale Roberts Create a two node list typedef struct list_node *list_pointer; typedef struct list_node { int data; list_pointer link; }; list_pointer ptr =NULL 10 20 NULL ptr
Dale Roberts Create a two node list (Answer) list_pointer create2( ) { /* create a linked list with two nodes */ list_pointer first, second; first = (list_pointer) malloc(sizeof(list_node)); second = ( list_pointer) malloc(sizeof(list_node)); second -> link = NULL; second -> data = 20; first -> data = 10; first ->link = second; return first; } 10 20 NULL ptr
Dale Roberts Acknowledgements Some code is from Horowitz, Sahni, and Anderson-Freed, Fundamentals of Data Structures in C. Some slides were originally developed by Chen, Hsin-His.