Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE1301 Computer Programming: Lecture 33 Linked Lists.

Similar presentations


Presentation on theme: "CSE1301 Computer Programming: Lecture 33 Linked Lists."— Presentation transcript:

1 CSE1301 Computer Programming: Lecture 33 Linked Lists

2 Topics Linked list List element Searching a linked list

3 Recall: Sorted List 01234

4 01234 Ann

5 Recall: Sorted List 01234 Ann

6 Recall: Sorted List 01234 Ann Dave

7 Recall: Sorted List 01234 Ann Dave

8 Recall: Sorted List 01234 Ann Dave Humphry

9 Recall: Sorted List 01234 Ann Dave Humphry

10 Recall: Sorted List 01234 Ann Dave Humphry Ben

11 Recall: Sorted List 01234 Ann Dave Humphry Ben

12 Recall: Sorted List 01234 Ann Dave Humphry Ben

13 Recall: Sorted List 01234 Ann Dave Humphry Ben

14 Recall: Sorted List 01234 Ann Dave Humphry Ben Q: Is there an alternative way to implement a sorted list? A: Linked List

15 01234 Ann Dave Humphry Ben First

16 01234 Ann Dave Humphry Ben First

17 01234 Ann Dave Humphry Ben First

18 01234 Ann Dave Humphry Ben First

19 01234 Ann Dave Humphry First Ben

20 01234 Ann Dave Humphry Ben First

21 01234 Ann Dave Humphry Ben First

22 01234 Ann Dave Humphry Ben First Amy

23 01234 Ann Dave Humphry Ben First Amy

24 01234 Ann Dave Humphry Ben First Amy

25 01234 Ann Dave Humphry Ben First Amy

26 01234 first element index of next element item invalid index (eg. -1)

27 Linked List Uses an array to form a sorted list Each element has a link to the next item 82953 4 first 1 0123 54 53402

28 List Element struct ListElementRec { int item; int next; }; typedef struct ListElementRec ListElement; 2 item next 5

29 What will a program have? An array of these structs ListElement list[maxListLength]; A variable to store the position of the first element of the list int first; The last element in the list must contain a non valid integer in its next field const int END = -1;

30 Accessing list items First element (a struct) list[first]; First item in list list[first].item; Index of next element list[first].next;

31 Accessing list items Next item in list int index; list[list[index].next].item; index 4 82953 4 first 1 0123 54 53402 END

32 Searching in a linked list See Algorithm in Lecture Notes Example: Trace values for: index linkedList[index].item linkedList[index].next

33 Summary In a linked list, list elements don't have to be ordered in the array itself. A second field in the list element structure, "next", maintains the list ordering. Next lecture: adding and deleting from a linked list


Download ppt "CSE1301 Computer Programming: Lecture 33 Linked Lists."

Similar presentations


Ads by Google