HELP!! Java LinkedLists!

DrunkPotHead

Diabloii.Net Member
HELP!! Java LinkedLists!

I have a LinkedListManager class(global variable front, which is head of the linked list), and inside of it, i have a node class

the node class has 2 public variables i can access:

Node next;
and
Object x;

Let's say i want to remove an item from the middle of the list.

The object can be identified by some parameter, so if i search through the linked list using a while(temp.next!=null) loop, i can get a number back, of how many times i need to use temp=temp.next to get the object

So can any1 post a function here that will get rid of one element in the middle of the linked list?

Or post here if i need to clarify something.
 

adamfgt78

Diabloii.Net Member
Let's see, its been awhile since I learned this(or didn't learn this).....

Basically, I think you'll want to traverse the list until you find the node that is before the target node. Then you'll want to have the .next point to the node after the target node. Written in pseudocode since I don't remember the exact Java syntax, the while loop might look something like this:

while(temp.next!=null)
{
if (temp.next.x==targetvalue)
{
temp.next = temp.next.next
{
}

BUT, while using Java, aren't there built in linked list functions? And the linked lists in Java are known as vectors? So all you'd have to do is look up the function to delete a vector node and call it and pass it the target value or place in the vector.

Please take this information with a grain of salt. Its been about 5 years since I learned this material and I could be wrong.
 

DrunkPotHead

Diabloii.Net Member
The thing with your code is that temp.next=temp.next.next is the same thing as temp=temp.next

and u also forgot to put temp=temp.next in the loop, so it won't be infinite
 

adamfgt78

Diabloii.Net Member
DrunkPotHead said:
The thing with your code is that temp.next=temp.next.next is the same thing as temp=temp.next

and u also forgot to put temp=temp.next in the loop, so it won't be infinite
Hmm, I think that I'm used to working with pointers in C while Java, if I recall, doesn't have pointers. So the code that I'm thinking of in C probably doesn't directly translate to Java. In C, a node contains the data variables and a pointer to the next node. Pointers are basically memory addresses, so when you're on the node before the target, you're going to want to modify the pointer (temp.next) to point to the node 2 nodes after it. The address of this node is stored in the target node's (temp.next) pointer to the next node (temp.next.next). Here is a function written in C++ which I got from here:

http://www.codeguru.com/Cpp/Cpp/cpp_mfc/lists/article.php/c783/

Note that we both forgot the cases where the list is empty and where the target node is the head.



bool CLinkedList::DeleteNode(int nData)
{
// first case to check the list empty
if (!m_pLinkedList)
return false;

// second case to check for the deleted node if it is the head
if (m_pLinkedList->nData == nData)
{
// you need to hold on to your pNext
CNode *pConnectAgain = m_pLinkedList->pNext;
delete m_pLinkedList;

// you need to connect it again.
m_pLinkedList = pConnectAgain;

return true;
}

CNode *pNode = m_pLinkedList;

// third case, your traversing would always be in terms of the pNext
while (pNode->pNext)
{
// your checking would be in terms of the pNext
if (pNode->pNext->nData == nData)
{
CNode *pDeleteNode = pNode->pNext;

// alot of pNext pointers, don't you think ?
pNode->pNext = pNode->pNext->pNext;
delete pDeleteNode;

return true;
}

pNode = pNode->pNext;
}

return false;
}
 

m1rage

Diabloii.Net Member
Try drawing a picture of each object and the reference to the next object, it helps a lot when visualizing the problem. Also check out the premade Linked List API, I'm sure whatever class you are using is probably using the premade class.

You can do a few things...just use a counter or static variable to count the number of iterations it takes to reach the desired object. Then, all you have to do is take that number, add one (this will give the reference to the object sequentially after the one you wish to remove), and copy the reference to that object (lets call the reference 'nextObj'). Also, lets call the reference before the one you wish to remove 'previousObj.' Then all you do is set 'previousObj.next = nextObj.' Since nothing references the object you wish to remove, it will be cleaned by the GC.

Let me know if it this helps :thumbsup:
 
Top