Java-------Single-linked list

Source: Internet
Author: User

Single Linked list:
* 1. A linked list can be an ordered or unordered listing
* 2. The contents of the linked list are usually stored in memory.
* 3. The list is composed of nodes, each node has the same structure
* 4. Node is divided into data domain and chain domain, data domain holds node content, chain field holds pointer of next node

Package mylinklist;

public class Mylinkedlist<t> {

/**
*node: Node Object
* Includes data field and link field next (Refers to next node object)
*/
Class Node {
Private T data;
Private Node Next;
Public Node () {

}
Node initialization
Public Node (T Data,node next) {
This.data = data;
This.next = Next;
}
}

Private node header;//linked header node
Private node tailer;//linked footer node
private int size;//Linked list length (number of nodes)


/**
* Linked list initialization
*/
Public mylinkedlist () {//NULL parameter construction
Header = null;
Tailer = null;
}
Public mylinkedlist (T data) {//Parametric construction
Header = new node (data,null);//Create head node
Tailer = header;
size++;
}

/**
* To find the length of the chain table
* @return
*/
public int GetSize () {
return size;
}

/**
* Returns the data of the node indexed as index
* @param index
* @return
*/
Public T get (int index) {
if (Index < 0 | | | index > SIZE-1)
throw new Indexoutofboundsexception ("Index Out of Bounds");
Return Getindexnode (index). data;
}

Public Node getindexnode (int index) {
if (Index < 0 | | | index > SIZE-1)
throw new Indexoutofboundsexception ("Index Out of Bounds");
Node current = header;
for (int i = 0;i < size; i++) {
if (i = = index) {
return current;
}
current = Current.next;
}
return null;
}

/**
* Returns element in the linked list position, or 1 if not present
* @param tdata
* @return
*/
public int GetIndex (T element) {
if (element = = null)
return-1;
Node current = header;
for (int i = 0; i < size; i++) {
if (Current.data = = Element) {
return i;
}
current = Current.next;
}
return-1;
}



/**
* add element at end of list
* @param element
*/
public void Add (T element) {
node n = new node (element,null);
if (header = = null) {
Header = N;
Tailer = header;
}else{
Tailer.next = n;
Tailer = n;
}
size++;
}

/**
* add element to the list header
* @param element
*/
public void Addatheader (T element) {
Header = new Node (Element,header);
if (tailer = = null) {
Tailer = header;
}
size++;
}

/**
* Insert an element behind the index position
* @param index
* @param element
*/
public void Insert (int index,t element) {
if (index<0 | | index>size-1) {
throw new Indexoutofboundsexception ("Index Out of Bounds");
}
if (header==null) {
Add (Element);
}else{
if (index==0) {
Addatheader (Element);
}else{
Node current = Getindexnode (index);
Node Insertnode = new node (element,current.next);
Current.next = Insertnode;
size++;
}
}
}
/**
* Remove nodes from any location
* @param index
* @return
*/
Public T deletenode (int index) {
if (index<0 | | index>size-1)
throw new Indexoutofboundsexception ("Index Out of Bounds");
if (index = = 0) {//delete element in head
Node n = header;//Record header nodes
Header = header.next;//Pointing the head pointer to the next node
size--;
Return n.data;//the contents of the Output record node
}else{//Delete in other locations
Node current = Getindexnode (index);
Node pre = Getindexnode (index-1);//Get previous node
Pre.next = current.next;//Sets the link field of the previous node to null
size--;
Return current.data;//returns the data field of the deleted node
}


}
/**
* Delete head node
* @return
*/
Public T Deleteheader () {
Return Deletenode (0);
}
/**
* Delete Tail node
* @return
*/
Public T Deletetailer () {
Return Deletenode (size-1);
}

Emptying nodes
public void Clear () {
Header = null;
Tailer = null;
size = 0;
}

/**
* ToString ();
*/
Public String toString () {
if (size = = 0)
Return "[]";
Node current = header;
StringBuilder sb = new StringBuilder ();
Sb.append ("[");
while (current.next! = null) {
Sb.append (Current.data). Append ("");
current = Current.next;
}
Sb.append (Current.data). Append ("]");
return sb.tostring ();
}


public static void Main (string[] args) {
mylinkedlist<string> link = new mylinkedlist<> ();
Link.add ("header");
Link.add ("11");
Link.add ("22");
Link.add ("33");
Link.addatheader ("Newheader");
Link.insert (2, "1.5");;

System.out.println (Link.getindex ("11"));
System.out.println (Link.getindex ("88"));
System.out.println (link.get (0));
System.out.println (Link.getsize ());
System.out.println (Link.deleteheader ());
System.out.println (Link.deletetailer ());
System.out.println (Link.deletenode (1));
System.out.println (link);
Link.clear ();
System.out.println (link);

}

}

Java-------Single-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.