單鏈表對象操作--java

之前寫了關於單鏈表操作是以 int 型爲例,但是在時間應用中,鏈表很少僅僅存儲數字,更多的是對象。那麼這一篇以存儲對象爲例,簡單介紹如何對單鏈表進行簡單的操作。

注意事項:刪除與增添操作應注意鏈表的長度,不可溢出,用不專業的話講,溢出分爲“上溢”和“下溢”,上溢指的是所刪除/插入的數據位置超過鏈表長度;下溢出指的是刪除/插入的數據位置爲負。

比如,鏈表長度爲2,現在執行操作要求刪除位置爲4的數據,顯然這是會報錯的。

比如,鏈表爲空,現在要求刪除數據,也是會報錯的。

比如,要求刪除位置爲-1的數據,位置-1是不存在的。

具體代碼詳見如下,若是剛剛接觸鏈表,建議先看前一篇,以 int 型爲例的。在閱讀過程中發現代碼有什麼不周到的地方,歡迎指出討論。

package ListLink;

public class LinkListA {
	public Node first;
	public int tmp=0;     //節點位置
	public int len=0;     //鏈表長度
	
	public LinkListA()
	{
		this.first=null;
	}
	
	public void addFirstNode(Teacher teacher)
	{
		Node node=new Node(teacher);
		node.next=first;
		first=node;
		len++;
	}
	
	public Node deleteFirstNode()
	{
		Node node=null;
		if(first==null)
		{
			System.out.println("刪除失敗,該鏈表爲空");
		}
		else
		{
			node=first;
			first=node.next;
			len--;
		}
		return node;
	}
	
	public void addNode(int index,Teacher teacher)
	{
		Node node=new Node(teacher);
		Node previous=first;
		Node current=first;
		if(index>len||index<0)
		{
			System.out.println("插入失敗,超過鏈表長度");
			return;
		}
		while(tmp!=index)
		{
			previous=current;
			current=current.next;
			tmp++;
		}
		if(index==0)
		{
			node.next=first;
			first=node;
			len++;			
		}
		else
		{
			node.next=current;
			previous.next=node;
			len++;
		}
		tmp=0;
	}

	public Node deleteNodeByIndex(int index)
	{
		Node node=null;
		if(index>=len||index<0)
		{
			System.out.println("刪除失敗,超出鏈表長度");
			return node;
		}
		Node previous=first;
		Node current=first;
        while(tmp!=index)
        {
        	previous=current;
        	current=current.next;
        	tmp++;
        }
        if(index==0)
        {
        	node=first;
        	first=node.next;
        	len--;
        }
        else
        {
        	node=current;
        	previous.next=current.next;
        	len--;
        }
		return node;		
	}
	
	
	public void PrintAll()
	{
		Node node=first;
		while(node!=null)
		{
			node.display();
			node=node.next;			
		}
		
	}
	
	public static void main(String[] args)
	{
		LinkListA lla=new LinkListA();
		Teacher teacher=new Teacher("0001","張三","男",45,"12111111","[email protected]");
		lla.PrintAll();
		lla.addFirstNode(teacher);
		Teacher teacher1=new Teacher("0001","李四","男",42,"12111111","[email protected]");
		lla.addNode(-2, teacher1);
		lla.PrintAll();
	}
	
}

package ListLink;

public class Node {

	protected Node next;
	protected Teacher teacher;
	
	public Node(Teacher teacher)
	{
		this.teacher=teacher;
	}
	
	public void display()
	{
		System.out.println(" 教工編號: "+teacher.getSno()+" 教工姓名: "+teacher.getSname()+" 性別: "+teacher.getSex()+" 年齡: "+teacher.getAge()+" 電話: "+teacher.getTel()+" 郵箱:"+teacher.getEmail());
	}
}

package ListLink;

public class Teacher {
	private String sno;
	private String sname;
	private String sex;
	private int age;
	private String tel;
	private String email;
	
	public Teacher(String sno,String sname,String sex,int age,String tel,String email)
	{
		this.sno=sno;
		this.sname=sname;
		this.sex=sex;
		this.age=age;
		this.tel=tel;
		this.email=email;
	}
	
	public Teacher(Teacher teacher)
	{
		this.sno=teacher.sno;
		this.sname=teacher.sname;
		this.sex=teacher.sex;
		this.age=teacher.age;
		this.tel=teacher.tel;
		this.email=teacher.email;
	}
	
	public String getSno() {
		return sno;
	}
	public String getSname() {
		return sname;
	}
	public String getSex() {
		return sex;
	}
	public int getAge() {
		return age;
	}
	public String getTel() {
		return tel;
	}
	public String getEmail() {
		return email;
	}
	
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章