1 / 21

Linked Lists

Linked Lists. Array Limitations. Arrays have a fixed size that cannot be changed at run time What if your program had an array to store info regarding 50 students and the user wanted to store more than 50 records…. Arrays – Train Analogy.

fraley
Download Presentation

Linked Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked Lists

  2. Array Limitations • Arrays have a fixed size that cannot be changed at run time • What if your program had an array to store info regarding 50 students and the user wanted to store more than 50 records…

  3. Arrays – Train Analogy • Does a freight train have a fixed number of freight cars ?? • The answer is NO. The number of cars depends on the quantity of freight that needs to be hauled • If the size was fixed, • the cars would have to go empty if freight was less than total capacity • If freight was more than capacity, then more trains would need to be used • Therefore, fixed size trains would be inefficient • Trains’ solution: • For more freight simply add some cars • Reduce the number of cars if freight is less

  4. Linked Lists • Similarly, arrays are inefficient if we do not know in advance, the amount of storage needed • The problem can be solved with Dynamic Memory Allocation • Linked list is a very important data structure which makes use of dynamic memory allocation and can solve the problem of inefficient data storage of arrays • The linked list works very much like a train

  5. but something is missing …

  6. Making of a Linked List • We have so far used structures to store relevant pieces of information e.g. the structstudent_info contains name, course and graduation year of a student • We also used (fixed size) arrays of structures to store multiple structs • Therefore, structs can act as the “freight cars” of a train. • We can create as many structs as we want with every struct located somewhere in the memory

  7. John Cop3223h 2014 ray cot4123 2015 deborah cnt5228 2016 Jenny cda2343 2014

  8. Only if we could connect these structs in a way so as to access one struct after the other !!!!

  9. Creating Links in a Linked List typedefstruct { char name[64]; char course[128]; intyear; structstudent_info *next; } student_info;

  10. John Cop3223h 2014 *next ray cot4123 2015 *next deborah cnt5228 2016 *next Jenny cda2343 2014 *next

  11. Now we have a “LIST” of structs that is “LINKED” • The structs in a linked list are called elements or more commonly nodes • But, how do we access the first node of the linked list ?? • With a special pointer to the first node, usually called First.

  12. First John Cop3223h 2014 *next ray cot4123 2015 *next deborah cnt5228 2016 *next Jenny cda2343 2014 *next

  13. Dynamic Memory Allocation • Used to allocate memory at Run Time. • Also used to Free up memory at run time. • The function malloc(mem_size) returns a pointer to a newly allocated block of memory of size mem_size • The function free(ptr) de-allocates a block of memory pointed to by ptr

  14. Dynamic Memory Allocation student_info* create_node(void) { student_info *ptr = malloc(sizeof(student_info)); } • the pointer ‘ptr’ now contains address of a newly created node • After creating a node, it can be assigned the values that it is created to hold and its next pointer is assigned the address of next node. • If no next node exists (or if its the last node) then as already discussed, a NULL is assigned.

  15. Storing data in a linked list node void get_student_info(student_info* st) { system("cls"); printf("Enter student's name: "); scanf("%s", &st->name); printf("\n\nEnter student's course: "); scanf("%s", &st->course); printf("\n\nEnter student's planned graduation year: "); scanf("%d", &st->year); st->next = NULL; }

  16. Traversing a linked list void print_student_info(student_info *st) { system("cls"); while(st != NULL) { printf("\n\n%s is enrolled in %s will graduate in the year %d\n", st->name, st->course, st->year); st = st->next; } system("pause"); }

  17. the pointer ‘ptr’ now contains address of a newly created node

More Related