Академический Документы
Профессиональный Документы
Культура Документы
Copyrighted Material -Additional resource material supplied with the book Data Structures and Algorithms : Concepts, Techniques and Applications authored by G.A.V. Pai and published by
Tata McGraw Hill. This resource material is for Instructor's use only.
PROPRIETARY MATERIAL. 2008 The McGraw-Hill Companies, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced or distributed
in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill
for their individual course preparation. If you are a student using this PowerPoint slide, you are using it without permission.
Linked Lists
(Chapter 6)
Outline
Introduction
Singly Linked Lists
Circularly Linked Lists
Doubly Linked Lists
Multiply Linked Lists
Applications
ADT for links
ADT for singly linked lists
LINK
10
class Node {
String element;
Node next;
public Node (String e){
element = e;
}
}
public class linked_list {
public static void main(String[] args) {
Node head = null;
Node tail = null;
Node temp = null;
Node traverse=null;
temp = new Node ("KTrg");
tail=temp;
head=temp;
temp = new Node("Dungun");
temp.next=head;
head=temp;
traverse=head;
while (traverse != null){
System.out.println("traverse "+traverse.element);
traverse=traverse.next;
}
11
DELETE_SL(START, NODEX)
12
Locate Node
public Link find(int key) {
// find link with given key, assumes non-empty list
Link current = first; // start at first
while(current.iData != key) // while no match,
{
if(current.next == null) // if end of list,
return null; // didnt find it
else // not end of list,
current = current.next; // go to next link
}
return current; // found it
}
// -------------------------------------------------------------
13
Delete Node
public Link delete(int key) // delete link with given key
{ // (assumes non-empty list)
Link current = first; // search for link
Link previous = first;
while (current.iData != key)
{
if (current.next == null)
return null; // didnt find it
else
{
previous = current; // go to next link
current = current.next;
}
} // found it
if (current == first) // if first link,
first = first.next; // change first
else // otherwise,
previous.next = current.next; // bypass it
return current;
}
// -------------------------------------------------------------
14
15
16
17
Sorted List
18
19
20
accessibility of a node
delete operations
relative efficiency in the implementation of
list based operations
21
HEAD NODE
22
Each node has one or more data fields but only two link
fields termed left link (LLINK) and right link (RLINK).
23
DATA
LLINK
RLINK
The deletion of a node X from the list calls only for the
value X to be known.
Disadvantages of a doubly linked list
memory requirement
24
25
26
27
Applications
Addition of polynomials
Representation of a sparse matrix
28
Store a value of one link variable LINK1 to another link variable LINK2
STORE_LINK ( LINK1, LINK2)
29
30