数据结构复习(Java实现)——单链表(不带头结点)

Node类:

package stu.cyzhang.danlianbiao;
/**
 * 这是单链表的结点类
 * @author dell
 *@version 1.0
 * @param 
 */
public class Node {

     private T data; //节点的数据
     public Node next; //指向的下一个节点
     
     Node(T data,Node next){
    	 this.data = data;
    	 this.next = next;
     }
     
     public T getData() {
         return data;
     }

}

 SingleLinkList类:

package stu.cyzhang.danlianbiao;

/**
 * 这是一个(不带头结点)单链表实现程序,包括链表的创建、初始化、插入、删除等常用操作
 * @author dell
 * @version 1.0
 *
 */
public class SingleLinkList {

	private Node head;//头结点
	private Node tail;//尾结点
	private int length=0;//链表的长度
	
	//单链表的创建和初始化
	public SingleLinkList() {
		head = tail = null;
	}
	/**
	 * 判断单链表是否为空
	 */
	public boolean isEmpty(){
		return head == null;
	}
	/**
	 * 清空单链表
	 */
	public void claerList(){
		head = tail = null;
		length = 0;
	}
	/**
	 * 在头插入
	 */
	public void insertAtHead(T data){
		head = new Node(data,head);
		if(tail == null)//如果是空链表,就把tail指向head
			tail = head;
		length++;
	}
	/**
	 * 在尾插入
	 */
	public void  insertAtTail(T data){
		if(head == null){
			//空链表处理
			head = new Node(data, null);
			tail = head;
		}else{
			Node newNode = new Node(data, null);
			tail.next = newNode;
			tail = newNode;
		}
		length++;
	}
	
	/**
	 * 获取第index个数据
	 *@param index 指定获取链表中第index个数据 
	 */
	public Node getNodeByIndex(int index){
		if(index < 0 && index >length -1){
			System.out.println("指定位置越界");
		}
		Node node = head;
		for (int i = 0; i < index; i++) {
			node = node.next;
		}
		
		return node;
	}
	
	/**
	 * 插入数据到指定位置
	 * @param index 要插入的位置
	 * @param data 要插入的数据
	 */
	public void insert(int index, T data){
		if(index < 0 || index >length){
			System.out.println("无效的指定位置");
		}
		//在头部插入
		if (index == 0) {
			insertAtHead(data);
		}
		//在中间插入
		else if(index > 0 && index  node = getNodeByIndex(index-1);
			Node tNode = node.next;
			Node newNode = new Node(data, null);
			node.next = newNode;
			newNode.next = tNode;
			length++;
		}
		//在尾部插入
		else{
			insertAtTail(data);
		}
	}
	
	/**
	 * 在指定的数据后面插入
	 * @param appountedItem 指定的数据
	 * @param data 要插入的数据
	 */
	public void insertAtPoint(T appointedItem, T data){
		//空链表处理
		if(isEmpty()){
			insertAtHead(data);
		}
		else {
			Node newNode = new Node(data, null);
			Node tNode = head;
			while(tNode != null && !tNode.getData().equals(appointedItem)){
				tNode = tNode.next;
			}
			Node neNode = tNode.next;
			tNode.next = newNode;
			newNode.next = neNode;
			length++;
		}
	}
	
	/**
	 * 打印链表
	 */
	public void printList(){
		Node pHead = head;
		if(pHead == null){
			System.out.println("空链表");
		}
		while(pHead != null){
			System.out.print(pHead.getData()+" ");
			pHead = pHead.next;
		}
		System.out.println();
	}
	
	/**
	 * 删除指定数据
	 * @param appointedItem 要删除的数据
	 */
	public void deleteByPoint(T appointedItem){
		Node preNode = head;
		Node currNode = head;
		if(isEmpty()){
			System.out.println("删除无效,空链表");
		}else{
			while(currNode != null && !currNode.getData().equals(appointedItem)){
				preNode = currNode;
				currNode = currNode.next;
			}
			if(currNode == null){
				System.out.println("没有指定数据");
			}else{
				preNode.next = currNode.next;
				length--;
			}
		}
	}
	
	/**
	 * 获取数据的索引值
	 * @param appointedItem 要查找的数据
	 * @return index 返回的数据的索引值,返回-1即找不到
	 */
	public int getItemIndex(T appointedItem){
		int index = 0;
		int pos = 0;
		Node iNode = head;
		while(iNode != null && !iNode.getData().equals(appointedItem)){
			iNode = iNode.next;
			pos++;
		}
		if (iNode == null) {
			index = -1;
		}else{
			index = pos;
		}
		return index;
	}
	
}

SingleListTest类:

package stu.cyzhang.danlianbiao;

import org.junit.Test;

public class SingleListTest {
	
	@Test
	public void test(){
		SingleLinkList sll = new SingleLinkList<>();
		sll.insertAtHead("a");
		sll.insertAtTail("c");
//		Node node = sll.getByItemData("b");
//		System.out.println(node.getData());
//		sll.insert(2, "b");

		sll.insertAtPoint("a", "b");

//		sll.claerList();
		sll.printList();
		sll.deleteByPoint("b");
		int index = sll.getItemIndex("a");
		sll.printList();
		System.out.println(index);
	}

}



你可能感兴趣的:(数据结构复习(Java实现)——单链表(不带头结点))