Linked Lists. Array Limitations Arrays have a fixed size that cannot be changed at run time What if...

21
Linked Lists

Transcript of Linked Lists. Array Limitations Arrays have a fixed size that cannot be changed at run time What if...

Page 1: 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.

Linked Lists

Page 2: 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.

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…

Page 3: 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.

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

Page 4: 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.

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

Page 5: 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.
Page 6: 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.
Page 7: 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.
Page 8: 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.

but something is missing …

Page 9: 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.
Page 10: 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.

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

Page 11: 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.

JohnCop3223h

2014 raycot4123

2015

Jennycda2343

2014

deborahcnt5228

2016

Page 12: 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.

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

Page 13: 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.

Creating Links in a Linked List

typedef struct { char name[64]; char course[128]; int year; struct student_info *next;

} student_info;

Page 14: 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.

JohnCop3223h

2014*next

raycot4123

2015*next

Jennycda2343

2014*next

deborahcnt5228

2016*next

Page 15: 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.

• 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.

Page 16: 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.

JohnCop3223h

2014*next

raycot4123

2015*next

Jennycda2343

2014*next

deborahcnt5228

2016*next

First

Page 17: 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.

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

Page 18: 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.

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.

Page 19: 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.

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; }

Page 20: 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.

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");}

Page 21: 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.

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