The Artima Developer Community
Sponsored Link

Java Answers Forum
Help me to make a Link List method

5 replies on 1 page. Most recent reply: Sep 29, 2004 2:15 PM by Kondwani Mkandawire

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Mele2004m

Posts: 10
Nickname: nenny
Registered: Sep, 2004

Help me to make a Link List method Posted: Sep 22, 2004 5:31 PM
Reply to this message Reply
Advertisement
1)Write an InsertLast(int Key, int Age) method for a linked list

2)Write a DisplayKey(int Key) method for a linked list.

3)Write a DeleteLast() method for a linked list.

4)Write a DeleteKey(int Key) method for a Sorted Link


KevinT

Posts: 24
Nickname: metzedler
Registered: Jun, 2003

Re: Help me to make a Link List method Posted: Sep 23, 2004 11:25 PM
Reply to this message Reply
Hi so you want your linked list to be sorted while insert.

what is the role of the Key here ? like in InsertLast it simply inserted to last, why do we need the key here ?

Mele2004m

Posts: 10
Nickname: nenny
Registered: Sep, 2004

Re: Help me to make a Link List method Posted: Sep 26, 2004 10:26 PM
Reply to this message Reply
Key is like the data where we insert to the linkList

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Help me to make a Link List method Posted: Sep 28, 2004 3:50 PM
Reply to this message Reply
I'm lost here. It sounds like a very odd
Hash table if it was some sort of simulation
of a bag with a contiguous data collection
(somewhat of an array) what you called Key
would infact be called index (that's just
preference though). The fact that its an
int though makes it a little odd. Coz of confusion
of the Duplicates...

Brief us a little more about this bag structure you
are creating...

Regards,
Spike

Mele2004m

Posts: 10
Nickname: nenny
Registered: Sep, 2004

Re: Help me to make a Link List method Posted: Sep 28, 2004 6:28 PM
Reply to this message Reply
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;
}
}

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Help me to make a Link List method Posted: Sep 29, 2004 2:15 PM
Reply to this message Reply
Hi there:

Just going through your code. I must say your
display method is extremely odd.


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;

}
}



I'm assuming that you are trying to display
something with the argument "key" you are taking
in a parameter named "key" which never gets used
in this method (why is it bieng taken in)?

Note: inputLink.key is not the same as the
key parameter which is to be supplied as an
argument when the method is being used.
You bring in parameter "key": displayData(int key)
but this never gets used.

Maybe you should alter your code or explain why
you did this then we'll try and work it from
there...

Hope this helps...

Good luck...

Spike

Flat View: This topic has 5 replies on 1 page
Topic: Handling Alt+Tab Previous Topic   Next Topic Topic: for the experts a ques 4m a novicee

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use