Lecture-12-Linked Lists

advertisement
Lecture 12
Linked Lists
In this lecture
 Fundamentals




Applications
Memory Allocation
Creating a List
Inserting Nodes
Fundamentals of Linked Lists
Head
 A dynamic data structure
10
ff
20
aa
32
f1
40
01
NULL
 Head pointer indicates the beginning of the
list
 List traversal starts from the head
 NULL pointer indicates the end of the list
 List traversal ends at NULL or when the
goal is reached
Fundamentals of Linked Lists
 Three logically distinct forms of linked lists
– Empty List
Head
NULL
– List with only one node
Head
10
Head
– List with more than one node
NULL
ff
NULL
Types of Linked Lists
 Single
linked list
Head
NULL
This is the most basic type of linked list, with each
node containing a single pointer,
to the next node.
Head
 Multi
linked list
NULL
More advanced than the single linked list, each
node may be connected to many other nodes.
Special case : Doubly linked lists
 Circular
linked list
Head
With a circular linked list, the last node is
connected to the first, to form a circle.
Examples Linked Lists

Read a file and build a list of nodes in order. Insert a new node in
order. Delete a node. Search for a target node (single linked list)

Multi Linked List : The standard use of multi-linked lists is to
organize a collection of elements in many different ways. For
example, suppose my elements include the name of a person
and his/her age. e.g. (FRED,19) (MARY,16) (JACK,21)
(JILL,18) . I might want to order these elements alphabetically
and also order them by age. I would have two pointers - NEXTalphabetically, NEXT-age - and the list header would have two
pointers, one based on name, the other on age.
Age pointer
name pointer
Fred 19
Jack 21
Jill 18
Mary 16
NULL
NULL
Examples of Doubly Linked Lists



Sparse Matrices - common use
A sparse matrix is a matrix of numbers, in which almost all the
entries are zero.
These arise frequently in engineering applications . Typically only
about N elements are non-zero. For example:
0
0
27
19

88
0
0
0
0
0
0
66
We can represent this by having linked lists for each row and each
column. Because each node is in exactly one row and one column it
will appear in exactly two lists - one row list and one column. So it
needs two pointers: Next-in-this-row and Next-in-this-column. In
addition to storing the data in each node, it is normal to store the coordinates (i.e. the row and column the data is in in the matrix).
Operations that set a value to zero cause a node to be deleted, and
vice versa.
Sparse Matrix Implementation
Doubly Linked List - An Example
Class Node
class Node {
public Node();
public Node(int x) ;
public Node(int x, Node ptr) value
public Node getPtr();
public int getValue();
public void setPtr(Node ptr);
public void setValue(int) ;
private int value;
private Node next;
};
next
Implementing node methods
Constructors
Node() { value = 0; next = NULL;}
Node(int x) { value = x;}
Node(int x, Node ptr) {value = x; next = ptr;}
Accessors
Node getPtr() { return next;}
int getValue() { return value;}
Mutators
void setPtr(Node ptr) { next = ptr;}
void setValue(int) { value = next;}
Buliding a new Linked List





Assume that the node is an instance of the class of the following type:
class Node {
First
node(int);
int value;
20
Node next;
};
next
Node First; // defines the head
Allocate space : First = new Node(20);
For new nodes we must use another pointer
Otherwise what if we say First = new node(30)? First
No refer
30
20
next
next
Adding New Nodes

Define a new pointer
Node nextnode;
nextnode = new Node(30);
or Node nextnode=new Node(30);
nextnode
First
next

next
First
To make the link we must put the
nextnode in first.next
next
next
next
A Different Look at the Linked List
First
20
30
next
next
node 1 is referred to as First
node 2 is referred to as First.next

How do we refer to pointer field of node 2?

nextnode
Add another node to the end of the list. We
must use the public behavior Next() to access
the contents of private state next.
nextnode = new Node(40);
First
20
30
40
next
next
next
Inserting to the Middle
First
1
2
next
nextnode
next
3
next
1.5
next
We must make 1.next go to 1.5
 And 1.5.next go to 2. Is the ORDER important? Consider this
What is wrong ? Self-Reference

First
1
next
1.5
So what can we do?
First
nextnode

1
2
3
next
next
next
1.5
next
Therefore the correct order is
First
nextnode
1
2
next
next
1.5
next
Inserting to the Front
Suppose we have a constructor in the Node
class as follows:
Node(int n, Node ptr){
value = n; next = ptr;
}

Then inserting something to the front is easy.
If the list is empty, then first = new Node(n);
Else first = new Node(n, first)
Deleting a node from the list

We always need to know the node before the one we
need to delete. If we want to delete the target node
below...
2
First
target
next
1
3
next
next
Traversing a Linked List

Suppose we need to visit every node in a linked
list
First
1
2
3
next
next
next
Node temp = first;
while (temp != null)
{ System.out.println(temp.getValue());
temp = temp.next;
}

Download