EEE 243B Applied Computer Programming - PowerPoint PPT Presentation

barclay-palmer
eee 243b applied computer programming n.
Skip this Video
Loading SlideShow in 5 Seconds..
EEE 243B Applied Computer Programming PowerPoint Presentation
Download Presentation
EEE 243B Applied Computer Programming

play fullscreen
1 / 21
Download Presentation
EEE 243B Applied Computer Programming
71 Views
Download Presentation

EEE 243B Applied Computer Programming

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. EEE 243BApplied Computer Programming Linked Lists I – Arrays of structures Structure of Singly LL, Creating LL §15.1 – 15.2

  2. Review • How do we access the fields in a structure? • If I have a pointer, what operator do I use to dereference and access a field at the same time? Prof S.P. Leblanc

  3. Outline • Arrays of structures • Pointers inside a structure • Linked lists • Head of the list • Nodes • Linked List Example Prof S.P. Leblanc

  4. 1. Arrays of structures • When we created type-defined structures (typedefstruct {…} TYPE_NAME;) we created new complex types that are very expressive • We also gave ourselves the ability to create derived types from these complex types • We can now have arrays of structures: TYPE_NAME arrayName[X]; Prof S.P. Leblanc

  5. 1. Arrays of structures • For example: typedefstruct { char firstName[15]; char lastName[25]; unsigned long collegeNumber; float average; } STUDENT; //name of the type STUDENT students[20]; //an array of students Prof S.P. Leblanc

  6. 1. Arrays of structures • Arrays of structures are powerful for storing a fixed quantity of complex data that will not change often • However, arrays of structures have some limitations • You must know the quantity of elements in your array before compile time • That is not flexible. You could use very large arrays that contain a maximum number of elements – this is a waste of memory • If you have to delete an element, you must decide to either leave a “hole” in your array or to move back all the elements above the deleted position • If you want to insert an element in an array in a given order (alpha by lastName) you may have to move several elements – Not very efficient for large arrays Prof S.P. Leblanc

  7. 1. Arrays of structures • Deleting an element in an array: • Inserting an element in an array: Prof S.P. Leblanc

  8. 2. Pointers inside a structure • In order to resolve the inefficiencies of using arrays to store structures, we call on the power of pointers • Each structure that we define can contain any other data types as fields; including pointer types Prof S.P. Leblanc

  9. 2. Pointers inside a structure • For example, if I wanted to store the name of a lab partner inside my STUDENT structure, I could include two new fields: typedefstruct { char firstName[15]; char lastName[25]; unsigned long collegeNumber; float average; char labPartnerFirstName[15]; char labPartnerLastName[25]; } STUDENT; //name of the type Prof S.P. Leblanc

  10. 2. Pointers inside a structure • The last example required 40 more bytes per structure. A more efficient way would be to include a pointer to the lab partner structure (only 4 additional bytes): typedefstructSTUDENT_TAG { char firstName[15]; char lastName[25]; unsigned long collegeNumber; float average; structSTUDENT_TAG* labPartner; } STUDENT; //name of the type Prof S.P. Leblanc

  11. Stan Marsh 21345 75.2 49010 Eric Cartman 22348 78.1 38090 2. Pointers inside a structure 38090 49010 Prof S.P. Leblanc

  12. 3. Linked List • So we can point from one structure to another structure…and link them together • We can therefore create another kind of data structure • Instead of using arrays of structures, we could use pointers to link all the structures of the same type together • This new data structure is called a linked list • The elements in a linked list are referred to as nodes Prof S.P. Leblanc

  13. 3a. Linked lists - Nodes • Each node in the linked list contains two main components: • The data – i.e. our student information • The link – a pointer to the next node in the list • A linked list is therefore a chain of structures of the same type Prof S.P. Leblanc

  14. 3b. Linked lists - Head • Linked lists are dynamic structures that grow and shrink as we need. • We therefore make use of the malloc() and free() functions to grow and shrink our structures. • As we saw in the lecture on dynamic memory allocation, newly allocated blocks of memory have no symbolic names attached to them. • We must therefore declare a pointer to “hold” the head(start)of the linked list Prof S.P. Leblanc

  15. 3c. Linked list – Example • I could therefore declare my student node structure as follows: typedefstructSTUDENT_NODE_TAG { char firstName[15]; char lastName[25]; unsigned long collegeNumber; float average; structSTUDENT_NODE_TAG* pNextStdNode; } STUDENT_NODE; //name of the type Prof S.P. Leblanc

  16. 3c. Example – Create with 1 node • The head of my linked list is a pointer of type STUDENT_NODE: STUDENT_NODE* pStudentList= NULL; • I could then add my first student in the list: pStudentList = (STUDENT_NODE*)malloc(sizeof(STUDENT_NODE)); strcpy(pStudentList->firstName,"Holden"); strcpy(pStudentList->lastName,"Caulfield"); pStudentList->collegeNumber = 21345; pStudentList->average = 74.2; pStudentList->pNextStdNode = NULL; //next node does not exist Prof S.P. Leblanc

  17. Holden Caulfield 21345 75.2 NULL 3c. Example – Create with 1 node 38090 0 pStudentList 38090 111459 Prof S.P. Leblanc

  18. 3c. Example – Adding a node • I could then add a second student in the list after Holden: STUDENT_NODE* pNewStudent= NULL; //in my declaration sect … pNewStudent=(STUDENT_NODE*)malloc(sizeof(STUDENT_NODE)); strcpy(pNewStudent->firstName,"Roland"); strcpy(pNewStudent->lastName,"Deschain"); pNewStudent->collegeNumber = 22348; pNewStudent->average = 78.1; pNewStudent->pNextStdNode = NULL; //Now we link the first node to point to the second pStudentList->pNextStdNode = pNewStudent; Prof S.P. Leblanc

  19. Holden Caulfield 21345 75.2 49010 Roland Deschain 22348 78.1 NULL 3c. Example – Adding a node 38090 49010 0 pStudentList 38090 111459 Prof S.P. Leblanc

  20. Quiz Time • Why are arrays of structure not efficient? • Why do you have to have a head on a linked list? Prof S.P. Leblanc

  21. Next lecture • Linked List II Prof S.P. Leblanc