Linked List First next Data Linked List Link next Data Link next Data Link next Data Link Null.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
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
Double linked list Lai Ah Fur. The structure of node class IntDLLNode { int info; IntDLLNode next = null, prev = null; IntDLLNode() { } IntDLLNode(int.
1/27 COP 3540 Data Structures with OOP Chapter 5 Linked Lists.
Data Structures ADT List
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Data Structure Lecture-5
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
The Ranked Sequence ADT Definition A ranked sequence is a collection of items arranged in a linear order, where each item has a rank defining the relative.
Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes.
LIST( using dynamic memory allocation). Introduction  We need dynamic Memory Allocation, when the data is dynamic in nature.  That is, the number of.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
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)
Linked Lists CSC 172 SPRING 2004 LECTURE 6. ANNOUNCEMENTS Project 2 due Wed, Feb 18 th, 5PM, CSB Read Weiss Chapter 17 Department T shirts available $10.
Linked Lists CSC 172 SPRING 2004 LECTURE 6. ANNOUNCEMENTS Project 2 due Wed, Feb 18 th, 5PM, CSB Read Weiss Chapter 17 Department T shirts available $10.
COP 3540 Data Structures with OOP
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
1/26 COP 3540 Data Structures with OOP Chapter 5 - Part 2 Linked Lists Abstract Data Types and Sorted Lists.
CMSC 341 Linked Lists, Stacks and Queues. 8/3/2007 UMBC CMSC 341 LSQ 2 Implementing Your Own Linked List To create a doubly linked list as seen below.
Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types (ADTs) Lists Stacks Queues.
Midterm Review 22C:21 Computer Science II. Problem 1 A Set ADT represents some subset of {1,2,..., n} for some natural number n. It supports the operations.
Linked Lists Tonga Institute of Higher Education.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
The List ADT Definition A list is a collection of objects, called nodes, connected in a chain by links. There may or may not be an ordering relationship.
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.
Linked Lists © John Urrutia 2013, All Rights Reserved1.
Introduction to Data Structures and Algorithms
Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Chapter 5 Linked Lists II
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1.
1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today.
1 Java linked list. Java linked list - definition ▪ Often in programming we are required to systematically store some type of information. A prime example.
CS212: DATASTRUCTURES Lecture 3: Searching 1. SinglyLinked list 2 A linked list is a series of connected nodes. Each node contains at least: – A piece.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
1 COP 3540 Data Structures with OOP Chapter 5 - Part 3 Linked Lists Doubly-Linked Lists and Iterators.
Java linked list.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
WAP to find out the number is prime or not Import java.util.*; Class Prime { public static void main(string args[]) { int n,I,res; boolean flag=true;
COSO 1030 Section 3 Linked Data Representation. What is about Reference and Object in JAVA List and Node The Difference Between List and Array Linear.
Doubly / Two Way Linked List It is linear collection of data elements, called nodes, where each node N is divided into three parts: – An information field.
Disadvantages of Physically linear data structures Fixed size Not flexible Insertion / deletion is difficult Always need boundary conditions etc.
Linked Data Structures
Introduction What Is Linked List? (2002 , 2007,2011)
Doubly Linked List Review - We are writing this code
Algorithm for deleting a node from a singly linked list
Java collections library
COP 3538 Data Structures with OOP
8-1.
8-1.
null, true, and false are also reserved.
Data Structures ADT List
Data Structures ADT List
Propositional Equivalences Rosen 5th and 6th Editions section 1.2
ADT list.
COSC 1030 Section 8 List.
class PrintOnetoTen { public static void main(String args[]) {
Data Structures ADT List
The List ADT Definition A list is a collection of objects, called nodes, connected in a chain by links. There may or may not be an ordering relationship.
Header and Trailer Sentinels
Introduction to Algorithms and Data Structures
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
General List.
Presentation transcript:

Linked List First next Data Linked List Link next Data Link next Data Link next Data Link Null

Linked List Operasi: PPPPenyisipan sebuah item di awal list. PPPPenghapusan sebuah item di awal list. MMMMenampilkan isi list

Linked List (insertFirst Method) First Linked List next Data Link next Data Link Null next Data Link Null

Linked List (deleteFirst Method) First Linked List next Data Link next Data Link next Data Link next Data Link Null

Linked List (displayList Method) First next Data Linked List Link next Data Link next Data Link next Data Link Null hasil statement sekarang = first hasil statement sekarang = sekarang.next hasil statement sekarang = sekarang.next hasil statement sekarang = sekarang.next hasil statement sekarang = sekarang.next

class Link class Link { public int iData; public double dData; public Link next; // public Link(int id, double dd) { iData = id; dData = dd; } // public void tampilkanLink() { System.out.print("{" + iData + ", " + dData + "} "); } }

class LinkList class LinkList { private Link pertama; // public LinkList() { pertama = null; } // public boolean kosong() { return (pertama==null); } // }

class LinkList (lanjutan) public void sisipPertama(int id, double dd) { Link linkBaru = new Link(id, dd); linkBaru.next = pertama; pertama = linkBaru; } // public Link hapusPertama() { Link temp = pertama; pertama = pertama.next; return temp; } //

public void tampilkanList() { System.out.print("List (pertama-->terakhir): "); Link sekarang = pertama; while(sekarang != null) { sekarang.tampilkanLink(); sekarang = sekarang.next; } System.out.println(""); } // }

LinkListApp class LinkListApp { public static void main(String[] args) { LinkList listQ = new LinkList(); listQ.sisipPertama(22, 2.99); listQ.sisipPertama(44, 4.99); listQ.sisipPertama(66, 6.99); listQ.sisipPertama(88, 8.99); listQ.tampilkanList(); while( !listQ.kosong() ) { Link linkQ = listQ.hapusPertama(); linkQ.tampilkanLink(); System.out.print(" terhapus"); System.out.println(""); } listQ.tampilkanList(); } }

find Method // public Link find(int key) { Link current = first; while(current.iData != key) { if(current.next == null) return null; else current = current.next; } return current; } //

delete Method public Link delete(int key) { Link current = first; Link previous = first; while(current.iData != key) { if(current.next == null) return null; else{ previous = current;

current = current.next; }} if(current == first) first = first.next; else previous.next = current.next; return current; } //

delete First next Data Linked List Link next Data Link next Data Link next Data Link Null previouscurrent

double-Ended List First Last next Data Linked List Link next Data Link next Data Link next Data Link Null

next Data Link next Data Link double-ended list First Last Linked List next Data Link Null

program class Link { public long dData; public long dData; public Link next; public Link next; // public Link(long d) public Link(long d) { dData = d; dData = d; } // public void displayLink() public void displayLink() { System.out.print(“(“+dData+”)” + “ ” ); System.out.print(“(“+dData+”)” + “ ” ); } // }

class FirstLastList { private Link first; private Link first; private Link last; private Link last; // public FirstLastList() { first = null; first = null; last = null; last = null; } // public boolean isEmpty() { return first==null; } { return first==null; } // public void insertFirst(long dd) { Link newLink = new Link(dd); Link newLink = new Link(dd); if( isEmpty() ) if( isEmpty() ) last = newLink; last = newLink; newLink.next = first; newLink.next = first; first = newLink; first = newLink; } //

public void insertLast(long dd) { Link newLink = new Link(dd); Link newLink = new Link(dd); if( isEmpty() ) if( isEmpty() ) first = newLink; first = newLink; else else last.next = newLink; last.next = newLink; last = newLink; last = newLink;} // public long deleteFirst() { long temp = first.dData; long temp = first.dData; if(first.next == null) if(first.next == null) last = null; last = null; first = first.next; first = first.next; return temp; return temp; } //

public void displayList() { System.out.print(“List (first-->last): “); System.out.print(“List (first-->last): “); Link current = first; Link current = first; while(current != null) while(current != null) { current.displayLink(); current.displayLink(); current = current.next; current = current.next; } System.out.println(“”); System.out.println(“”); } // }

class FirstLastApp { public static void main(String[] args) public static void main(String[] args) { FirstLastList theList = new FirstLastList(); FirstLastList theList = new FirstLastList(); theList.insertFirst(22); theList.insertFirst(22); theList.insertFirst(44); theList.insertFirst(44); theList.insertFirst(66); theList.insertFirst(66); theList.insertLast(11); theList.insertLast(11); theList.insertLast(33); theList.insertLast(33); theList.insertLast(55); theList.insertLast(55); theList.displayList(); theList.displayList(); theList.deleteFirst(); theList.deleteFirst(); theList.displayList(); theList.displayList(); } // end main() } // end main() } // end class FirstLastApp } // end class FirstLastApp

Link next prev Data Doubly linked-list First Last Linked List Link next prev Data next prev Data next prev Data Null

Link next prev Data First Last Linked List Link next prev Data Link next prev Data next prev Data Null

Program class Link { public long dData; public Link next; public Link previous; // public Link(long d) { dData = d; } // public void displayLink() { System.out.print(dData + “ ”); } // } // end class Link

class DoublyLinkedList { private Link first; private Link last; // public DoublyLinkedList() { first = null; last = null; } // public boolean isEmpty() { return first==null; } //

public void insertFirst(long dd) { Link newLink = new Link(dd); if( isEmpty() ) last = newLink; else first.previous = newLink; newLink.next = first; first = newLink; } // public void insertLast(long dd) { Link newLink = new Link(dd); if( isEmpty() ) first = newLink; else{ last.next = newLink; newLink.previous = last; } last = newLink; } //

public Link deleteFirst() { Link temp = first; if(first.next == null) last = null; else first.next.previous = null; first = first.next; return temp; } // public Link deleteLast() { Link temp = last; if(first.next == null) first = null; else last.previous.next = null; last = last.previous; return temp; } //

// insert dd setelah key public boolean insertAfter(long key, long dd) { Link current = first; while(current.dData != key) { current = current.next; if(current == null) return false; } Link newLink = new Link(dd); if(current==last){ newLink.next = null; last = newLink; }else{ newLink.next = current.next; // newLink <-- old next current.next.previous = newLink; } newLink.previous = current; current.next = newLink; return true; } //

public Link deleteKey(long key) { Link current = first; while(current.dData != key) { current = current.next; if(current == null) return null; }if(current==first) first = current.next; else current.previous.next = current.next; if(current==last) last = current.previous; else current.next.previous = current.previous; return current; } //

public void displayForward() { System.out.print(“List (first-->last): “); Link current = first; while(current != null) {current.displayLink(); current = current.next; }System.out.println(“”);} // public void displayBackward() { System.out.print(“List (last-->first): “); Link current = last; while(current != null) {current.displayLink(); current = current.previous; }System.out.println(“”);} // } // end class DoublyLinkedList

class DoublyLinkedApp { public static void main(String[] args) { DoublyLinkedList theList = new DoublyLinkedList(); theList.insertFirst(22);theList.insertFirst(44);theList.insertFirst(66);theList.insertLast(11);theList.insertLast(33);theList.insertLast(55);theList.displayForward();theList.displayBackward();theList.deleteFirst();theList.deleteLast();theList.deleteKey(11);theList.displayForward(); theList.insertAfter(22, 77); theList.insertAfter(33, 88); theList.displayForward();}}