Linklist (java edition, contain rear)

Source: Internet
Author: User

Complexity with rear, end-of-insertion o (1)
Package linearlist;
After the implementation of the post-insertion and delete are easier, as far as possible in this direction to convert
public class Linearlist {
Class Node {
private int data;
Private node next;
Public node (int Data,node Next) {
this.data=data;
this.next=next;}
}
Private node head;
Private node Rear;//two references, pointing to an empty
private int length=0;
Public Linearlist () {
Head=new node (0,null);
rear=head;
}
void AddFirst (int Data)//head plug
{if (head.next==null) {
Node N=new node (data,null);
head.next=n; rear=n;length++;}
else {node n=new node (data,head.next); head.next=n;length++;}}
void AddLast (int Data)//tail plug
{node n=new Node (data,null);
rear.next=n;
rear=n;length++;}
void Addafter (node N,int data)//post-insertion
{node n1=n.next;
If (n1==null) {
Node N2=new node (data,null);
n.next=n2;rear=n2;length++;
}
else{
Node N2=new node (data,n.next);
n.next=n2;length++;
}
}
void Addbefore (node n,int data)//front plug
{addafter (n,n.data);
N.data=data;//no length++;
}
int Removefirst ()//header Delete
{if (head.next ==null) {return 0;}
If (head.next==rear) {
int data=rear.data;
head.next=null;
rear=head;length--; Return data;}
int data=head.next.data;
head.next=head.next.next;length--;
Return data;
}
int Removelast ()//tail Delete
{node N=head;int data=rear.data;
While (n!=null) {
If (n.next.equals (rear))
{n.next=null;rear=n;}
n=n.next;
}length--;
Return data;
}

int Removeafter (node N)//post-delete
{if (n.next==null) return 0;
Node N1=n.next;int data=n1.data;
If (n1.next==null) {
N.next =null;
rear=n;}
n.next=n1.next;
length--;
Return data;}
int Removebefore (node N)//pre-delete
{node n1=head.next;
If (n1==n) return 0;
While (n1!=null) {
If (n1.next.next.equals (N)) {
return Removeafter (n1);} When you encounter the first node that satisfies a condition, you jump out
n1=n1.next;
}//no length--;
Return 0;
}
int Ofdex (int Data)//search Order
{node N=head;int i=0;
While (n!=null) {
If (n.data==data) break;
n=n.next;i++;
}
Return i;
}
Boolean Isempty ()//empty
{if (head.next==null)
Return true;
Return false;}

int contains (int data)//sentenced to include
{node n=head;
While (n!=null) {
If (n.data==data) return 1;
n=n.next;
}
return-1;
}
Node Find (int data)//find nodes
{node n=head;
While (n!=null) {
If (n.data==data) return n;
n=n.next;
}
Return null;
}
public void Clear ()//empty
{head.next=null;
rear=head; length=0;
}
int size ()//linked List length
{return length;}
void Traverse ()//traversal
{if (head.next==null) {System.out.println ("the list is empty"); return;}
Node n=head.next;
For (int I=0;i<length;i++) {
System.out.println (n.data);
n=n.next;
}
}

Linklist (java edition, contain rear)

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.