Presentation is loading. Please wait.

Presentation is loading. Please wait.

CGS 3460 Pointer n To hold the location of another variable n Declaration: la type and a name with an asterisk lE.g. int *ptr; n Assigning a value lptr.

Similar presentations


Presentation on theme: "CGS 3460 Pointer n To hold the location of another variable n Declaration: la type and a name with an asterisk lE.g. int *ptr; n Assigning a value lptr."— Presentation transcript:

1 CGS 3460 Pointer n To hold the location of another variable n Declaration: la type and a name with an asterisk lE.g. int *ptr; n Assigning a value lptr = &k;

2 CGS 3460 Pointers and Structures n Declaration struct date *datePtr; n Initialization datePtr = &today; n Access members l(*datePtr).year ldatePtr->year struct date{ int day; char month[10]; int year; }; struct date today;

3 CGS 3460 Structures contains pointers n Pointers can be members in a structure struct date{ int day; char* month; int year; }; n Special case: struct date{ int day; char* month; int year; struct date *next; }; day month Year next day month Year next

4 CGS 3460 Linked List n What is a list? lNode lLink n An alternative to Array n Pros/Cons l+ More flexible l+ Can grow and shrink as necessary l+ Insert and delete node with particular index l- Cannot be randomly accessed l+ sorted (ordered list)

5 CGS 3460 Linked List n Operations lStart a list lCreate a node lInsert lDelete lSearch lPrint

6 CGS 3460 Memory Allocation n malloc is by far most frequently used Defined in void *malloc(size_t size); lAllocate memory space with size bytes lWhy does it return a void pointer? Because it doesn't matter to malloc to what type this memory will be used for If no memory of the size available, will return null Be sure to check whether it is null;

7 CGS 3460 Function sizeof n Helpful when dynamically allocating memory lsizeof(data type) returns a size_t of the data type in byte(s) For a typical 32-bit machine sizeof(int) returns 4

8 CGS 3460 Free Allocated Space n Very important lSystem won’t automatically take back memory space allocated through malloc lIf not free, a memory leak n How? lUse function void free(void *ptr); lIt release the memory space referenced by ptr Note that free can take in NULL, as specified by ANSI

9 CGS 3460 Build a Linked List n Build a structure representing a node struct studentRecord { int idNum; struct studentRecord *next; }; n Initialize the node struct studentRecord *first = NULL; first NULL

10 CGS 3460 Create a node n Follow these steps: lAllocate memory for the node lSet data into the node struct studentRecord *new_student; new_student = malloc(sizeof(struct studentRecord )); (* new_student).idNum = 1; new_student  next = NULL; Id: 1 Next: NULL new_student

11 CGS 3460 Insert a node: insert in the front(empty) n Follow these steps: lCreate a node lSet the node pointing to the front of the list lSet it as the starting node of this list new_student = malloc(sizeof(struct studentRecord )); new_student  idNum = 2; new_student  next = first; first = new_student ; Id: 2 Next: new_student first NULL

12 CGS 3460 Insert a node: insert in the front(not empty) n Follow these steps: lCreate a node lSet the node pointing to the front of the list lSet it as the starting node of this list new_student = malloc(sizeof(struct studentRecord )); new_student  idNum = 2; new_student  next = first; first = new_student ; Id: 2 Next: new_student first NULL Id: 5 Next:

13 CGS 3460 Insert a node: insert in middle n To insert a new node after node called pt, follow these steps: lCreate a node lSet the node pointing to the next node after pt in the list lSet it as the next node of this list after pt new_student = malloc(sizeof(struct studentRecord )); new_student  idNum = 2; new_student  next = pt  next; pt  next = new_student ; Id: 2 Next: new_student first NULL Id: 5 Next: pt

14 CGS 3460 Traversing along the List for (p = first; p != NULL; p = p  next) { …. } int Length(struct studentRecord* first) { int count = 0; struct studentRecord* current = first; while (current != NULL) { count++; current=current->next; } return(count); }

15 CGS 3460 Get nth Element in List int GetNth(struct studentRecord * first, int index) { struct studentRecord * current = first; int count = 0; // the index of the node while (current != NULL) { if (count == index) return(current  idNum ); count++; current = current  next; } return(-1); // if we get to this line, the caller was asking // for a non-existent element }

16 CGS 3460 Delete n Delete (almost reverse of insertion) llocating the node to be deleted (see search a linked list) laltering the previous node to bypass the deleted node lcalling free to reclaim the space occupied by the deleted node

17 CGS 3460 Example for deleting n To delete a node p from a linked list, you should also know the node which is in front of p in the list(q) if (p != NULL) { q  next = p  next; free(p); } NULL Id: 5 Next: Id: 2 Next: first Id: 2 Next: p q

18 CGS 3460 Example for deleting n If deleting the first node in the list if( p == first ) { first = p->next; free(p); } NULL Id: 5 Next: Id: 2 Next: first Id: 2 Next: p

19 CGS 3460 Demo n http://www.cosc.canterbury.ac.nz/mukundan/dsal/LinkList Appl.html http://www.cosc.canterbury.ac.nz/mukundan/dsal/LinkList Appl.html


Download ppt "CGS 3460 Pointer n To hold the location of another variable n Declaration: la type and a name with an asterisk lE.g. int *ptr; n Assigning a value lptr."

Similar presentations


Ads by Google