Building a Linked List in Java Linked List
Building a Linked List in Java
Linked 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. We’ll start with that. . .
Node class Node { Student. Record data; Node next; public Node(Student. Record data) { set. Data(data); set. Next(null); } // Constructor public void set. Data(Student. Record data) { this. data = data; // !!!!!!!!!!!!! } public void set. Next(Node next) { this. next = next; } public Student. Record get. Data() { return data; } public Node get. Next() { return next; } public String to. String() { Comments omitted if(data == null) for clarity!!! return "Node: null"; else return "Node: " + data. to. String(); }
Are we done? What about the test main? ? ?
Remember the main(e) public static void main(String args[]) { Student. Record sr = new Student. Record("Bob", 3. 5, 123456789); Node n 1 = new Node(null); System. out. println("Empty node testn" + n 1); // Load up and print n 1. set. Data(sr); System. out. println("Bob: "+n 1); sr = new Student. Record("Mary", 3. 7, 987654321); Node n 2 = new Node(sr); n 1. set. Next(n 2); System. out. println("Bob: "+n 1); System. out. println("Mary: "+n 2); } // main } // Node
Let’s see what’s happening
Tracing Student. Record sr = new Student. Record("Bob", 3. 5, 123456789); sr name = "Bob" gpa = 3. 5 ssn = 123456789
Tracing Node n 1 = new Node(null); sr n 1 name = "Bob" gpa = 3. 5 ssn = 123456789 data next
Tracing n 1. set. Data(sr); sr n 1 name = "Bob" gpa = 3. 5 ssn = 123456789 data next
Tracing sr = new Student. Record("Mary", 3. 7, 987654321); sr n 1 name = "Bob" gpa = 3. 5 ssn = 123456789 data next name = "Mary" gpa = 3. 7 ssn = 987654321
Tracing Node n 2 = new Node(sr); sr n 1 n 2 name = "Bob" gpa = 3. 5 ssn = 123456789 data next name = "Mary" gpa = 3. 7 ssn = 987654321
Tracing n 1. set. Next(n 2); sr n 1 n 2 name = "Bob" gpa = 3. 5 ssn = 123456789 data next name = "Mary" gpa = 3. 7 ssn = 987654321
Key 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
Review • We have created and tested two classes – class Student. Record – class Node • We now construct the class Linked. List • We’ll start with the fields and simple accessors/modifiers • Then we’ll write the add, traverse, find and delete methods (plus helpers if necessary)
Linked. List class Linked. List { private Node head; public Linked. List() { set. Head(null); } // constructor private void set. Head(Node head) { this. head = head; } // set. Head private Node get. Head() { return head; } // get. Head
Linked. List (add method) // Purpose: add in order by SSN // Postcon: list will contain one additional item public void add(Student. Record sr) { if(get. Head() == null || get. Head(). get. Data(). get. Ssn() > sr. get. Ssn() ) { Node temp = new Node(sr); temp. set. Next(get. Head()); set. Head(temp); } else { add(get. Head(), sr); } } // add
Linked. List (add helper method) // Purpose: add helper private void add(Node cur, Student. Record sr) { if( cur. get. Next() == null || cur. get. Next(). get. Data(). get. Ssn() > sr. get. Ssn() ) { Node temp = new Node(sr); temp. set. Next(cur. get. Next()); cur. set. Next(temp); } else { add(cur. get. Next(), sr); } } // add
Linked. List (traverse ) // Purpose: traverse list // Postcon: no change to list public void traverse() { traverse(get. Head()); } // traverse // Purpose: traverse helper private void traverse(Node cur) { if(cur != null) { System. out. println(cur); traverse(cur. get. Next()); } } // traverse
Linked. List (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
Linked. List (traverse iteratively ) // Purpose: traverse iteratively (just shown for // comparison // Postcon: No change to list public void traverse. I() { Node cur = get. Head(); while(cur != null) { System. out. println(cur); cur = cur. get. Next(); } } // traverse. I // Purpose: traverse iteratively with title public void traverse. I(String s) { System. out. println(s); traverse. I(); } // traverse. I
Linked. List (find ) // Purpose: Locate record by SSN // Postcon: No change to list public Student. Record find(int targ. Ssn) { return find(get. Head(), targ. Ssn); } // find // Purpose: Find helper private Student. Record find(Node cur, int targ. Ssn) { if(cur == null) return null; else if(cur. get. Data(). get. Ssn() == targ. Ssn) { return cur. get. Data(); } else { return find(cur. get. Next(), targ. Ssn); } } // find
Linked. List (delete. First)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 delete. First(int targ. Ssn) { if(get. Head() != null) { if(get. Head(). get. Data(). get. Ssn() == targ. Ssn) { set. Head(get. Head(). get. Next()); } else { delete. First(get. Head(), targ. Ssn); } } } // delete. First
Linked. List (delete. First)occurence // Purpose: delete first occurence helper private void delete. First(Node cur, int targ. Ssn) { if(cur. get. Next() != null) { if(cur. get. Next(). get. Data(). get. Ssn() == targ. Ssn) { cur. set. Next(cur. get. Next()); } else { delete. First(cur. get. Next(), targ. Ssn); } } } // delete. First
Linked. List (delete. All)occurences // Purpose: delete all occurences matching a target // SSN // Postcon: All matching occurences will be // eliminated public void delete. All(int targ. Ssn) { // (Extra Credit!!!) }
Linked. List (main) public static void main(String args[]) { Linked. List ell = new Linked. List(); ell. traverse("Empty list traversal"); ell. add(new Student. Record("Adam", 3. 0, 333)); ell. add(new Student. Record("Bozo", 2. 0, 222)); ell. add(new Student. Record("Carl", 1. 0, 444)); ell. traverse. I("Should be 222 333 444"); ell. add(new Student. Record("Doug", 0. 0, 111)); ell. traverse("Should be 111 222 333 444"); ell. delete. First(222); ell. traverse. I("Should be 111 333 444"); ell. delete. First(999); ell. delete. First(333); ell. delete. First(111); ell. delete. First(444); ell. traverse("Empty list? ? ? "); } // main } // Linked. List
Application /* 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 menu. Choice() { 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. read. Int("Choice"); }
Application // Purpose: get information to fill student record // Postcon: returns filled Student. Record public static Student. Record get. SR() { String name = IOGadget. read. Line("Name"); double gpa = IOGadget. read. Double("GPA"); int ssn = IOGadget. read. Int("SSN"); return new Student. Record(name, gpa, ssn); }
Application // print menu and fulfill request public static void menuloop() { Linked. List list = new Linked. List(); int choice; do { choice = menu. Choice(); if(choice == 1) list. add(get. SR()); else if(choice == 2) { Student. Record sr = list. find(IOGadget. read. Int("SSN")); if(sr == null) System. out. println("Not found"); else System. out. println(sr); }
Application else if(choice == 3) list. traverse("Student List"); else if(choice == 4) { int ssn = IOGadget. read. Int("SSN? "); list. delete. First(ssn); } else if(choice == 5) System. out. println("Exiting"); else System. out. println("Illegal choice"); } while(choice != 5); } // menuloop
Application public static void main(String args[]) { menuloop(); } } // class Application
Diagram class Linked. List LLNode head methods… ce tan of LLNode object Stu. Rec data LLNode next Ins ta Ins nc e of f ta n c eo Linked. List object head class LLNode Stu. Rec data LLNode next methods… Ins class Application menuloop { Linked. List list } main { menuloop() } LLNode object Stu. Rec data LLNode next
- Slides: 32