Академический Документы
Профессиональный Документы
Культура Документы
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. Well start with that...
Node
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() { Comments omitted if(data == null) for clarity!!! return "Node: null"; else return "Node: " + data.toString(); }
Are we done?
What about the test main???
Tracing
StudentRecord sr = new StudentRecord("Bob", 3.5, 123456789); name = "Bob" gpa = 3.5 ssn = 123456789
sr
Tracing
Node n1 = new Node(null);
sr
data
n1
next
Tracing
n1.setData(sr);
sr
data
n1
next
Tracing
sr = new StudentRecord("Mary", 3.7, 987654321);
sr
data
n1
next
Tracing
Node n2 = new Node(sr);
sr
data
n1
next
data
n2
next
Tracing
n1.setNext(n2);
sr
data
n1
next
data
n2
next
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 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)
LinkedList
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
LinkedList (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
LinkedList (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
LinkedList (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
LinkedList (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
LinkedList (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
LinkedList (deleteAll)occurences
// Purpose: delete all occurences matching a target // SSN // Postcon: All matching occurences will be // eliminated public void deleteAll(int targSsn) { // (Extra Credit!!!) }
LinkedList (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
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 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"); }
Application
// 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); }
Application
// 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); }
Application
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
Application
public static void main(String args[]) { menuloop(); } } // class Application
Diagram
class Application menuloop { LinkedList list } main { menuloop() } class LinkedList LLNode head class LLNode StuRec data LLNode next methods
methods