Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Stacks, Queues, and Linked Lists
Linked Lists Chapter 4.
1/27 COP 3540 Data Structures with OOP Chapter 5 Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Data Structures Using C++
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Linked Lists. Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link (pointer or reference)
Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.
Data Structures Using C++ 2E
Chapter 17 Linked List.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
C++ Classes and Data Structures Jeffrey S. Childs
Data Structures Using Java1 Chapter 4 Linked Lists.
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Linked List by Chapter 5 Linked List by
Introduction to Data Structures and Algorithms
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Chapter 5 Linked Lists II
Data Structures Using C++1 Chapter 5 Linked Lists.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
Linked Lists A formal data structure. Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Chapter 16: Linked Lists.
Lecture 6 of Computer Science II
Data Structures: Linked Lists
C++ Programming:. Program Design Including
Chapter 22 Custom Generic Data Structures
Lists CS 3358.
UNIT-3 LINKED LIST.
Linked lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Algorithm for deleting a node from a singly linked list
8-1.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
8-1.
Topic 11 Linked Lists -Joel Spolsky
Chapter 18: Linked Lists.
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
ADT list.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Lecture 14 Linked Lists CSE /26/2018.
Data Structures & Algorithms
Chapter 2 : List ADT Part I – Sequential List
Programming II (CS300) Chapter 07: Linked Lists
Stacks, Queues, and Deques
Linked lists.
Topic 11 Linked Lists -Joel Spolsky
Presentation transcript:

Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus Chapter 3 : Linked List By : Zanariah Idrus ©The McGraw-Hill Companies, Inc.

Chapter Objective Methods in LinkedList Understanding basic properties of LinkedList Types of LinkedList (Doubly LinkedList, Circular LinkedList)

ARRAYLIST VS. LINKEDLIST

Linked List

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.

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

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.

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

Structure of Linked List Structure of a node Structure of a linked list 45 next head 85 95 null

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.

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.

LinkedList Linked structure – store elements anywhere in memory and linked by a reference/pointer

There are 3 types of linked list: a) singly linked list b) doubly linked list c) circularly linked list

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.

Types of linked list (cont …) Singly linked list

Types of linked list (cont …) Singly linked list

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.

Types of linked list (cont …) Inserting into a Doubly-Linked List

Types of linked list (cont …) Removing from a Double-Linked List

The last node points to the first node Circularly Lists The last node points to the first node

Circularly Lists

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

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

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; }

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

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

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); }

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.

public void insertAtFront(Object insertItem) { Node newNode = new Node(insertItem); if (isEmpty()) { first = newNode; last = newNode; } else newNode.next = first; first = newNode;

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.

public void insertAtBack(Object insertItem) { Node newNode = new Node(insertItem); if (isEmpty()) { first = newNode; last = newNode; } else last.next = newNode;

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)

public Object removeFromFront() { Object removeItem = null; if (isEmpty()) return removeItem; } removeItem = first.data; if ( first == last) first = null; last = null; else first = first.next;

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)

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;

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.

The getFirst() method cont … public Object getFirst() { if (isEmpty()) return null; else current = first; return current.data; }

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.

The getNext() method public Object getNext() { if (current == last) return null; else current = current.next; return current.data; }

CSC258 DATA STRUCTURE