Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Dale Roberts, Lecturer Data Structure.

Similar presentations


Presentation on theme: "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Dale Roberts, Lecturer Data Structure."— Presentation transcript:

1 Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Dale Roberts, Lecturer IUPUIdroberts@cs.iupui.edu Data Structure Review

2 Dale Roberts Pointer Review (1) int i, *pi; i pi 1000 2000 ?? pi = &i; i pi 1000 2000 ?1000 *pi i = 10 or *pi = 10 i pi 1000 2000 101000 *pi

3 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 1000 2000 *ptr ptr->data  (*ptr).data data link

4 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 1000 2000 ptr 2000 3000 *ptr(*ptr)->link trail (node): a pointer point to a list node trail 1000 3000 *trail data link trail->link  (*trail).link

5 Dale Roberts Pointer Review (4) element delete(stack_pointer *top) top delete(&st) vs. delete(st) 200300 400...... 200 300 top st 200 400 300 st top 400 Does not change.

6 Dale Roberts Dynamic Storage Question Question: Dynamically allocate an integer, and then return the memory back to the operating system.

7 Dale Roberts Dynamic Storage Answer Answer: int *pi; pi = (int *) malloc(sizeof(int)); free(pi); request memoryreturn memory

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

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

10 Dale Roberts Linked List Deletion Question: Given the following list, delete the word mat. bat  cat  sat  vat NULL mat 

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

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

13 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)

14 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

15 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

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


Download ppt "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Dale Roberts, Lecturer Data Structure."

Similar presentations


Ads by Google