data structures & algorithms chapter 5 linked list ms. manal al-asmari

23
Data Structures & Algorithms CHAPTER 5 Linked List Ms. Manal Al-Asmari

Upload: gladys-randall

Post on 19-Dec-2015

226 views

Category:

Documents


0 download

TRANSCRIPT

Ms. Manal Al-Asmari

Data Structures & Algorithms

CHAPTER 5Linked List

Ms. Manal Al-Asmari

Motivation

• Suppose I have an array: 1,4,10,19,6• I want to insert a 7 between the 4 and the 10• What do I need to do?• What about deleting an element from the

array ?

Ms. Manal Al-Asmari

Need for Linked list• We need an alternative structure that stores elements in asequence but allows for more efficient insertion and deletion ofelements at random positions in the list.

• In a linked list, elements contain links that reference the successorelement in the list.

• Inserting and deleting an element is a local operation and requiresupdating only the links adjacent to the element. The otherelements in the list are not affected.

• An Array must shift all elements on the tail whenever a newelement enters or exits the list.

Ms. Manal Al-Asmari

Arrays vs. Linked lists• Arrays• have a pre-determined fixed size• easy access to any element a[i] in constant time• no space overhead• S = n x sizeof(element)

• Linked lists• no fixed size; grow one element at a time• space overhead• each element must store an additional reference• S = n x sizeof (element) + n x sizeof(reference)• no easy access to i-th element• need to hop through all previous elements

Ms. Manal Al-Asmari

Linked Lists“Reference-Based Lists”

• Linked list• List of items, called nodes• The order of the nodes is determined by the address,

called the link, stored in each node.• Every node (except the last node) contains the address

of the next node

• Components of a node• Data: stores the relevant information• Link: stores the address of the next node

Ms. Manal Al-Asmari

Structure of Linked Lists

Figure1: Structure of a node

Figure2: Linked list

•Head or first holds the address of the first node in the list• The info part of the node can be either a value of a primitive type or a reference to an object

Ms. Manal Al-Asmari

Linked Lists

• Class Node• Represents nodes on a list• It has two instance variables * info (of type int, but it can be any other type) * link (of type Node)

public class Node { public int info; public Node link; }

Ms. Manal Al-Asmari

Linked List: Some properties

Ms. Manal Al-Asmari

Linked List: Some properties

Ms. Manal Al-Asmari

Linked List: Some properties

Ms. Manal Al-Asmari

Linked List: Some properties

Ms. Manal Al-Asmari

Traversing a Linked List

• Basic operations of a linked list that require the link to be traversed:

* Search the list for an item * Insert an item in the list * Delete an item from the list• You cannot use head to traverse the list * You would lose the nodes of the list *Use another reference variable of the same type ashead: current

Ms. Manal Al-Asmari

Traversing a Linked List

• The following code traverses the list current = head; while (current != null) { //Process current current = current.link; }

Ms. Manal Al-Asmari

Insertion

Consider the following linked list

You want to create a new node with info 50 and insert it after p

Ms. Manal Al-Asmari

Insertion

Ms. Manal Al-Asmari

Insertion

• The following statements insert the node in the linked list at the required place

newNode.link = p.link; p.link = newNode;

• The sequence of statements to insert the node is very important

Ms. Manal Al-Asmari

Insertion

Ms. Manal Al-Asmari

Deletion

Ms. Manal Al-Asmari

Building a Linked List

• You can build a list in two ways: forward or backward:• Forward manner A new node is always inserted at the end of the

linked list• Backward manner A new node is always inserted at the beginning

of the linked list

Ms. Manal Al-Asmari

Types of Linked Lists:

• Doubly Linked Lists• Circular Linked Lists

Ms. Manal Al-Asmari

1 \Doubly Linked Lists

• Linked list in which every node has a next link and a back link.

• A doubly linked list can be traversed in either direction

Ms. Manal Al-Asmari

2 \Circular Linked Lists

• A linked list in which the last node refers to the first node.

• It is convenient to make first point to the last node.

Ms. Manal Al-Asmari

THE END