Linked List - PowerPoint PPT Presentation

View by Category
About This Presentation
Title:

Linked List

Description:

Linked List Introduction to Linked List Node Class Linked List The Bag Class with Linked List – PowerPoint PPT presentation

Number of Views:94
Avg rating:3.0/5.0
Slides: 60
Provided by: RKM8
Learn more at: http://www.rkmaruyama.net
Category:
Tags: doubly | link | linked | list

less

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

Title: Linked List


1
Linked List
  1. Introduction to Linked List
  2. Node Class
  3. Linked List
  4. The Bag Class with Linked List

2
Linked List
  • Linked List
  • Is a series of connected nodes, where each node
    is a data structure with data field and
    pointer(s) field.
  • Advantages over array implementation
  • Can grow and shrink in size, with no upper limit
  • Fast insertion and deletion

3
Declarations for Linked Lists
  • For this presentation, nodes in a linked list are
    objects, as shown here.

15
data_field
class node public typedef double
value_type ... private value_type
data_field node link_field
link_field
10
data_field
7
data_field
link_field
null
link_field
4
typedef Keyword
  • typedef int age_ttypedef int count_ttypedef
    double gpa_tage_t myAge 21count_t
    numElements 5gpa_t myGPA 3.8
  • void someFunc(age_t age, count_t count)is
    clearer than void someFunct(int age, int count)

5
Why Use typedef?
  • Using typedefs can make your code clearer
  • Using ypedefs can make your code easier to modify

6
Declarations for Linked Lists
  • The data_field of each node is a type called
    value_type, defined by a typedef.

15
data_field
class node public typedef int value_type
... private value_type data_field node
link_field
link_field
10
data_field
7
data_field
link_field
null
link_field
7
Declarations for Linked Lists
  • Each node also contains a link_field which is a
    pointer to another node.

15
data_field
class node public typedef int value_type
... private value_type data_field node
link_field
link_field
10
data_field
7
data_field
link_field
null
link_field
8
Your Turn
  • Write the public interface for the Node
    classe.g, Constructors, accessors, modifiers.

9
Solution
class node public //CONSTRUCTORS
node() node(const valueType value, node
ptr) //CONSTANT FUNCTIONS valueType
getData() node getNext() //MODIFIER
FUNCTIONS void setData(valueType newData)
void setLink(node newLink) private
valueType data node next
10
Declarations for Linked Lists
  • A program can keep track of the front node by
    using a pointer variable such as head_ptr in this
    example.
  • Notice that head_ptr is not a node -- it is a
    pointer to a node.

15
data_field
link_field
10
data_field
7
data_field
link_field
null
head_ptr
link_field
11
Declarations for Linked Lists
  • A program can keep track of the front node by
    using a pointer variable such as head_ptr.
  • Notice that head_ptr is not a node -- it is a
    pointer to a node.
  • We represent the empty list by storing null in
    the head pointer.

null
head_ptr
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_ptr
13
Inserting a Node at the Front
  • Create a new node, pointed to by a local variable
    insert_ptr.

insert_ptr
15
10
7
null
head_ptr
14
Inserting a Node at the Front
  • insert_ptr new node

insert_ptr
15
10
7
null
head_ptr
15
Inserting a Node at the Front
insert_ptr
13
15
10
7
13
null
entry
head_ptr
16
Inserting a Node at the Front
insert_ptr
13
15
10
7
13
null
entry
head_ptr
17
Inserting a Node at the Front
insert_ptr
13
  • The correct new node can be completely created in
    one step by calling an appropriate node
    constructor.

15
10
7
13
null
entry
head_ptr
18
Inserting a Node at the Front
insert_ptr
13
15
10
7
13
null
entry
head_ptr
19
Inserting a Node at the Front
insert_ptr
13
15
10
7
13
null
entry
head_ptr
20
Inserting a Node at the Front
  • insert_ptr new node(entry, head_ptr)
  • head_ptr insert_ptr

13
When the function returns, the linked list has a
new node at the front.
15
10
7
null
head_ptr
21
Inserting a Node at the Front
void list_head_insert(node head_ptr, const
nodevalue_type entry) node
insert_ptr insert_ptr new node(entry,
head_ptr) head_ptr insert_ptr
22
Inserting a Node at the Front
void list_head_insert(node head_ptr, const
nodevalue_type entry) node
insert_ptr insert_ptr new node(entry,
head_ptr) head_ptr insert_ptr
Does the function work correctly for the empty
list ?
23
Inserting a Node at the Front
void list_head_insert(node head_ptr, const
nodevalue_type entry) node
insert_ptr insert_ptr new node(entry,
head_ptr) head_ptr insert_ptr
Does the function work correctly for the empty
list ?
13
null
entry
head_ptr
24
Inserting a Node at the Front
void list_head_insert(node head_ptr, const
nodevalue_type entry) node
insert_ptr insert_ptr new node(entry,
head_ptr) head_ptr insert_ptr
insert_ptr
13
13
null
entry
null
head_ptr
25
Inserting a Node at the Front
void list_head_insert(node head_ptr, const
nodevalue_type entry) node
insert_ptr insert_ptr new node(entry,
head_ptr) head_ptr insert_ptr
insert_ptr
13
13
entry
null
head_ptr
26
Inserting a Node at the Front
void list_head_insert(node head_ptr, const
nodevalue_type entry) node
insert_ptr insert_ptr new node(entry,
head_ptr) head_ptr insert_ptr
When the function returns, the linked list has
one node.
13
null
head_ptr
27
Caution!
  • Always make sure that your linked list functions
    work correctly with an empty list.

28
Pseudocode for Inserting Nodes
  • Nodes are often inserted at places other than the
    front of a linked list.
  • There is a general pseudocode that you can follow
    for any insertion function. . .

29
Pseudocode for Inserting Nodes
  • Determine whether the new node will be the first
    node in the linked list. If so, then there is
    only one step

30
Pseudocode for Inserting Nodes
  • Determine whether the new node will be the first
    node in the linked list. If so, then there is
    only one step

31
Pseudocode for Inserting Nodes
  • Determine whether the new node will be the first
    node in the linked list. If so, then there is
    only one step

A pointer to the head of the list
32
Pseudocode for Inserting Nodes
  • Determine whether the new node will be the first
    node in the linked list. If so, then there is
    only one step

33
Pseudocode for Inserting Nodes
34
Pseudocode for Inserting Nodes
previous_ptr
10
15
7
null
head_ptr
35
Pseudocode for Inserting Nodes
previous_ptr
10
15
7
  • What is the name of this orange pointer ?

null
head_ptr
36
Pseudocode for Inserting Nodes
previous_ptr
10
15
7
  • What is the name of this orange pointer ?

null
head_ptr
37
Pseudocode for Inserting Nodes
previous_ptr
10
15
7
null
head_ptr
38
Pseudocode for Inserting Nodes
13
previous_ptr
10
15
7
  • Write one C statement which will do the
    insertion.

null
head_ptr
39
Pseudocode for Inserting Nodes
13
previous_ptr
10
15
7
  • What might cause this statement to fail to
    compile?

null
head_ptr
40
Pseudocode for Inserting Nodes
13
previous_ptr
10
15
7
  • Use a node member function to get the link field
    if needed.

null
head_ptr
41
Pseudocode for Inserting Nodes
  • Determine whether the new node will be the first
    node in the linked list. If so, then there is
    only one step
  • Otherwise (if the new node will not be first)
  • Set a pointer named previous_ptr to point to the
    node which is just before the new node's
    position.
  • Make the function call

42
Pseudocode for Inserting Nodes
  • The process of adding a new node in the middle of
    a list can also be incorporated as a separate
    function. This function is called list_insert in
    the linked list toolkit of Section 5.2.

43
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.

44
Removing the Head Node
remove_ptr
head_ptr
45
Removing the Head Node
Draw the change that this statement will make to
the linked list.
remove_ptr
head_ptr
46
Removing the Head Node
remove_ptr
head_ptr
47
Removing the Head Node
remove_ptr
head_ptr
48
Removing the Head Node
head_ptr
49
Summary
  • It is easy to insert a node at the front of a
    list.
  • The linked list toolkit also provides a function
    for inserting a new node elsewhere
  • It is easy to remove a node at the front of a
    list.
  • The linked list toolkit also provides a function
    for removing a node elsewhere--you should read
    about this function and the other functions of
    the toolkit.

50
Doubly Linked List
O
head
O
class list . . .private node head
int count
class node . . .private elementType
data node next node previous
51
insertAtFront()
52
insertAtFront()
void listinsertAtFront(valueType item)
node temp new node(item, NULL, NULL) if
(isEmpty()) head temp else
temp-gtsetNext(head) head-gtsetPrevious(temp)
head temp count
53
removeFromFront()
temp
O
O
head
O
54
removeFromFront()
valueType listremoveFromFront() valueType
result NIL if (!isEmpty())
result head-gtgetData() node temp
head head-gtsetPrevious(NULL) head
head-gtgetNext() delete temp
count-- return result
55
insertAtBack()
temp
O
head
temp
newPtr
O
56
insertAtBack()
void listinsertAtBack(valueType item)
node newPtr new node(item, NULL, NULL) //
find the last node if (isEmpty()) head
newPtr else node temp head
while (temp-gtgetNext() ! NULL)
temp temp-gtgetNext()
temp-gtsetNext(newPtr) newPtr-gtsetPrevious(t
emp) count
57
removeFromBack()
temp
O
O
head
O
58
removeFromBack()
valueType listremoveFromBack() valueType
result NIL if (!isEmpty()) //
find last node node temp head
while (temp-gtgetNext() ! NULL)
temp temp-gtgetNext() // remove
last node temp-gtgetPrevious()-gtsetNext(NULL)
delete temp return result
59
Tail Pointer (to the last node)
tail
head
O
class list . . .private node head
node tail int count
class node . . .private elementType
data node next
About PowerShow.com