Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.

Similar presentations


Presentation on theme: "Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked."— Presentation transcript:

1 Linked Lists

2 Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked list What are the disadvantages of using a linked list?

3 Linked List Node – one element of the linked list –Object – data stored in the node – examples? –next – a reference to the next node in the list last node points to NULL Object next Ø Object next Object next

4 Linked List head keeps track of the head of the list tail keeps track of the last node in the list –tail not always used Object next Ø Object next Object next head tail

5 Insertion at Head Object1 next Ø head Object2 next Object3 next Insert here new_node tail

6 Insertion at Head Create new_node –store object in new_node Point new_node next to the node head points to Object1 next Ø head Object2 next Object3 next new_node tail

7 Insertion at Head Create new_node –store object in new_node Point new_node next to the node head points to Point head to new_node Object1 next Ø head Object2 next Object3 next new_node tail

8 Insertion at Head Does this algorithm work for the list below? Object1 next Ø head Object2 next Object3 next Ø head Object3 next new_node tail

9 Insertion at Head Create new_node –store object in new_node Point new_node next to the node head points to Point head to new_node If tail points to NULL –point tail to new_node

10 Insertion at Head Create new_node –store object in new_node Point new_node next to the node head points to Point head to new_node If tail points to NULL –point tail to new_node Ø head Object3 next new_nodetail

11 Insertion at Tail Ø head Object3 next new_nodetail Object1 next Ø head Object2 next Object3 next Insert here tail new_node

12 Find find(3) find(16) - always remember to deal with special cases 5 next Ø 3 12 next head tail

13 Deletion Deletion of head –Complexity? Deletion of tail –Complexity? Object1 next Ø head Object2 next Object3 next tail

14 Insertion/Deletion in Middle Insert between Object1 and Object2 Delete Object1 Object1 next Ø head Object2 next Object3 next tail

15 Exercises Implement a method to recursively count number of nodes in a linked list Implement a method to reverse a linked list Implement a method that takes as input two integers representing node positions and swaps the specified nodes

16 Doubly Linked Lists Each node keeps a pointer to the next node and to the previous node –Makes some operations (such as insertion at end) more efficient –Costs? At the beginning and end of the list are sentinel nodes –Simplify insertion/deletion algorithm Object3 prev next trailer Object2 prev next Object1 prev next header

17 Doubly Linked Lists Insertion and deletion at beginning/end Insertion and deletion in middle Object3 prev next trailer Object2 prev next Object1 prev next header trailerheader

18 Doubly Linked Lists Insertion Object3 prev next trailer Object2 prev next Object1 prev next header Object4 prev next insert here new_node

19 Doubly Linked Lists Insertion at head 1.Set next of new_node to point to what header’s next points to 2.Set prev of node that header’s next points to to point to new_node 3.Set prev of new_node to point to header 4.Set header’s next to point to new_node Number 1 must come before number 4 Insertion at trailer? Deletion? Object3 prev next trailer Object2 prev next Object1 prev next header Object4 prev next insert here new_node


Download ppt "Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked."

Similar presentations


Ads by Google