Linked Lists - PowerPoint PPT Presentation

About This Presentation
Title:

Linked Lists

Description:

Title: Data Structures Author: Sami Rollins Last modified by: Sami Rollins Created Date: 9/12/1999 10:01:37 PM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 20
Provided by: SamiRo3
Learn more at: https://www.cs.usfca.edu
Category:
Tags: linked | lists | trailer

less

Transcript and Presenter's Notes

Title: Linked Lists


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
Object
Object
Object
next
next
next
Ø
  • 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

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

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

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

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

new_node
tail
Ø
head
Object3
next
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
new_node
tail
Ø
head
Object3
next
  • 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

11
Insertion at Tail
tail
Object1
Object2
head
next
next
Ø
Object3
Insert here
new_node
next
new_node
tail
Ø
head
Object3
next
12
Find
tail
5
3
12
head
next
next
next
Ø
  • find(3)
  • find(16) - always remember to deal with special
    cases

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

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

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
Object3
Object2
Object1
header
prev
prev
prev
trailer
next
next
next
  • 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

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

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

19
Doubly Linked Lists
new_node
Object4
prev
insert here
next
Object3
Object2
Object1
header
prev
prev
prev
trailer
next
next
next
  • Insertion at head
  • Set next of new_node to point to what headers
    next points to
  • Set prev of node that headers next points to to
    point to new_node
  • Set prev of new_node to point to header
  • Set headers next to point to new_node
  • Number 1 must come before number 4
  • Insertion at trailer?
  • Deletion?
Write a Comment
User Comments (0)
About PowerShow.com