Top Banner
CSE1301 Computer Programming: Lecture 33 Linked Lists
33

CSE1301 Computer Programming: Lecture 33 Linked Lists.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSE1301 Computer Programming: Lecture 33 Linked Lists.

CSE1301 Computer Programming:

Lecture 33Linked Lists

Page 2: CSE1301 Computer Programming: Lecture 33 Linked Lists.

Topics

• Linked list

• List element

• Searching a linked list

Page 3: CSE1301 Computer Programming: Lecture 33 Linked Lists.

Recall: Sorted List

0 1 2 3 4

Page 4: CSE1301 Computer Programming: Lecture 33 Linked Lists.

Recall: Sorted List

0 1 2 3 4

Ann

Page 5: CSE1301 Computer Programming: Lecture 33 Linked Lists.

Recall: Sorted List

0 1 2 3 4

Ann

Page 6: CSE1301 Computer Programming: Lecture 33 Linked Lists.

Recall: Sorted List

0 1 2 3 4

Ann

Dave

Page 7: CSE1301 Computer Programming: Lecture 33 Linked Lists.

Recall: Sorted List

0 1 2 3 4

Ann Dave

Page 8: CSE1301 Computer Programming: Lecture 33 Linked Lists.

Recall: Sorted List

0 1 2 3 4

Ann Dave

Humphry

Page 9: CSE1301 Computer Programming: Lecture 33 Linked Lists.

Recall: Sorted List

0 1 2 3 4

Ann Dave Humphry

Page 10: CSE1301 Computer Programming: Lecture 33 Linked Lists.

Recall: Sorted List

0 1 2 3 4

Ann Dave Humphry

Ben

Page 11: CSE1301 Computer Programming: Lecture 33 Linked Lists.

Recall: Sorted List

0 1 2 3 4

Ann Dave Humphry

Ben

Page 12: CSE1301 Computer Programming: Lecture 33 Linked Lists.

Recall: Sorted List

0 1 2 3 4

Ann Dave Humphry

Ben

Page 13: CSE1301 Computer Programming: Lecture 33 Linked Lists.

Recall: Sorted List

0 1 2 3 4

Ann Dave HumphryBen

Page 14: CSE1301 Computer Programming: Lecture 33 Linked Lists.

Recall: Sorted List

0 1 2 3 4

Ann Dave HumphryBen

Q: Is there an alternative way to implement a sorted list?

A: Linked List

Page 15: CSE1301 Computer Programming: Lecture 33 Linked Lists.

0 1 2 3 4

Ann Dave Humphry

Ben

First

Page 16: CSE1301 Computer Programming: Lecture 33 Linked Lists.

0 1 2 3 4

Ann Dave Humphry

Ben

First

Page 17: CSE1301 Computer Programming: Lecture 33 Linked Lists.

0 1 2 3 4

Ann Dave Humphry

Ben

First

Page 18: CSE1301 Computer Programming: Lecture 33 Linked Lists.

0 1 2 3 4

Ann Dave Humphry

Ben

First

Page 19: CSE1301 Computer Programming: Lecture 33 Linked Lists.

0 1 2 3 4

Ann Dave Humphry

First

Ben

Page 20: CSE1301 Computer Programming: Lecture 33 Linked Lists.

0 1 2 3 4

Ann Dave Humphry Ben

First

Page 21: CSE1301 Computer Programming: Lecture 33 Linked Lists.

0 1 2 3 4

Ann Dave Humphry Ben

First

Page 22: CSE1301 Computer Programming: Lecture 33 Linked Lists.

0 1 2 3 4

Ann Dave Humphry Ben

First

Amy

Page 23: CSE1301 Computer Programming: Lecture 33 Linked Lists.

0 1 2 3 4

Ann Dave Humphry Ben

First

Amy

Page 24: CSE1301 Computer Programming: Lecture 33 Linked Lists.

0 1 2 3 4

Ann Dave Humphry Ben

First

Amy

Page 25: CSE1301 Computer Programming: Lecture 33 Linked Lists.

0 1 2 3 4

Ann Dave Humphry Ben

First

Amy

Page 26: CSE1301 Computer Programming: Lecture 33 Linked Lists.

0 1 2 3 4

first element

index of next element

item

invalid index (eg. -1)

Page 27: CSE1301 Computer Programming: Lecture 33 Linked Lists.

Linked List

• Uses an array to form a sorted list

• Each element has a link to the next item

8 2 9 5 34

first 10 1 2 3 54

5 34 02 -1

Page 28: CSE1301 Computer Programming: Lecture 33 Linked Lists.

List Element

struct ListElementRec

{

int item;

int next;

};

typedef struct ListElementRec ListElement;

2item

next 5

Page 29: CSE1301 Computer Programming: Lecture 33 Linked Lists.

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 fieldconst int END = -1;

Page 30: CSE1301 Computer Programming: Lecture 33 Linked Lists.

Accessing list items

• First element (a struct) list[first];

• First item in list list[first].item;

• Index of next element list[first].next;

Page 31: CSE1301 Computer Programming: Lecture 33 Linked Lists.

Accessing list items

• Next item in list

int index;

list[list[index].next].item; index 4

8 2 9 5 34

first 10 1 2 3 54

5 34 02 END

Page 32: CSE1301 Computer Programming: Lecture 33 Linked Lists.

Searching in a linked list

• See Algorithm in Lecture Notes

• Example: Trace values for:index

linkedList[index].item

linkedList[index].next

Page 33: CSE1301 Computer Programming: Lecture 33 Linked Lists.

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