CIS265 Lecture Notes: Custom Made Linked Lists
Total Page:16
File Type:pdf, Size:1020Kb
CIS265 Lecture Notes: Custom Made Linked Lists
PERSON class public class Person implements Comparable
private String name; private int age; // ------public Person(String name, int age) { super(); this.name = name; this.age = age; } // ------
public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } // ------public String toString() { return " Name:" + name + " Age:" + age; } // ------@Override public int compareTo(Person otherPerson) { if ( this.age == otherPerson.getAge() ) return 0; else if ( this.age > otherPerson.getAge()) return 1; else return -1; } } NODE class public class Node< E extends Comparable
private E element; private Node
// ------public Node(E dataObject) { //Person p = new Person(theName, theAge); element = dataObject; next = null; previous = null; } // ------
public E getElement() { return element; }
public void setElement(E element) { this.element = element; }
public Node
public void setNext(Node
public Node
public void setPrevious(Node
// ------
public String showNode() { return element.toString(); }
@Override public int compareTo(Node
private Node
public void setHead(Node
public Node
public void setLast(Node
public int getCount() { return count; }
public void setCount(int count) { this.count = count; }
// ------// insert new node at the end of the list public void add(Node
if (count == 0) { head = newNode; last = newNode; count = 1; return; }
count++; last.setNext(newNode); newNode.setPrevious(last); last = n; } // ------// add new node in the list’s natural order public void addInOrderVersion1(Node
count++; Node
if (ptr == null) { last.setNext(newNode); newNode.setPrevious(last); last = newNode; } else { newNode.setNext(ptr); newNode.setPrevious(ptr.getPrevious()); if (ptr == head) { head = newNode;
} else { ptr.getPrevious().setNext(newNode); ptr.setPrevious(newNode); } }
} // ------public void addInOrderVersion2(Node
// new node goes at the beginning of the list if ( head.compareTo(newNode) > 0 ){ //new node is smaller than first element newNode.setNext(head); head.setPrevious(newNode); head = newNode; return; } // new node goes at the end of the list if ( last.compareTo(newNode) < 0 ){ //new node is larger than last element newNode.setPrevious(last); last.setNext(newNode); last = newNode; return; }
Node
while ( ptr != null) { if ( newNode.compareTo(ptr) < 0) break; prior = ptr; ptr = ptr.getNext();
}// while
// place news node in between existing elements newNode.setNext(ptr); newNode.setPrevious(prior); prior.setNext(newNode); ptr.setPrevious(newNode);
}
// ------public void showList() { System.out.printf("\n<>\n", head, last, count); E element; Node
} DRIVER package csu.matos; public class Driver1 {
/** * Creating a custom-made linked list holding Person data * */ public static void main(String[] args) {
Node
Node
head = p1; tail = p3;
p1.setNext(p2); p2.setNext(p3); p2.setPrevious(p1); p3.setPrevious(p2);
ptr = head; while (ptr != null) { System.out.println(ptr + "\t" + ptr.showNode()); ptr = ptr.getNext(); }
System.out.println("Here is the list");
// ------
LinkedList
list.add (p1); list.add(p2); list.add(p3);
list.showList();
System.out.println("Here is the list-after addInOrder");
list.addInOrderVersion2(new Node
list.showList();
}// main } CONSOLE
csu.matos.Node@2a9931f5 Name:AAA Age:111 csu.matos.Node@2f9ee1ac Name:BBB Age:222 csu.matos.Node@67f1fba0 Name:CCC Age:333
Here is the list
<> csu.matos.Node@2a9931f5 Name:AAA Age:111 csu.matos.Node@2f9ee1ac Name:BBB Age:222 csu.matos.Node@67f1fba0 Name:CCC Age:333
Here is the list-after addInOrder
<> csu.matos.Node@2a9931f5 Name:AAA Age:111 csu.matos.Node@1ee7b241 Name:XXX Age:150 csu.matos.Node@2f9ee1ac Name:BBB Age:222 csu.matos.Node@67f1fba0 Name:CCC Age:333 VERSION 2
Minor changes are made here to enhance the management of Generic Data Objects accepted by the custom made doubly- linked list
.
DRIVER package csu.matos; public class Driver {
/** * Author: V. Matos * Date: 4-11-2013 * Goal: Create a generic UDT (User-defined Data Type) * to support linked-list operations. * Minor changes to enhance the previous version * (look for Generic-Data-Type and treatment of toString) */ public static void main(String[] args) {
// create a custom-made doubly-linked list MyList
mylist.addLast(new Person("Daenerys", "123456789")); mylist.addLast(new Person("Arya", "111222333")); mylist.addLast(new Person("Sansa", "222333444")); mylist.addLast(new Person("Tyrion", "333444555")); mylist.addLast(new Person("John", "444555666"));
System.out.println( mylist.showControlData() ); mylist.showForward(); mylist.showBackward();
System.out.println("\nSearching...."); String key = "222333444"; Node
// try to delete node found above if ( mylist.removeNode(foundNode) ) { System.out.println("After attempt to delete..."); mylist.showForward(); }
// insert Person at a given position (fake ArrayLit's insert) if ( mylist.insert( 2, new Person("Brain", "999888777")) ) { mylist.showForward(); }
}//main } MyList Class package csu.matos; public class MyList
// contructor public MyList() { this.first = null; this.last = null; this.count = 0; } // mutators public Node
// ------// Add new node at the end of the list: public int addLast(E data){ // place given data in a new node n Node
if ( count == 0){ first = newNode; last = newNode; } else { // place new element at the end of the existing list last.setNext(newNode); newNode.setPrevious(last); last = newNode; } return ++count; }
// Add new node at the end of the list: public int addFirst(E data) { // place given data in a new node n Node
if (count == 0) { first = newNode; last = newNode; } else { // place new element at the beginning of the existing list first.setPrevious(newNode); newNode.setNext(first); first = newNode; } return ++count; } public Node
return null; }//find public void showForward(){ int index = 0; System.out.println("\nTraversing list (forward ...)" + count); Node
// remove node at give memory location public boolean removeNode(Node
if ( count == 1 && ((nodeToBeDeleted == first) || (nodeToBeDeleted == last ))){ first = last = null; count= 0; return true; } // Check cases: nodeToBeDeleted is first, last, or in between Node
if ( nodeToBeDeleted == first){ right.setPrevious(null); first = right; return true; }
if ( nodeToBeDeleted == last){ left.setNext(null); last = left; return true; }
// NAIVE: assuming nodeToBeDeleted is in current chain // (a better way is to keep extra ptr. to control node) left.setNext(right); right.setPrevious(left); return true; }//removeNode
public boolean insert(int pos, E newData) { Node
// add newData at the end of the list; if ( pos == count -1){ addLast(newData); return true; }
if ( pos == 0 ){ addFirst(newData); return true; }
Node
} Node Class package csu.matos; public class Node
// constructor public Node(E data) { this.data = data; this.next = null; this.previous = null; } // mutators
public Node() { this.data = null; this.next = null; this.previous = null; } public E getData() { return data; } public void setData(E data) { this.data = data; } public Node
public Node
public void setPrevious(Node
// user-defined methods public String showData() { String format = "\n[NODE \tCURR:\t %s ]\n" + "[NODE \tNEXT:\t %s\n\tPREVIOUS:%s \n\tDATA:\t %s ]"; return String.format(format, this, this.getNext(), this.getPrevious(), this.getData() ); } } Person Class
package csu.matos; public class Person implements Comparable
// constructor(s) public Person(String name, String ssn) { super(); this.name = name; this.ssn = ssn; thisPerson = this; }
public Person() { super(); this.name = "n.a"; this.ssn = "-1"; thisPerson = this; }
// mutator(s) public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getSsn() { return ssn; }
public void setSsn(String ssn) { this.ssn = ssn; } // ======// user-defined methods
public String showData(){ return String.format( "\n\t[PERSON LOC: %s]\n\t[PERSON NAME:%s SSN:%s]", this, this.getName(), this.getSsn() ); }
@Override public String toString(){ // done this way to stop recursion-observe that plain: this // calls this.toString(). String thisLoc = getClass().getName() + '@' + Integer.toHexString( hashCode() ); //return thisLoc; return String.format( "\n\t[PERSON LOC: %s]\n\t[PERSON NAME:%s SSN:%s]", thisLoc, this.getName(), this.getSsn() );
}
// compare Person data with a give SSN key-value @Override public int compareTo(String key) { String mySsn = this.getSsn(); if (mySsn.compareTo(key) == 0) return 0; else if (mySsn.compareTo(key) > 0) return 1; else return -1; } }
CONSOLE
CONTROL FIRST:csu.matos.Node@62fcf06c LAST: csu.matos.Node@4c0c7539 COUNT:5
Traversing list (forward ...)5 0 [NODE CURR: csu.matos.Node@62fcf06c ] [NODE NEXT: csu.matos.Node@45e41830 PREVIOUS:null DATA: [PERSON LOC: csu.matos.Person@1f01b29] [PERSON NAME:Daenerys SSN:123456789] ] 1 [NODE CURR: csu.matos.Node@45e41830 ] [NODE NEXT: csu.matos.Node@3a8721bd PREVIOUS:csu.matos.Node@62fcf06c DATA: [PERSON LOC: csu.matos.Person@7db81d4f] [PERSON NAME:Arya SSN:111222333] ] 2 [NODE CURR: csu.matos.Node@3a8721bd ] [NODE NEXT: csu.matos.Node@428c6e04 PREVIOUS:csu.matos.Node@45e41830 DATA: [PERSON LOC: csu.matos.Person@118aeabe] [PERSON NAME:Sansa SSN:222333444] ] 3 [NODE CURR: csu.matos.Node@428c6e04 ] [NODE NEXT: csu.matos.Node@4c0c7539 PREVIOUS:csu.matos.Node@3a8721bd DATA: [PERSON LOC: csu.matos.Person@373968f1] [PERSON NAME:Tyrion SSN:333444555] ] 4 [NODE CURR: csu.matos.Node@4c0c7539 ] [NODE NEXT: null PREVIOUS:csu.matos.Node@428c6e04 DATA: [PERSON LOC: csu.matos.Person@1a18c28a] [PERSON NAME:John SSN:444555666] ]
Traversing list (backward ...)5 4 [NODE CURR: csu.matos.Node@4c0c7539 ] [NODE NEXT: null PREVIOUS:csu.matos.Node@428c6e04 DATA: [PERSON LOC: csu.matos.Person@1a18c28a] [PERSON NAME:John SSN:444555666] ] 3 [NODE CURR: csu.matos.Node@428c6e04 ] [NODE NEXT: csu.matos.Node@4c0c7539 PREVIOUS:csu.matos.Node@3a8721bd DATA: [PERSON LOC: csu.matos.Person@373968f1] [PERSON NAME:Tyrion SSN:333444555] ] 2 [NODE CURR: csu.matos.Node@3a8721bd ] [NODE NEXT: csu.matos.Node@428c6e04 PREVIOUS:csu.matos.Node@45e41830 DATA: [PERSON LOC: csu.matos.Person@118aeabe] [PERSON NAME:Sansa SSN:222333444] ] 1 [NODE CURR: csu.matos.Node@45e41830 ] [NODE NEXT: csu.matos.Node@3a8721bd PREVIOUS:csu.matos.Node@62fcf06c DATA: [PERSON LOC: csu.matos.Person@7db81d4f] [PERSON NAME:Arya SSN:111222333] ] 0 [NODE CURR: csu.matos.Node@62fcf06c ] [NODE NEXT: csu.matos.Node@45e41830 PREVIOUS:null DATA: [PERSON LOC: csu.matos.Person@1f01b29] [PERSON NAME:Daenerys SSN:123456789] ]
Searching.... FOUND [PERSON LOC: csu.matos.Person@118aeabe] [PERSON NAME:Sansa SSN:222333444] After attempt to delete...
Traversing list (forward ...)4 0 [NODE CURR: csu.matos.Node@62fcf06c ] [NODE NEXT: csu.matos.Node@45e41830 PREVIOUS:null DATA: [PERSON LOC: csu.matos.Person@1f01b29] [PERSON NAME:Daenerys SSN:123456789] ] 1 [NODE CURR: csu.matos.Node@45e41830 ] [NODE NEXT: csu.matos.Node@428c6e04 PREVIOUS:csu.matos.Node@62fcf06c DATA: [PERSON LOC: csu.matos.Person@7db81d4f] [PERSON NAME:Arya SSN:111222333] ] 2 [NODE CURR: csu.matos.Node@428c6e04 ] [NODE NEXT: csu.matos.Node@4c0c7539 PREVIOUS:csu.matos.Node@45e41830 DATA: [PERSON LOC: csu.matos.Person@373968f1] [PERSON NAME:Tyrion SSN:333444555] ] 3 [NODE CURR: csu.matos.Node@4c0c7539 ] [NODE NEXT: null PREVIOUS:csu.matos.Node@428c6e04 DATA: [PERSON LOC: csu.matos.Person@1a18c28a] [PERSON NAME:John SSN:444555666] ]
Traversing list (forward ...)5 0 [NODE CURR: csu.matos.Node@62fcf06c ] [NODE NEXT: csu.matos.Node@45e41830 PREVIOUS:null DATA: [PERSON LOC: csu.matos.Person@1f01b29] [PERSON NAME:Daenerys SSN:123456789] ] 1 [NODE CURR: csu.matos.Node@45e41830 ] [NODE NEXT: csu.matos.Node@4f8bff68 PREVIOUS:csu.matos.Node@62fcf06c DATA: [PERSON LOC: csu.matos.Person@7db81d4f] [PERSON NAME:Arya SSN:111222333] ] 2 [NODE CURR: csu.matos.Node@4f8bff68 ] [NODE NEXT: csu.matos.Node@428c6e04 PREVIOUS:csu.matos.Node@45e41830 DATA: [PERSON LOC: csu.matos.Person@702d2da4] [PERSON NAME:Brain SSN:999888777] ] 3 [NODE CURR: csu.matos.Node@428c6e04 ] [NODE NEXT: csu.matos.Node@4c0c7539 PREVIOUS:csu.matos.Node@4f8bff68 DATA: [PERSON LOC: csu.matos.Person@373968f1] [PERSON NAME:Tyrion SSN:333444555] ] 4 [NODE CURR: csu.matos.Node@4c0c7539 ] [NODE NEXT: null PREVIOUS:csu.matos.Node@428c6e04 DATA: [PERSON LOC: csu.matos.Person@1a18c28a] [PERSON NAME:John SSN:444555666] ]