Download presentation
Presentation is loading. Please wait.
Published byJonas Henry Davis Modified over 9 years ago
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
8
but something is missing …
10
Making of a Linked List We have so far used structures to store relevant pieces of information e.g. the struct student_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
11
John Cop3223h 2014 ray cot4123 2015 Jenny cda2343 2014 deborah cnt5228 2016
12
Only if we could connect these structs in a way so as to access one struct after the other !!!!
13
Creating Links in a Linked List typedef struct { char name[64]; char course[128]; int year; struct student_info *next; } student_info;
14
John Cop3223h 2014 *next ray cot4123 2015 *next Jenny cda2343 2014 *next deborah cnt5228 2016 *next
15
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.
16
John Cop3223h 2014 *next ray cot4123 2015 *next Jenny cda2343 2014 *next deborah cnt5228 2016 *next First
17
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
18
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.
19
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; }
20
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"); }
21
the pointer ‘ptr’ now contains address of a newly created node
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.