4190'204 Data Structures Lecture 5 - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

4190'204 Data Structures Lecture 5

Description:

Needs shift operation for insertion/deletion. Also expensive in time. Linked list ... Since int is a primitive type, it cannot be an inherited class of Object. ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 39
Provided by: cs173
Category:

less

Transcript and Presenter's Notes

Title: 4190'204 Data Structures Lecture 5


1
4190.204 Data StructuresLecture 5
  • Bob McKay
  • School of Computer Science and Engineering
  • College of Engineering
  • Seoul National University
  • Partly based on
  • Carrano Prichard, Data Abstraction and Problem
    Solving with Java, Chapter 4
  • Lecture Notes of
  • Prof. Moon Byung Ro

2
Linked Lists
  • Array? list? ??? ?
  • Intuitively simple
  • Weak points
  • Overflow requires either
  • Overestimating the size so it doesnt happen
  • Expensive in memory
  • Resizing by copying
  • Expensive in time
  • Needs shift operation for insertion/deletion
  • Also expensive in time
  • Linked list
  • Free from overflow issues
  • Free from shift overhead
  • Overhead for linking

3
Figure 4.1 a) A linked list of integers b)
insertion c) deletion
4
JAVA Object References
Declaring reference variables
Allocating an object
Allocating another object, w/ the
dereferenced object marked for garbage
collection
Assigning a reference
5
Allocating an object
Assigning null to a reference variable
Assigning a reference with a null value
6
Call by Value
Integer x new Integer(9) changeNumber(x) //
attempts to assign 5 to x
public void changeNumber(Integer n) n new
Integer(5)
7
Linked List
  • Each node contains
  • Data (item)
  • Link

??, string,??,
8
A naïve structure
  • public class IntegerNode
  • public int item
  • public IntegerNode next
  • Not good information hiding
  • Not good data abstraction

??? IntegerNode n1 new IntegerNode( )
IntegerNode n2 new IntegerNode( ) n1.item
5 n2.item 9 n1.next n2
9
An Intermediate Version
??? IntegerNode n1 new IntegerNode( )
IntegerNode n2 new IntegerNode( )
n1.setItem(5) n2.setItem(9)
n1.setNext(n2)
public class IntegerNode private int
item private IntegerNode next public void
setItem(int newItem) item
newItem public int getItem( ) return
item public void setNext(IntegerNode
nextNode) next nextNode public
IntegerNode getNext( ) return next
10
An Improved Version
public class IntegerNode private int
item private IntegerNode next //
constructors public IntegerNode(int newItem)
item newItem next null public
IntegerNode(int newItem, IntegerNode nextNode)
item newItem next nextNode //
setItem, getItem, setNext, getNext as before
??? IntegerNode n2 new IntegerNode(9)
IntegerNode n1 new IntegerNode(5, n2)
11
Problems Still Remain
  • restricted to a single integer field
  • low reusability

public class IntegerNode private int
item private IntegerNode next public
IntegerNode(int newItem) item
newItem next null public
IntegerNode(int newItem, IntegerNode nextNode)
item newItem next nextNode
12
A Reusable Version
  • The Object class is
  • a superclass of every class

public class Node private Object
item private Node next public Node(Object
newItem) item newItem next
null public Node(Object newItem, Node
nextNode) item newItem next
nextNode public Object getItem( )
return item // setItem, setNext,
getNext similar
13
??? Node n new Node(new Integer(6)) Node
first new Node(new Integer(9), n)
  • Since int is a primitive type, it cannot be an
    inherited class of Object.
  • Integer is a class in java.lang.

14
Head Node
  • Generally, we want to access a linked list
    through a head reference

Node head null
Node head new Node(new Integer(5))
  • ? ??, head ? ??? reference variable??.

15
Displaying the Contents
  • Head? ???? linked list? ??? ??? display ??

for (Node curr head curr ! null curr
curr.getNext( )) System.out.println(curr.getIte
m( ))
16
Java Implementation
  • Public class Node
  • ?? ?? ??
  • public interface ListInterface
  • // list operators
  • public boolean isEmpty( )
  • public int size( )
  • public void add(int index, object item)
  • public void remove(int index)
  • public Object get(int index)
  • public void removeAll( )

17
public class ListReferenceBased implements
ListInterface private Node head private
int numItems // constructor public
ListReferenceBased( ) numItems 0 head
null // operations public boolean
isEmpty( ) return numItems
0 public int size( ) return
numItems
18
private Node find(int index) // return
reference to ith node Node curr head //
1st node for (int i 1 i lt index i)
curr curr.getNext( ) return
curr public Object get(int index) if
(index gt 1 index lt numItems) Node curr
find(index) Object dataItem
curr.getItem( ) return dataItem else
Exception ??
19
Deleting a Specified Node (at head or in middle)
  • ?? curr? ???? ??? ??
  • ?? ?? ??? prev? ???? ??? ??

prev.setNext(curr.getNext( ))
C??? prev-gtnext curr-gtnext
  • ??? ??? ??? ??

head curr.getNext( )
20
Inserting a Node (in middle)
  • ?? prev? curr? ???? ?? ??? ? ?? ??

C??? newNode malloc(sizeof Node) newNode-gtitem
30 newNode-gtnext curr prev.next newNode
newNode new Node(new Integer(30)) newNode.setNe
xt(curr) prev.setNext(newNode)
21
Inserting a Node (at head)
  • ??? ?? ?? ??? ??

newNode new Node(new Integer(2)) newNode.setNex
t(head) head newNode
C??? newNode malloc(sizeof Node) newNode-gtitem
2 newNode-gtnext head head newNode
22
Inserting a Node (at end)
  • ??? ?? ?? ??? ?? (not a special case)

newNode new Node(new Integer(102)) newNode.setN
ext(curr) prev.setNext(newNode)
curr ? null ?? ?? ??? ?? ??? ?? ?? ??
23
Deletion? ?? ??
curr
prev
public class ListReferenceBased implements
ListInterface private Node head private
int numItems public void remove(int index)
if (index gt 1 index lt numItems)
if (index 1) head head.getNext(
) else Node prev find(index
1) Node curr prev.getNext(
) prev.setNext(curr.getNext(
)) numItems-- else Exception
??
24
Insertion? ?? ??
public class ListReferenceBased implements
ListInterface private Node head private
int numItems public void add(int index,
Object item) if (index gt 1 index lt
numItems1) if (index 1) Node
newNode new Node(item, head) head
newNode else Node prev find(index
1) Node newNode new Node(item,
prev.getNext( )) prev.setNext(newNode)
numItems else Exception ??

prev
25
Passing a Linked List to a Method
aMethod(head)
26
Dummy Head Node
  • List? ??? dummy head node? ?? ??.
  • Head node? ??? ?? ??? ????? ???
  • Simplifies writing of code, because the first
    element in the list is not a special case any
    more
  • Initialization/Constructor

public class ListReferenceBased implements
ListInterface private Node head private
int numItems public ListReferenceBased( )
numItems 0 head new Node(
) head.setNext(null)
27
??. Without Dummy Node
  • Initialization/Constructor

public class ListReferenceBased implements
ListInterface private Node head private
int numItems public ListReferenceBased( )
numItems 0 head null
28
Deletion?? with Dummy Node? ?? ???
public class ListReferenceBased implements
ListInterface private Node head private
int numItems public void remove(int index)
if (index gt 1 index lt numItems)
Node prev find(index 1) Node curr
prev.getNext( ) prev.setNext(curr.getNext(
)) numItems-- else Exception ??

29
??. Without Dummy Node
public class ListReferenceBased implements
ListInterface private Node head private
int numItems public void remove(int index)
if (index gt 1 index lt numItems)
if (index 1) head head.getNext(
) else Node prev find(index
1) Node curr prev.getNext(
) prev.setNext(curr.getNext(
)) numItems-- else Exception
??
numItems--
30
Dummy Node? ??
  • ???? item ??? list ?? ??? ?? ? ??.
  • The dummy node can be used to hold important
    information about the list

Dummy head node
head
5
89
24
largest
length
smallest
public class ListInfo private int
length private Object smallestItem,
largestItem // methods for accessing the
private datalength, smallestItem,
largestItemappear here // end class
31
Doubly Linked List
  • Linked lists are very useful for searching
    forwards through a list
  • They arent so good for searching backwards
  • For example, if you want to delete the element
    matching a given item
  • You will need to keep an extra pointer so you can
    link up the previous element once you find the
    one you want to delete
  • Doubly linked lists simplify such operations by
    maintaining pointers in both directions

32
Circular Linked List
  • Circular lists can further simplify code by
    eliminating special cases

33
Deletion in Doubly Linked List
  • prev ??? ?? ?? ??.
  • ?/?? ? ??? ?? ??

curr.getPrecede( ).setNext(curr.getNext(
)) curr.getNext( ).setPrecede(curr.getPrecede(
))
  • ??? ?/?? ??? ?? ??? ?? ?? ??.

34
Deletion in Circular Doubly Linked List with
Dummy Head Node
Dummy head node
curr
  • ?/?? ??? ?? ??? ?? ?? ?? ??.

curr.getPrecede( ).setNext(curr.getNext(
)) curr.getNext( ).setPrecede(curr.getPrecede(
))
35
Insertion in Doubly Linked List
  • ?/?? ? ??? ?? ??

or
newNode new dListNode( curr.getPrecede( ),
curr, ) Curr.getPrecede( ).setNext(newNode) Cur
r.setPrecede(newNode)
newNode new dListNode() newNode.setNext(curr)
newNode.setPrecede(curr.getPrecede(
)) curr.getPrecede( ).setNext(newNode) curr.setP
recede(newNode)
  • ?/?? ??? ? ?? ?? ???

36
An Example Sparse Polynomial
-3x7 4x5 7x3 x2 9
Dummy head
terms
37
?? ?? (Nested Lists)
38
Summary
  • Linked lists implemented using references
  • The internal representation should be hidden
  • Can swap from singly to doubly linked lists
    without changing interface
  • Changes in detail of representation give
    different trade-offs in code simplicity / speed /
    space
Write a Comment
User Comments (0)
About PowerShow.com