1 linked lists (lec 6). 2 introduction singly linked lists circularly linked lists doubly linked...

Post on 19-Jan-2016

262 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Linked Lists

(Lec 6)

2

Introduction

Singly Linked Lists

Circularly Linked Lists

Doubly Linked Lists

Multiply Linked Lists

Applications

ADT for links

ADT for singly linked lists

Outline

3

Sequential data structures in general, suffer from the following drawbacks:

• Inefficient implementation of insertion and deletion operations

• Inefficient use of storage memory

4

Linked Lists

• A linked list is a series of connected nodes• Each node contains at least– A piece of data (any type)– Pointer to the next node in the list

• Head: pointer to the first node• The last node points to NULL

A

Head

B C

A

data pointer

node

5

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

6

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 given node as dictated by the application.

7

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)

• to return or dispose nodes from the reserved area or pool to the free area after use RETURN(X)

8

Linked Lists 8

Singly Linked Lists

• A singly linked list is a concrete data structure consisting of a sequence of nodes

• Each node stores– element– link to the next node

next

elem node

A B C D

9

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)

LINK

DATA

10

Linked Lists 10

Inserting at the Head

1. Allocate a new node2. Insert new element3. Make new node

point to old head4. Update head to point

to new node

11

Linked Lists 11

Removing at the Head

1. Update head to point to next node in the list

2. Allow garbage collector to reclaim the former first node

12

Linked Lists 12

Singly linked list with ‘tail’ sentinel

public class SLinkedListWithTail { protected Node head; // head node of the list protected Node tail; // tail node of the list /** Default constructor that creates an empty list */ public SLinkedListWithTail() {

head = null; tail = null;

}// ... update and search methods would go here ...

}

13

Linked Lists 13

Inserting at the Tail

1. Allocate a new node

2. Insert new element

3. Have new node point to null

4. Have old last node point to new node

5. Update tail to point to new node

14

Linked Lists 14

Removing at the Tail

• Removing at the tail of a singly linked list cannot be efficient!

• There is no constant-time way to update the tail to point to the previous node

15

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.

16

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.

Deletion in a singly linked list

17

Linked Lists 17

Doubly Linked List• A doubly linked list is often more

convenient! • Nodes store:

– element– link to the previous node– link to the next node

• Special trailer and header nodes

prev next

elem

trailerheader nodes/positions

elements

node

18

Linked Lists 18

Insertion• We visualize operation insertAfter(p, X), which returns position q

A B X C

A B C

p

A B C

p

X

q

p q

19

Linked Lists 19

Insertion AlgorithmAlgorithm insertAfter(p,e):

Create a new node vv.setElement(e)v.setPrev(p) {link v to its predecessor}v.setNext(p.getNext()) {link v to its successor}(p.getNext()).setPrev(v) {link p’s old successor to v}p.setNext(v) {link p to its new successor, v}return v {the position for the element e}

20

Linked Lists 20

Deletion• We visualize remove(p), where p == last()

A B C D

p

A B C

D

p

A B C

21

Linked Lists 21

Deletion Algorithm

Algorithm remove(p):t = p.element {a temporary variable to hold the

return value}(p.getPrev()).setNext(p.getNext()) {linking out p}(p.getNext()).setPrev(p.getPrev())p.setPrev(null){invalidating the position p}p.setNext(null)return t

22

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.

23

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

24

• headed circularly linked list or circularly linked list with head node.

HEAD NODE

25

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

Doubly Linked Lists

26

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

LLINK RLINK

DATA

27

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.

28

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.

29

Multiply Linked Lists

• A multiply linked list as its name suggests is a linked representation with multiple data and link fields

30

Applications

• Addition of polynomials • Representation of a sparse matrix

31

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)

32

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)

33

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)

34

Variations of Linked Lists

• Circular linked lists– The last node points to the first node of the list

– How do we know when we have finished traversing the list? (Tip: check if the pointer of the current node is equal to the head.)

A

Head

B C

35

Variations of Linked Lists• Doubly linked lists– Each node points to not only successor but the

predecessor– There are two NULL: at the first and last nodes in

the list– Advantage: given a node, it is easy to visit its

predecessor. Convenient to traverse lists backwards

A

Head

B C

36

Array versus Linked Lists

• Linked lists are more complex to code and manage than arrays, but they have some distinct advantages.– Dynamic: a linked list can easily grow and shrink in size.

• We don’t need to know how many nodes will be in the list. They are created in memory as needed.

• In contrast, the size of a C++ array is fixed at compilation time.

– Easy and fast insertions and deletions• To insert or delete an element in an array, we need to copy to

temporary variables to make room for new elements or close the gap caused by deleted elements.

• With a linked list, no need to move other nodes. Only need to reset some pointers.

top related