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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.