Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures & Algorithms

Similar presentations


Presentation on theme: "Data Structures & Algorithms"— Presentation transcript:

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

2 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

3 Need for Linked list We need an alternative structure that stores elements in a sequence but allows for more efficient insertion and deletion of elements at random positions in the list. In a linked list, elements contain links that reference the successor element in the list. Inserting and deleting an element is a local operation and requires updating only the links adjacent to the element. The other elements in the list are not affected. An Array must shift all elements on the tail whenever a new element enters or exits the list. Ms. Manal Al-Asmari

4 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

5 Linked Lists “Reference-Based Lists”
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

6 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

7 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

8 Linked List: Some properties
Ms. Manal Al-Asmari

9 Linked List: Some properties
Ms. Manal Al-Asmari

10 Linked List: Some properties
Ms. Manal Al-Asmari

11 Linked List: Some properties
Ms. Manal Al-Asmari

12 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 as head: current Ms. Manal Al-Asmari

13 Traversing a Linked List
The following code traverses the list current = head; while (current != null) { //Process current current = current.link; } Ms. Manal Al-Asmari

14 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

15 Insertion Ms. Manal Al-Asmari

16 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

17 Insertion Ms. Manal Al-Asmari

18 Deletion Ms. Manal Al-Asmari

19 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

20 Types of Linked Lists: Doubly Linked Lists Circular Linked Lists
Ms. Manal Al-Asmari

21 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

22 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

23 THE END Ms. Manal Al-Asmari


Download ppt "Data Structures & Algorithms"

Similar presentations


Ads by Google