Download presentation
Presentation is loading. Please wait.
Published byΑναστασούλα Νικολάκος Modified over 5 years ago
1
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Chapter 3 : Linked List By : Zanariah Idrus ©The McGraw-Hill Companies, Inc.
2
Chapter Objective Methods in LinkedList
Understanding basic properties of LinkedList Types of LinkedList (Doubly LinkedList, Circular LinkedList)
3
ARRAYLIST VS. LINKEDLIST
4
Linked List
5
LINKEDLIST The linked list is implemented using nodes linked to each other, which are not necessarily adjacent in memory Each node contains a previous node link (depends to the type of link list), next node link, and value, which contains the actual data.
6
LINKEDLIST A data part that stores an element of the list.
NUR AZMINA MOHAMAD ZAMANI (FSKM - UiTM Perak) LINKEDLIST A data structure that consists of nodes which are linked to each other in the sequence (implemented using nodes linked to each other). LinkedList has two(2) parts: A data part that stores an element of the list. A next part that stores a link that indicates the location of the node containing the next list element. The first node is called the HEAD The last node is called the TAIL
7
LINKEDLIST Data type of each node depends on the specific application — kind of data being processed. Link component of each node is a reference variable. Data type of this reference variable is node type itself.
8
Structured of LinkedList
Every node in a linked list has two components: one to store relevant information one to store address (the link) of next node in list Address of first node in list stored in separate location, called the head or first element next Entry head 45 next
9
Structure of Linked List
Structure of a node Structure of a linked list 45 next head 85 95 null
10
LinkedList When new data is inserted, a node is inserted and the links of the surrounding nodes are updated accordingly. When one is removed, the same happens - The surrounding nodes are changing their links and the deleted node moves to garbage. This, as well, gives strengths and weaknesses. Fast manipulation as you’d expect, adding and removing new data anywhere in the list is immediately. Change two links, and you have a new value anywhere you want it.
11
LinkedList The advantages of using Linked list:
Appropriate data structure - when the number of data is unpredictable. Dynamic data structure - the length of a list can increase or decrease as necessary. Simple to maintain in sorted order - by inserting each new element at proper point in the list.
12
LinkedList Linked structure – store elements anywhere in memory and linked by a reference/pointer
13
There are 3 types of linked list:
a) singly linked list b) doubly linked list c) circularly linked list
14
Types of linked list (cont …)
Singly linked list Each element is contained in an object, called an Entry object, that also includes a reference, called a link, that holds the next element in the list.
15
Types of linked list (cont …)
Singly linked list
16
Types of linked list (cont …)
Singly linked list
17
Types of linked list (cont …)
Doubly Linked List If each Entry object also includes a link to the Entry object that holds the previous element in the list, we have a doubly linked list.
18
Types of linked list (cont …)
Inserting into a Doubly-Linked List
19
Types of linked list (cont …)
Removing from a Double-Linked List
20
The last node points to the first node
Circularly Lists The last node points to the first node
21
Circularly Lists
22
Implementation of Linked List
JAVA provides class LinkedList for implementing and manipulating linked list in JAVA package –(java.util.*) LinkedList class in JAVA uses a doubly linked list implementation. Programmer can also create self-defined linked list by defining own linked list class to represent the node and linked list structure. For this course, we will create our own linked list class using singly linked list
23
Node structure Node object Next Data Reference link
Store reference to next node (Node type) Data - data type (primitive or ADT Class : Node Fields/Data : Data, Next; Methods : Constructor
24
The declaration of the class Node as below:
A node is an object of a class called Node Each Node object contain a reference (called next) to the link in the list public class Node { Object data; // data Node next; // reference to next Node public Node (int id) // constructor { data =id; }
25
The declaration of the class LinkList as below:
public class LinkList { private Node first; // reference to the first node in the list private Node current; // reference to the current node private Node last; // reference to the last node public LinkList( ) // constructor { first = current = last = null; // no item in the list; } …….. Other method goes here
26
Methods involve in LinkedList class:
Constructor (default & normal) isEmpty() // check whether list is empty insertAtFront(object) // insert at the front of list insertAtBack(object) // insert at the end of list removeFromFront() // delete element from front removeFromBack() // delete element from back getFirst() // get the first node getNext() // get the reference for next node
27
The isEmpty() method public boolean isEmpty() { return (first==null);
isEmpty() method will check if the list is empty or not. It returns true if the list is empty, otherwise it returns false. The list is empty if first refers to null. This method is important when we want to delete a node where the list must be checked to make sure it is not empty upon deletion. public boolean isEmpty() { return (first==null); }
28
The insertAtFront() method
The insertAtFront() method inserts a new node at the beginning of the list. The easiest way to insert a node because first already point to the first node. Starts by creating a new node, and then set the next field in the newly created node to point to the first node. Then, change first to point to the newly created node.
29
public void insertAtFront(Object insertItem) { Node newNode = new Node(insertItem); if (isEmpty()) { first = newNode; last = newNode; } else newNode.next = first; first = newNode;
30
The insertAtBack() method
The insertAtBack() method inserts a new node at the end of the list. This quite easy method as last is already point to the last node. Starts by creating a new node, and set the next field in the last node to point to the newly create node. Then, change last to point to the newly created node.
31
public void insertAtBack(Object insertItem)
{ Node newNode = new Node(insertItem); if (isEmpty()) { first = newNode; last = newNode; } else last.next = newNode;
32
The removeFromFront() method
The removeFromFront() method is the reverse of insertAtFront(). The data object in the first node is marked to be deleted. It disconnects the first node by rerouting first to point to the second node. The second node is found by looking at the next field in the first node. Then the removed object is returned for the convenience of user (to print, calculate, etc)
33
public Object removeFromFront()
{ Object removeItem = null; if (isEmpty()) return removeItem; } removeItem = first.data; if ( first == last) first = null; last = null; else first = first.next;
34
The removeFromBack() method
The removeFromBack() method is the reverse of insertAtBack(). The data object in the last node is marked to be deleted. It disconnects the last node by rerouting last to point to the second last node. The second last node is found by looking at the node whose next is pointing to last. Then the removed object is returned for the convenience of user (to print, calculate, etc)
35
public Object removeFromBack()
{ Object removeItem = null; if (isEmpty()) return removeItem; } removeItem = last.data; if ( first == last) first = null; last = null; else current = first; while (current.next != last) current = current.next; last = current; last.next = null;
36
The getFirst() method The getFirst() method will get the first item in the list. current is set to point to the first node. Then the data object in the current will be returned to the calling statement.
37
The getFirst() method cont …
public Object getFirst() { if (isEmpty()) return null; else current = first; return current.data; }
38
The getNext() method The getNext() method will get the next item to current if it is not the last node. The method checks if current is the last node, if it is method will return null. If current is not the last node, current is moved to point to the next node. Then the data object in the current will be returned to the calling statement.
39
The getNext() method public Object getNext() { if (current == last)
return null; else current = current.next; return current.data; }
40
CSC258 DATA STRUCTURE
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.