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 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:

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?

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

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

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

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

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

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

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

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

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

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

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

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

Exercises Recursively count number of nodes Reverse list Swap two nodes

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

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

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

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

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