java-------單鏈表

來源:互聯網
上載者:User

標籤:print   索引   size   資料   ext   oid   build   頭部   tin   

單鏈表:
* 1.鏈表可以是一種有序或無序的列表
* 2.鏈表的內容通常儲存在記憶體中分散的為止
* 3.鏈表由節點群組成,每一個節點具有相同的結構
* 4.節點分為資料域和鏈域,資料域存放節點內容,鏈域存放下一個節點的指標

 

package myLinkList;

public class MyLinkedList<T> {

/**
*Node:節點對象
*包括資料域data和鏈域next(指向下一個節點對象)
*/
class Node {
  private T data;
  private Node next;
  public Node(){

  }
  //節點初始化
  public Node(T data,Node next){
    this.data = data;
    this.next = next;
  }
}

private Node header;//鏈表前端節點
private Node tailer;//鏈表尾節點
private int size;//鏈表長度(節點個數)


/**
* 鏈表初始化
*/
public MyLinkedList() {//空參構造
  header = null;
  tailer = null;
}
public MyLinkedList(T data) {//有參構造
  header = new Node(data,null);//建立頭結點
  tailer = header;
  size++;
}

/**
* 求鏈表長度
* @return
*/
public int getSize() {
  return size;
}

/**
* 返回索引為index的節點的資料
* @param index 索引
* @return
*/
public T get(int index) {
  if(index < 0 || index > size-1)
    throw new IndexOutOfBoundsException("索引越界");
  return getIndexNode(index).data;
}

public Node getIndexNode(int index){
  if(index < 0 || index > size-1)
    throw new IndexOutOfBoundsException("索引越界");
  Node current = header;
  for(int i = 0;i < size; i++) {
    if(i == index) {
    return current;
  }
  current = current.next;
}
  return null;
}

/**
* 返回element在在鏈表位置,如果不存在,則返回-1
* @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;
}



/**
* 在鏈表末端添加element
* @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++;
}

/**
* 在鏈表頭部添加element
* @param element
*/
public void addAtheader(T element) {
  header = new Node(element,header);
  if(tailer == null){
    tailer = header;
  }
  size++;
}

/**
* 在index位置後邊插入元素
* @param index
* @param element
*/
public void insert(int index,T element) {
  if(index<0 || index>size-1) {
    throw new IndexOutOfBoundsException("索引越界");
  }
  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++;
    }
  }
}
/**
* 刪除任意位置的節點
* @param index
* @return
*/
public T deleteNode(int index){
  if(index<0 || index>size-1)
    throw new IndexOutOfBoundsException("索引越界");
  if(index == 0){//在頭部刪除元素
    Node n = header;//記錄前端節點
    header = header.next;//將頭指標指向下一個節點
    size--;
    return n.data;//輸出記錄節點的內容
  }else{//在其他位置刪除
    Node current = getIndexNode(index);//擷取當前節點
    Node pre = getIndexNode(index-1);//擷取前一個節點
    pre.next = current.next;//將前一個節點的鏈域設為null
    size--;
    return current.data;//返回刪除節點的資料域
  }


}
/**
* 刪除前端節點
* @return
*/
public T deleteHeader(){
  return deleteNode(0);
}
/**
* 刪除尾節點
* @return
*/
public T deleteTailer(){
return deleteNode(size-1);
}

//清空節點
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-------單鏈表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.