Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 13th, 2005, 05:05 AM
jakeklem jakeklem is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 1 jakeklem User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 42 m 35 sec
Reputation Power: 0
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;
 	}
 }
 

Reply With Quote
  #2  
Old May 1st, 2005, 05:13 AM
gertcuppens's Avatar
gertcuppens gertcuppens is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 118 gertcuppens User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 28 m
Reputation Power: 5
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Need help with linked list


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT