Linked Lists. Array Limitations Arrays have a fixed size that cannot be changed at run time What if...
-
Upload
jonas-henry-davis -
Category
Documents
-
view
215 -
download
0
Transcript of Linked Lists. Array Limitations Arrays have a fixed size that cannot be changed at run time What if...
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• 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
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
but something is missing …
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
JohnCop3223h
2014 raycot4123
2015
Jennycda2343
2014
deborahcnt5228
2016
• Only if we could connect these structs in a way so as to access one struct after the other !!!!
Creating Links in a Linked List
typedef struct { char name[64]; char course[128]; int year; struct student_info *next;
} student_info;
JohnCop3223h
2014*next
raycot4123
2015*next
Jennycda2343
2014*next
deborahcnt5228
2016*next
• 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.
JohnCop3223h
2014*next
raycot4123
2015*next
Jennycda2343
2014*next
deborahcnt5228
2016*next
First
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
Dynamic Memory Allocationstudent_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.
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; }
Traversing a linked listvoid 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");}
• the pointer ‘ptr’ now contains address of a newly created node