Download presentation

Presentation is loading. Please wait.

Published byAliza Shore Modified over 2 years ago

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_ptr – a pointer to the next node in the list last node points to NULL Object next_ptr Ø Object next_ptr Object next_ptr

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_ptr Ø Object next_ptr Object next_ptr head tail

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

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

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

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

9
Insertion at Head Create new_node –store object in new_node Point new_node next_ptr 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_ptr to the node head points to Point head to new_node If tail points to NULL –point tail to new_node Ø head Object3 next_ptr new_nodetail

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

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

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

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

15
Exercises Recursively count number of nodes Reverse list Swap two 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_ptr next_ptr trailer Object2 prev_ptr next_ptr Object1 prev_ptr next_ptr header

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

18
Doubly Linked Lists Insertion Object3 prev_ptr next_ptr trailer Object2 prev_ptr next_ptr Object1 prev_ptr next_ptr header Object4 prev_ptr next_ptr insert here new_node

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

Similar presentations

OK

Department of Computer Science 1 Some Practice Let’s practice for the final a little bit. OK?

Department of Computer Science 1 Some Practice Let’s practice for the final a little bit. OK?

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on demographic structure of neighbourhood Ppt on limits and continuity ppt Ppt on mammals and egg laying animals name Maths ppt on surface area and volume free download Ppt on 9-11 conspiracy theories attacks Ppt on duty rosters Ppt on disk formatting mac Ppt on land and sea breeze Ppt on sources of energy for class 8th board Ppt on sugarcane industry