Mele2004m
Posts: 10
Nickname: nenny
Registered: Sep, 2004
|
|
Re: Help me to make a Link List method
|
Posted: Sep 28, 2004 6:28 PM
|
|
This a pseudo code of a linklist class for making method but it's not work correctly... so can you fixed it. /******************** //InsertLast Method *******************/ //InsertLast(int Key, int Age) method for a linked list public void insertLast(int key, int age){ //Decalre and instantiate the link Link newLink = new Link(key, age); Link lastLink = firstLink; if(firstLink == null){ //Make a firstLink equal to new link firstLink = newLink; } else { //Look for the link it's equal to null then //insert the data or link after link that is //equal null while(lastLink != null) { //Then move to the next link lastLink = lastLink.nextLink; } newLink.nextLink = null; lastLink = newLink; } } /******************** //DisplayKey Method *******************/ //DisplayKey(int Key) method for a linked list. public Link displayData(int key) { //Make a variable that find the key Link inputLink = firstLink; int searchKey = 10; int searchKey1 = 30; int searchKey2 = 50; //If the first link equal null then //return null if(firstLink == null) { return null; } else { while(inputLink.key != searchKey) { inputLink = inputLink.nextLink; } System.out.println("Last Key"); inputLink.displayData(); return inputLink; } } /******************** //DeleteLast Method *******************/ //DeleteLast() method for a linked list. public Link deletedLast () { Link lastLink = firstLink; //If it equals to null then return null //If not equal null then delete Key if(firstLink == null) { return null; } else { while (lastLink.nextLink != null) { lastLink = lastLink.nextLink; } return lastLink; } } /******************************** //DeleteKey Method in sorted link *********************************/ //DeleteKey(int Key) method for a Sorted Link. public Link deleteKey() { if (firstLink == null) { //No links to delete return null; } else { //Save reference to deleted link Link deletedLink = firstLink; //Delete it: this.firstLink = this.firstLink.nextLink firstLink = firstLink.nextLink; //Return deleted link return deletedLink; } }
|
|