Title: Building a Linked List in Java
1Building a Linked List in Java
2Linked List
- In the Procedural Paradigm a linked list
consisted of - A pointer to the head of the list
- Nodes (in dynamic memory i.e. the heap)
containing data and additional next pointers - Modules that would perform a variety of functions
as needed add (in order), traverse, find,
delete, etc. - In the Object Oriented Paradigm a linked list
will consist of a class which contains - The head pointer (globally scoped in the class)
- Methods to perform the function listed above
- We will also need a class to hold the nodes.
Well start with that...
3Node
- class Node
- StudentRecord data
- Node next
- public Node(StudentRecord data)
- setData(data)
- setNext(null)
- // Constructor
- public void setData(StudentRecord data)
- this.data data // !!!!!!!!!!!!!!!!!!!!!!!!!!
-
- public void setNext(Node next) this.next
next - public StudentRecord getData() return data
- public Node getNext() return next
- public String toString()
- if(data null)
- return "Node null"
- else
- return "Node " data.toString()
-
Comments omitted for clarity!!!
4Are we done?
- What about the test main???
5Remember the main(e)
- public static void main(String args)
- StudentRecord sr
- new StudentRecord("Bob", 3.5, 123456789)
- Node n1 new Node(null)
- System.out.println("Empty node test\n" n1)
- // Load up and print
- n1.setData(sr)
- System.out.println("Bob "n1)
- sr new StudentRecord("Mary", 3.7, 987654321)
- Node n2 new Node(sr)
- n1.setNext(n2)
- System.out.println("Bob "n1)
- System.out.println("Mary "n2)
- // main
- // Node
6Lets see whats happening
7Tracing
- StudentRecord sr
- new StudentRecord("Bob", 3.5, 123456789)
name "Bob" gpa 3.5 ssn 123456789
sr
8Tracing
name "Bob" gpa 3.5 ssn 123456789
sr
data
n1
next
9Tracing
name "Bob" gpa 3.5 ssn 123456789
sr
data
n1
next
10Tracing
- sr new StudentRecord("Mary", 3.7, 987654321)
name "Bob" gpa 3.5 ssn 123456789
name "Mary" gpa 3.7 ssn 987654321
sr
data
n1
next
11Tracing
name "Bob" gpa 3.5 ssn 123456789
name "Mary" gpa 3.7 ssn 987654321
sr
data
n1
next
data
n2
next
12Tracing
name "Bob" gpa 3.5 ssn 123456789
name "Mary" gpa 3.7 ssn 987654321
sr
data
n1
next
data
n2
next
13Key to Understanding Java
- Understand that the "variables" that you may
think of as objects (not primitives) are ALWAYS
references (like pointers) to objects which live
in the heap. - The objects live and die immobile in the heap
- All the apparent movement of objects is just the
moving, copying, setting-to-null of references
14Review
- We have created and tested two classes
- class StudentRecord
- class Node
- We now construct the class LinkedList
- Well start with the fields and simple
accessors/modifiers - Then well write the add, traverse, find and
delete methods (plus helpers if necessary)
15LinkedList
- class LinkedList
- private Node head
- public LinkedList()
- setHead(null)
- // constructor
- private void setHead(Node head)
- this.head head
- // setHead
- private Node getHead()
- return head
- // getHead
16LinkedList (add method)
- // Purpose add in order by SSN
- // Postcon list will contain one additional
item - public void add(StudentRecord sr)
- if(getHead() null
- getHead().getData().getSsn() gt sr.getSsn() )
-
- Node temp new Node(sr)
- temp.setNext(getHead())
- setHead(temp)
-
- else
-
- add(getHead(), sr)
-
- // add
17LinkedList (add helper method)
- // Purpose add helper
- private void add(Node cur, StudentRecord sr)
- if( cur.getNext() null
- cur.getNext().getData().getSsn() gt
sr.getSsn() ) -
- Node temp new Node(sr)
- temp.setNext(cur.getNext())
- cur.setNext(temp)
-
- else
-
- add(cur.getNext(), sr)
-
- // add
18LinkedList (traverse )
- // Purpose traverse list
- // Postcon no change to list
- public void traverse()
- traverse(getHead())
- // traverse
- // Purpose traverse helper
- private void traverse(Node cur)
- if(cur ! null)
- System.out.println(cur)
- traverse(cur.getNext())
-
- // traverse
-
19LinkedList (traverse )
-
- // Purpose print out string passed in as
parameter - // then traverse list (useful for
- // debugging)
- // Postcon No change to list
- public void traverse(String s)
- System.out.println(s)
- traverse()
- // traverse
20LinkedList (traverse iteratively )
- // Purpose traverse iteratively (just shown for
- // comparison
- // Postcon No change to list
- public void traverseI()
- Node cur getHead()
- while(cur ! null)
- System.out.println(cur)
- cur cur.getNext()
-
- // traverseI
- // Purpose traverse iteratively with title
- public void traverseI(String s)
- System.out.println(s)
- traverseI()
- // traverseI
21LinkedList (find )
- // Purpose Locate record by SSN
- // Postcon No change to list
- public StudentRecord find(int targSsn)
- return find(getHead(), targSsn)
- // find
- // Purpose Find helper
- private StudentRecord find(Node cur, int
targSsn) - if(cur null)
- return null
- else
- if(cur.getData().getSsn() targSsn)
- return cur.getData()
- else
- return find(cur.getNext(), targSsn)
-
- // find
22LinkedList (deleteFirst)occurence
- // Purpose delete first occurence of record
with - // matching SSN
- // Postcon If SSN found record will be removed
thus - // list will be one shorter
- public void deleteFirst(int targSsn)
- if(getHead() ! null)
- if(getHead().getData().getSsn()
targSsn) - setHead(getHead().getNext())
- else
- deleteFirst(getHead(), targSsn)
-
-
- // deleteFirst
23LinkedList (deleteFirst)occurence
- // Purpose delete first occurence helper
- private void deleteFirst(Node cur, int targSsn)
-
- if(cur.getNext() ! null)
- if(cur.getNext().getData().getSsn()
targSsn) -
- cur.setNext(cur.getNext().getNext())
-
- else
-
- deleteFirst(cur.getNext(), targSsn)
-
-
- // deleteFirst
24LinkedList (deleteAll)occurences
- // Purpose delete all occurences matching a
target - // SSN
- // Postcon All matching occurences will be
- // eliminated
- public void deleteAll(int targSsn)
-
- // (Extra Credit!!!)
-
-
25LinkedList (main)
- public static void main(String args)
- LinkedList ell new LinkedList()
- ell.traverse("Empty list traversal")
- ell.add(new StudentRecord("Adam", 3.0, 333))
- ell.add(new StudentRecord("Bozo", 2.0, 222))
- ell.add(new StudentRecord("Carl", 1.0, 444))
- ell.traverseI("Should be 222 333 444")
- ell.add(new StudentRecord("Doug", 0.0, 111))
- ell.traverse("Should be 111 222 333 444")
- ell.deleteFirst(222)
- ell.traverseI("Should be 111 333 444")
- ell.deleteFirst(999)
- ell.deleteFirst(333)
- ell.deleteFirst(111)
- ell.deleteFirst(444)
- ell.traverse("Empty list???")
- // main
- // LinkedList
26Application
- / Demo application to allow user to add, find,
list, - and delete student records
- /
- class Application
- // Purpose print menu and get user choice
- // Postcon returns choice as int
- public static int menuChoice()
- System.out.println("Enter 1 to add")
- System.out.println("Enter 2 to find")
- System.out.println("Enter 3 to list")
- System.out.println("Enter 4 to delete")
- System.out.println("Enter 5 to quit")
- return IOGadget.readInt("Choice")
-
-
27Application
- // Purpose get information to fill student
record - // Postcon returns filled StudentRecord
- public static StudentRecord getSR()
- String name IOGadget.readLine("Name")
- double gpa IOGadget.readDouble("GPA")
- int ssn IOGadget.readInt("SSN")
- return new StudentRecord(name, gpa, ssn)
-
28Application
- // print menu and fulfill request
- public static void menuloop()
- LinkedList list new LinkedList()
- int choice
- do
- choice menuChoice()
- if(choice 1)
- list.add(getSR())
- else if(choice 2)
- StudentRecord sr
- list.find(IOGadget.readInt("SSN
")) - if(sr null)
- System.out.println("Not found")
- else
- System.out.println(sr)
-
29Application
- else if(choice 3)
- list.traverse("Student List")
- else if(choice 4)
- int ssn IOGadget.readInt("SSN?")
- list.deleteFirst(ssn)
-
- else if(choice 5)
- System.out.println("Exiting")
- else
- System.out.println("Illegal choice")
- while(choice ! 5)
- // menuloop
30Application
- public static void main(String args)
- menuloop()
-
- // class Application
31Diagram
class Application menuloop LinkedList
list main menuloop()
class LinkedList LLNode head methods
class LLNode StuRec data LLNode next
methods
Instance of
Instance of
Instance of
LinkedList object head
LLNode object StuRec data LLNode next
LLNode object StuRec data LLNode next
32(No Transcript)