Download presentation
Presentation is loading. Please wait.
Published byBrittany Parks Modified over 9 years ago
1
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: http://people.math.yorku.ca/~zyang/it ec2620m.htm Office: TEL 3049
2
Linked List
3
3 Key Points of this Lecture Maintaining data records Inserting into sorted arrays Dynamic data structures Linked-lists
4
4 Inserting into Sorted Arrays How is this done? –loop – larger values get moved into the next slot –when done, copy new value into next slot What is the complexity for inserting into a sorted array? –Is there a best, worst, and average case? do you always insert into the same location? –How many times, what does it cost? Best 0 moves O(1) Worst n moves, 1 swap per O(n) Avg n/2 moves, 1 swap per O(n)
5
5 Inserting into Full Arrays What happens if we want to insert another element into this array? –Copy entire array into a larger array (time consuming) How do we avoid this? –Store data in an array with extra space Time-Space trade-off –What saves time can often waste space –What saves space can often waste time
6
6 Dynamic Data Structures Would like to create extra memory slots one at a time… Creating Links Inserting Links
7
7 Arrays vs. Linked-Lists Arrays –fixed length –contiguous memory locations –direct access Linked-Lists –dynamic length –arbitrary memory locations –access by following links
8
8 Comparing Arrays and Linked- Lists Find and delete a value in a sorted array and a linked-list –Arrays find value with binary search (assume value is in array) shuffle all higher values down what is the complexity for find? –Best O(1) –Worst O( log n) –Avg O( log n)
9
9 Comparing Arrays and Linked- Lists (Cont’d) Array –What is the complexity for delete? Best last element O(1) Worst first element O(n) Avg middle element O(n) –What is the overall complexity? Best last element O( log n)+O(1) = O( log n) Worst first element O( log n)+O(n) = O(n) Avg mid-area element O(logn)+O(n) = O(n)
10
10 Comparing Arrays and Linked- Lists (Cont’d) Linked-Lists –find value by following links –link past target link (garbage collecting will remove it) –what is the complexity for find? Best first element O(1) Worst last element O(n) Avg middle element O(n)
11
11 Comparing Arrays and Linked- Lists (Cont’d) What is the complexity for delete? –Always O(1) What is the overall complexity? –Best first element O(1)+O(1) = O(1) –Worst last element O(n)+O(1) = O(n) –Avg middle element O(n)+O(1) = O(n)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.