Doubly linked list

Im trying to figure out how to set up a circular doubly linked list with a dummy head. I posted some code of my attempt to do this. The code is wrong  and doesnt work. What do I need to add or remove to make it work right?  I just want it to function as a circular doubly linked list with dummy head.
Thanks.
public class DLL 
{

	private Node head;
	private int size;
	public DLL()
	{
		head = null;
		size = 0;
	}

	public int getSize()
	{
		return size;
	}
	
	public void setSize()
	{
		this.size = size;
	}
		
}
//Do I need to add more to this class to make it work right?















public class Node 
{
		Node prev, next;
		Object item;
		private Node head;
		
		public Node(Object data, DLL next, DLL prev)
		{
			
		
		this.head = new Node(null, null, null);
		this.head = this.head;
		this.prev = this.head;
		int size = 0;
		}
		public Object getItem()
		{
			return this.item;
		}
		
		public void setItem(Object newItem)
		{
			this.item = newItem;
		}
		
		public Node getNext()
		{
			return this.next;
		}
		
		public void setNext(Node nextNode)
		{
			this.next = nextNode;
		}
	
}
//Do I need to add or switch things in this class with DLL?

Open in new window

theldroAsked:
Who is Participating?
 
Keego7237Commented:
Is this what your looking for?
void run(){
		DLL list = new DLL();
		for(int i=0; i<10; i++){
			list.add(new Node(i));
		}
		System.out.println(list.getSize());
		Node n = list.head;
		while(n.forward != null){ // infinite loop counting 0 to 9 as it goes through the circular list
			System.out.print(n.value);
			n = n.forward;
		}
	}
	
	class DLL{
		Node head;
		int size;
		DLL(){
			size = 0;
		}
		void add(Node newNode){
			if(head == null){
				head = newNode;
				size++;
				return;
			}
			if(head.forward == null){
				newNode.forward = head;
				newNode.backward = head;
				head.forward = newNode;
				head.backward = newNode;
				size++;
				return;
			}
			newNode.forward = head;
			newNode.backward = head.backward;
			head.backward.forward = newNode;
			head.backward = newNode;
			size++;
		}
		int getSize(){
			return size;
		}
		void removeNode(Node n){
			if(n.forward == null)
				head = null;
			else{
				n.forward.backward = n.backward;
				n.backward.forward = n.forward;
				if(n == head)
					n.forward = head;
			}
			size--;
		}
	}
	
	class Node{
		Node forward;
		Node backward;
		int value;
		Node(int value){
			this.value = value;
			forward = null;
			backward = null;
		}
	}

Open in new window

0
 
a_bCommented:
private Node head; --> This in not required in the Node class

You need to add methods to the DLL class to add nodes, delete nodes and to display the linked list
0
 
theldroAuthor Commented:
Thanks a lot. Now that I know what a circular doubly linked list with dummy head node looks like I can try to figure out how to do different things with it. I've only worked with singly linked lists so I only know  how they work.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.