418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Similar presentations

Presentation on theme: "418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains."— Presentation transcript:

1
418115: II

2
Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains a pointer to the next linked list element. It may also contain a pointer to the previous linked list element. In this case, we call it a doubly linked list element.

32
Implementing List with Linked List To simplify implementation, we will use two dummy elements to act as the first element and the last element of the list. These two dummies do not hold real data. Elements between them do.

42
Linked List Implementations Efficiency (cont.) Notice that how you specify the operations can have a lot of impact on the implementations efficiency. We can insert a linked list element into a linked list in O(1) if you know the element just before or after it. But, if we are given the position i to insert, it takes O(i) time just to get there.

43
Linked List Implementations Efficiency (cont.) How is linked list better than array? The space is O(n) at all time. More efficient use of memory. It performs some operation faster. Insert(x, 0) Remove(0) Both are O(1) in LinkedList, but O(n) in ArrayList. So, a queue implemented by a linked list takes O(1) per operation.