|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Need help with linked list
How can I make this linked list into a doubly linked list?
Code:
public class KLink{
public Node head;
// inner class Node
public class Node{
char data;
Node next;
Node(char a){data = a; next = null;}
}
public KLink(){head = null;}
public void delete(char a){
// empty list
if(head == null){System.out.println("Delete on empty list");}
// first element delete
if(a == head.data){
head = head.next;
return;
}
Node curr = head;
Node prev = null;
while(curr != null){
if(a == curr.data)break;
prev = curr;
curr = curr.next;
}
// remove in middle
if(curr != null){
prev.next = curr.next;
return;
}
// remove from end
prev = null;
}
public void add(char a){
Node addon = new Node(a);
// add to empty list
if(head == null){
head = addon;
return;
}
// add to beginning of list
if(a < head.data){
addon.next = head;
head = addon;
return;
}
// add to middle/end
// find insertion point
Node prev = null;
Node curr = head;
while(curr != null){
if(a < curr.data){
break;
}
prev = curr;
curr = curr.next;
}
// insert in middle
if(curr != null){
prev.next = addon;
addon.next = curr;
return;
}
// add to end
prev.next = addon;
}
public void print(String msg){
Node curr = head;
System.out.println(msg);
if(curr == null){
System.out.println("Empty List");
return;
}
while(curr != null){
System.out.println(curr.data );
curr = curr.next;
}
}
public boolean empty(){
return head == null;
}
}
|
|
#2
|
||||
|
||||
|
If you want a double linked list, you have to add a
previous, so you can go back in your list. Perhaps you could even provide a tail, so you could start at the end of the list. This would give something like this Code:
public Node head;
public Node tail;
// inner class Node
public class Node
{
char data;
Node next;
Node previous;
Node(char a)
{data = a; next = null;}
}
In your insert method, you should add for each next, a previous. This means you would have to change your method like this Code:
public void add(char a)
{
Node addon = new Node(a);
// add to empty list
if (head == null)
{
head = tail = addon;
return;
}
// add to beginning of list
if(a < head.data)
{
addon.next = head;
addon.previous = null;
head = addon;
return; }
// add to middle/end
// find insertion point
Node prev = null;
Node curr = head;
while(curr != null)
{
if(a < curr.data)
{
break;
}
prev = curr;
curr = curr.next;
}
// insert in middle
if(curr != null)
{
prev.next = addon;
addon.previous = prev;
addon.next = curr;
curr.previous = addon;
return;
}
// add to end
prev.next = addon;
addon.previous = prev;
}
I must admit I haven't compiled this code to test it. I'll leave that to you.ANd for the deletion, it should be similar. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Need help with linked list |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|