Вы находитесь на странице: 1из 30

@ McGraw-Hill Education

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

Sequential data structures in general,


suffer from the following drawbacks:

Inefficient implementation of insertion and


deletion operations

Inefficient use of storage memory

A linked representation serves to counteract the


drawbacks of sequential representation by exhibiting
the following merits:

Efficient implementation of insertion and deletion


operations.
Unlike sequential data structures, there is complete
absence of data movement of neighbouring
elements during the execution of these operations.

Efficient use of storage memory.


The operation and management of linked data
structures are less prone to instigate memory

A linked representation of data structure known as a


linked list is a collection of nodes.

Each node is a collection of fields categorized as data


items and links.

The data item fields hold the information content or


data to be represented by the node.

The link fields hold the addresses of the neighbouring


nodes or of those nodes which are associated with the

Implementation of linked lists

to frame chunks of memory into nodes with


the desired number of data items and fields

to determine which nodes are free and which


have been allotted for use

to obtain nodes from the free storage area or


storage pool for use GETNODE(X)

In java, to return or dispose nodes from the


reserved area or pool to the free area after use
will be done automatically by JVM.

Singly Linked Lists

A singly linked list is a linear data structure


each node of which has one or more data item
fields (DATA) but only a single link field
(LINK)
DATA

LINK

Insertion in a singly linked list


Algorithm 6.1 : To insert a data element ITEM
in a non empty singly liked list START, to
the right of node NODE.
Procedure INSERT_SL(START, ITEM, NODE)
/* Insert ITEM to the right of node NODE in
the list START */
Call GETNODE(X);
DATA(X) = ITEM;
LINK(X) = LINK(NODE);
Node X points to
the original right neighbour
of node NODE */
LINK(NODE) = X;
end INSERT_SL.

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

Deletion in a singly linked list


Algorithm 6.3 : Deletion of a node which is
to the right of node NODEX in a singly
linked list START
Procedure

DELETE_SL(START, NODEX)

if (START = NIL) then


Call ABANDON_DELETE;
/*ABANDON_DELETE terminates the delete
operation */
else
{TEMP = LINK(NODEX);
LINK(NODEX) = LINK(TEMP);
Call RETURN(TEMP); }
end DELETE_SL.

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

Insert First in double-headed list


public void insertFirst(long dd) // insert at front of list
{
Link newLink = new Link(dd); // make new link
if ( isEmpty() ) // if empty list,
last = newLink; // newLink <-- last
newLink.next = first; // newLink --> old first
first = newLink; // first --> newLink
}
Insert Last in double-headed list
public void insertLast(long dd) // insert at end of list
{
Link newLink = new Link(dd); // make new link
if ( isEmpty() ) // if empty list,
first = newLink; // first --> newLink
else
last.next = newLink; // old last --> newLink
last = newLink; // newLink <-- last
}

16

Singly linked list Stack implementation


Double-headed linked list Queue implementation.

17

Sorted List

18

19

Circularly Linked Lists

For further improvement in processing of a


singly linked list one may replace the null pointer
in the last node with the address of the first node
in the list.
Such a list is called as a circularly linked list or a
circular linked list or simply a circular list.

20

Advantages of circularly linked list

accessibility of a node
delete operations
relative efficiency in the implementation of
list based operations

Disadvantages of circularly linked lists


getting into an infinite loop owing to the
circular nature of pointers in the list
solution to this problem is to designate a
special node to act as the head of the list,
known as list head or head node

21

headed circularly linked list or circularly


linked list with head node.

HEAD NODE

22

Doubly Linked Lists

To enhance greater flexibility of movement, the linked


representation could include two links in every node, each
of which points to the nodes on either side of the given node

Such a linked representation known as doubly linked list

Each node has one or more data fields but only two link
fields termed left link (LLINK) and right link (RLINK).

The LLINK field of a given node points to the node on its


left and its RLINK field points to the one on its right.

A doubly linked list may or may not have a head node.


Again, it may or may not be circular.

23

DATA

LLINK

RLINK

Advantages of a doubly linked list

The availability of two links LLINK and RLINK permit


forward and backward movement during the processing
of the list.

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

Insertion in a doubly linked list


Algorithm 6.4 : To insert node X to the
right of node Y in a headed circular
doubly linked list P
Procedure INSERT_DL(X, Y)
LLINK(X) = Y;
RLINK(X) = RLINK(Y);
LLINK(RLINK(Y)) = X;
RLINK(Y) = X;
end INSERT_DL.

25

Deletion in a doubly linked list


Algorithm 6.5 : Delete node X from a
headed circular doubly linked list P
procedure DELETE_DL(P, X)
if (X = P) then ABANDON_DELETE;
else
{RLINK(LLINK(X)) = RLINK(X);
LLINK(RLINK(X)) = LLINK(X);
Call RETURN(X); }
end DELETE_DL.

26

Multiply Linked Lists

A multiply linked list as its name suggests is a


linked representation with multiple data and
link fields

27

Applications

Addition of polynomials
Representation of a sparse matrix

28

ADT for Links


Data objects:
Addresses of the nodes holding data and null links
Operations:

Allocate node (address X) from Available Space to accommodate data


GETNODE ( X)

Return node (address X) after use to Available Space


RETURN(X)

Store a value of one link variable LINK1 to another link variable LINK2
STORE_LINK ( LINK1, LINK2)

Store ITEM into a node whose address is X


STORE_DATA (X, ITEM)

Retrieve ITEM from a node whose address is X


RETRIEVE_DATA (X, ITEM)

29

ADT for Singly Linked Lists


Data objects:
A list of nodes each holding one (or more) data field(s) DATA
and a single link field LINK. LIST points to the start node of the
list.
Operations:
Check if list LIST is empty
CHECK_LIST_EMPTY ( LIST) (Boolean function)
Insert ITEM into the list LIST as the first element
INSERT_FIRST (LIST, ITEM)
Insert ITEM into the list LIST as the last element
INSERT_LAST (LIST, ITEM)
Insert ITEM into the list LIST in order
INSERT_ORDER (LIST, ITEM)

30

Delete the first node from the list LIST


DELETE_FIRST(LIST)

Delete the last node from the list LIST


DELETE_LAST(LIST)

Delete ITEM from the list LIST


DELETE_ELEMENT (LIST, ITEM)

Advance Link to traverse down the list


ADVANCE_LINK (LINK)

Store ITEM into a node whose address is X


STORE_DATA(X, ITEM)

Retrieve data of a node whose address is X and return it in


ITEM
RETRIEVE_DATA(X, ITEM)

Retrieve link of a node whose address is X and return the value


in LINK1
RETRIEVE_LINK (X, LINK1)

Вам также может понравиться