Linked Lists - PowerPoint PPT Presentation

Loading...

PPT – Linked Lists PowerPoint presentation | free to download - id: 20a618-YmM4N



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Linked Lists

Description:

1. Linked Lists. It's a conspiracy! 2. Linked list: a data structure used to ... null); i ) cursor = cursor.link; return cursor; 33. Linked Lists. It's a conspiracy! ... – PowerPoint PPT presentation

Number of Views:1454
Avg rating:3.0/5.0
Slides: 34
Provided by: catesh
Learn more at: http://www.kirkwood.edu
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Linked Lists


1
Linked Lists
  • Its a conspiracy!

2
Linked list a data structure used to represent
an ordered list
  • Consists of a sequence of nodes
  • A node consists of a data item and a reference to
    the next node -- the connecting reference is
    called a link
  • Links keep nodes in sequence
  • Last nodes link is assigned null
  • Entire list is accessed via the first node in the
    list, usually called the head node

3
Node structure
  • public class Node
  • private int data
  • private Node link
  • …
  • Data type is for data member declared as int as
    an example could be any type (and soon will be)
  • Node contains a reference to itself -- actual use
    is as reference to another Node

4
Portrait of a linked list
  • List begins with head node
  • keeps track of list front
  • may or may not contain data if it doesnt, its
    a dummy node
  • A list may also have a tail node to keep track of
    the end of the list could also be a dummy

5
Portrait of a linked list
Head and tail nodes illustrated here are
dummies they have link members that point to
other nodes, contain no data
tail
head
NULL
6
Accessing list members
  • Every node has a link through which it can be
    accessed this is the link member of the
    previous node
  • For example, in the list from the previous slide,
    head.link refers to the first node in the list
  • the data in the first node is head.link.data
  • head.link.link is a reference to the second node

7
Operations on Nodes
  • Constructor creates Node with specified data and
    link
  • Accessors mutators for data and link

8
Constructors
public Node(int initialData, Node initialLink)
data initialData link
initialLink public Node () data
0 link null
9
Accessors Mutators
  • public int getData ()
  • return data
  • public Node getLink()
  • return link
  • public void setData (int newData)
  • data newData
  • public void setLink (Node newLink)
  • link newLink

10
Operations on Linked Lists
  • Inserting a node
  • at the front
  • at any position other than the front
  • Removing a node
  • from the front
  • from any other position
  • Removing all nodes

11
Operations on Linked Lists
  • Finding the length of a list
  • Finding a target value in a list
  • Finding a value at a specified position in a list
  • Making a copy of a list
  • Making a copy of part of a list

12
Inserting a Node at the Front
  • We want to add a new entry, 13, to the front of
    the linked list shown here.

15
10
7
13
null
entry
head
13
Inserting a Node at the Front
  • head new Node(13, head)
  • first argument is data for new node
  • second argument is link for new node points to
    node that used to be at front of list

13
15
When the function returns, the linked list has a
new node at the front, containing 13.
10
7
null
head
14
Caution!
  • Always make sure that your linked list methods
    work correctly with an empty list.

EMPTY LIST
15
Pseudocode for Removing Nodes
  • Nodes often need to be removed from a linked
    list.
  • As with insertion, there is a technique for
    removing a node from the front of a list, and a
    technique for removing a node from elsewhere.
  • Well look at the pseudocode for removing a node
    from the front of a linked list.

16
Removing the Head Node
Removal of the first node is easily accomplished
with the following instruction head
head.getLink()
head
17
Removing the Head Node
Heres what the linked list looks like after the
removal finishes. Automatic garbage collection
takes care of removing the node that is no longer
part of the list.
head_ptr
18
Inserting nodes elsewhere in the list
  • The head node is the only node to which we have
    direct access, unless we also maintain a tail
    node
  • Even if this is the case, however, the task of
    adding a node anywhere besides the front is a bit
    complicated, since we need access to the node
    that precedes the spot where the new node is to
    be added

19
Pseudocode for Inserting Nodes
We begin with the assumption that we already have
a reference to the Node just before the one we
want to add (labelled previous_ptr in the
illustration)
In this example, the new node will be the second
node
previous_ptr
10
15
7

null
head
20
Pseudocode for Inserting Nodes
Look at the pointer which is in the node
referenced by previous_ptr
This pointer is called previous_ptr.link
10
previous_ptr
15
7
null
head
21
Pseudocode for Inserting Nodes
previous_ptr.link points to the head of a small
linked list, with 10 and 7
10
previous_ptr
15
7
null
head
22
Pseudocode for Inserting Nodes
  • previous_ptr.setLink (new Node(13,
    previous_ptr.getLink))

13
The new node must be inserted at the front of
this small linked list.
10
previous_ptr
15
7
null
head
23
Node insertion the previous example
  • The code on the previous slide isnt part of the
    Node class described in the textbook, although
    the principle is discussed there
  • The previous slides approach to adding a Node in
    the middle of the list is an example of recursive
    thinking about a linked list
  • every linked list has a head node
  • every node is the head node of a linked list
  • the very last node in the list is the head node
    of an empty list

24
Same approach, different code
Here is the code provided in the textbooks Node
class its the same approach, wrapped in a
method public void addNodeAfter(int item)
link new Node(item, link) Here, the
calling object (of type Node) takes the place of
previous_ptr.link in the first example
25
Removing a Node from elsewhere in the list
  • If we want to remove a node from somewhere
    besides the front of the list, the process is a
    bit more complicated
  • As with list insertion, the key idea is to make
    sure that we never lose our grip on the rest of
    the list while were making changes to a
    particular node
  • Again, we will assume for the moment that we
    already have a reference to the node previous to
    the one we want to remove

26
Removing a node that is not at the head
  • As with insertion, we could accomplish this task
    with an instruction like the following
  • previous_ptr.setLink (previous_ptr.getLink().getLi
    nk())
  • or by using the method in the Node class
  • public void removeNodeAfter( ) link
    link.link

27
Methods that operate on an entire list
  • This set of methods includes finding the length
    of list, searching for a value in the list, and
    copying all or part of a list
  • These methods are declared static because they
    operate on lists, not on Nodes
  • Static methods can operate on any linked list,
    including an empty list

28
Code for listLength
  • public static int listLength(Node head)
    Node cursor int answer answer
    0 for (cursor head cursor ! null
    cursor cursor.link) answer
    return answer

29
Finding a target value in a list
  • Traverse list, using same strategy as in
    listLength function
  • If target is found, return pointer to its node
  • If target is not found and cursor reaches end of
    list, return null

30
Code for list_search
  • public static Node listSearch(Node head, int
    target) Node cursor for
    (cursor head cursor ! null cursor
    cursor.link) if (target cursor.data)
    return cursor return
    null

31
Finding value at specific location
  • Traverse list to current position, or until
    cursor is null
  • Return value of cursor once list has been
    traversed

32
Code for list_locate
  • public static Node listPosition(Node head, int
    position) Node cursor int i
    if (position lt 0) throw new
    IllegalArgumentException
  • ("position is not positive") cursor
    head for (i 1 (i lt position) (cursor
    ! null) i) cursor cursor.link
    return cursor

33
Linked Lists
  • Its a conspiracy!
About PowerShow.com