Documente Academic
Documente Profesional
Documente Cultură
1 Implementation 2.2.2 2 2 2 Application Linked Lists 2.3.1 Implementation 231 I l t ti 2.3.2 Application Other types of Lists
Intersession May 2009 1
2.2
2.3
2.4
Introduction to List
What is List ? List is an ordered sequence of the same type elements (example: Telephone directory, students) List provides methods for manipulating elements via their indices ( l (elements index). t i d ) manipulating a specified range of elements, searching for elements access the elements delete the elements
[0]
[1]
[2]
[3]
[4]
Linked List Linked structure store elements anywhere in y memory and linked by a reference/pointer
Linked list
Linked List Linked List is a linear collection of objects (called nodes) connected by the reference links. Program accesses a linked list via a reference to the first node in list. Program accesses a subsequent nodes via the reference link stored in the previous node. efe ence sto ed t e p evious Reference in the last node of a list is set to null.
Reference link Firstnode Lastnode
null
Intersession May 2009 4
b) Doubly Linked list each node contains two references ( to the previous and to the next node)
List N d bj t Li t Node object Data - data type (primitive or ADT Class : Fields/Data : Methods : ListNode Data, Next; Constructor Retrieval methods
Intersession May 2009 8
Methods : ListNode ( Object obj ) // default constructor ListNode( Object obj, ListNode nextNode ) // constructor Object getObject() // return the data ListNode getNext() // return link/reference to next data
Intersession May 2009 9
Methods : 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() F B k() // delete element from back d l t l tf b k getFirst() // get the first node getNext() // get the reference for next node
10
11
// use t traverse the li t to t th list // name used for list // default constructor
13
15
/* Return element of the next node pointed by currNode. Use together with getFirst to traverse the list */ p public Object getNext() j g () { if (currNode != lastNode) { currNode = currNode.next; return currNode.data; } else return null; }
Intersession May 2009 16
17
// class to handle the exception class EmptyListException extends RuntimeException { public EmptyListException( String name ) { super( "The " + name + " is empty" ); } }
20
// to print initial li t t i t i iti l list out1 += "Initial data in list :" + "\n" + printList(objList); objList.insertAtFront( 234 ); objList insertAtFront("234"); objList.insertAtFront("567"); // to print current list out1 += "\nCurrent data in list :" + "\n" + printList(objList);
22
23
24
// to print list content after removing data from list out4 += "\ndata in list after removing two elements :" + "\n" + printList(objList); out +=out1+out2+out3+out4; JOptionPane.showMessageDialog(null,out,"Result", JOptionPane.PLAIN_MESSAGE); } //end main
25
} // end PrimitiveLL
26
28
29
} // end main
30
33