12. Java implementation of doubly linked list

Source: Internet
Author: User
Tags stub

Schematic diagram:

Operation Result:

Node Code:

public class Node {
int data;
Node Next;
Node previous; Forward pointer

public Node (int value) {
TODO auto-generated Constructor stub
This.data = value;
}

public void display ()
{
System.out.print (data+ "");
}
}

Doublelinklist:

public class Doublelinklist {
Private Node first;
Private Node last; Tail knot Point

Public Doublelinklist () {
TODO auto-generated Constructor stub
First =null;
last = null;
}

public void Insertfirst (int value)//header inserted into
{
Node node = new node (value);
if (IsEmpty ())
{
First = node;
Last =node;
}
Else
{
Node.next = First;
first.previous = node;
First = node;

}

}

public void Insertlast (int value)//Insert node from tail
{
Node node = new node (value);
if (IsEmpty ())
{
First = node;
Last =node;
}
Else
{
Last.next =node;
Node.previous = Last;
last = node;
}
}

public void Deletefirst ()//Delete head node
{

if (First.next ==null)
{
First =null;
Last=null;
}
Else
{
Node tmp = First.next;
Tmp.previous =null;
first= tmp;
if (first = = null)
Last =null;
}

}

public void Deletelast ()//delete from tail
{
if (last.previous = = null)
{
Last =null;
First=null;
}
Else
{
Last.previous.next =null;
last = last.previous;
}

}


public void find (int value)//Find data
{
Node current = first;
while (current.data! = value)
{
Current =current.next;
}
Current.display ();
}

public void Finddelete (int value)//Find data and delete
{
Node current = first;

if (First.data ==value)
{
First =first.next;
}
Else
{
while (current.data! = value)
{
Current =current.next;
}
Current.previous.next = Current.next;
}




}

public boolean IsEmpty ()//Determine if the linked list is empty
{
return (first = = null);
}


public void display ()//Show All nodes
{
Node current = first;
while (current! = null)
{
Current.display ();
Current =current.next;
}
}


}

12. Java implementation of a doubly linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.