Download presentation
Presentation is loading. Please wait.
Published byLina Leveridge Modified over 9 years ago
1
Ics202 Data Structures
2
hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer
3
class LinkedList { protected Element head; protected Element tail; public LinkedList () // constructor for empty list { } public final class Element {Object datum; Element next; Element (Object datum, Element next) { this.datum = datum; this.next = next;} public Object getDatum () //to return element { return datum; } public Element getNext () //to get next element { return next; } Java Class
4
public void insertAfter (Object item) { // to insert after element next = new Element (item, next); if (tail == this) tail = next; } public void insertBefore (Object item) { //to insert before element Element tmp = new Element (item, this); if (this == head) head = tmp; else { Element prevPtr = head; while (prevPtr != null && prevPtr.next != this) prevPtr = prevPtr.next; prevPtr.next = tmp; } } } public void purge () { //to clear the list head = null; tail = null; }
5
public Element getHead () { // to return head return head; } public Element getTail () { // to return tail return tail; } public boolean isEmpty () { // to check the item in the list return head == null; } public Object getFirst () { // to return head or first element return head.datum; } public Object getLast () { // to return tail or last element return tail.datum; } public Element getPtr(Object item) { //return ptr for (Element ptr = head; ptr != null;ptr = ptr.next) if (ptr.datum == item) return ptr; return null; }
6
public void prepend (Object item) // to add element at the fist of list {Element tmp = new Element (item, head); if (head == null) tail = tmp; head = tmp; } public void append (Object item) // to add element at the end of list {Element tmp = new Element (item, null); if (head == null) head = tmp; else tail.next = tmp; tail = tmp; } public void assign (LinkedList list) //to assign element of list to another list {if (list != this) { purge (); for (Element ptr = list.head; ptr != null; ptr = ptr.next) append (ptr.datum); }}
7
public void extract (Object item) // to delet the element for list {Element ptr = head; Element prevPtr = null; while (ptr != null && ptr.datum != item){ prevPtr = ptr; ptr = ptr.next;} if (ptr == null) throw new IllegalArgumentException ("item not found"); if (ptr == head) head = ptr.next; else prevPtr.next = ptr.next; if (ptr == tail) tail = prevPtr; } public void print (LinkedList list) // used to print the element of linked list {if (isEmpty()) System.out.println("The list is empty"); else{ for (Element ptr = list.head; ptr != null; ptr = ptr.next) System.out.print(ptr.datum + " "); System.out.println(); }}}
8
class LinkedListTest { public static void main(String [] args) { // create a new empty list l System.out.println("The head of the list is: " + l.getHead()); // print the tail // check if the list empty or not l.print(l); l.prepend(30); // add 20 and 10 at the first of list // print the list // add 40 and 50 at the last of list l.print(l); // print the first element System.out.println(l.getLast() + "");
9
// delet 30 from the list and print the list // create a new empty list l1 // assign l to l1 // print l1 l1.getPtr(10).insertBefore(100); l1.print(l1); // insert 100 after 50 and print the list l1.getHead().insertBefore("xxx"); … // insert ("xxx") after the tail of the list l1.print(l1); }}
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.