单链表的基本操作
发布日期:2021-05-11 02:21:52 浏览次数:14 分类:精选文章

本文共 6655 字,大约阅读时间需要 22 分钟。

ListNode������������������������������

������������������������

���������������������������������������������

���������������������������������

���������������������������������

������������������������������������������

���������������������������

codepen link


������������

//1���������������������������������

ListNode������������

class ListNode {

public int data;
public ListNode next;
//������������
public ListNode(int data) {
this.data = data;
this.next = null;
}
}

MySingleList������������

public class MySingleList {

public ListNode head; //���������
public MySingleList(){this.head=null;} //���������������������������

//1,������������������������������������������������  public void addFirst(int data){   ������454b Ln newly created node:  	class Example:  	public void addFirst(int data) {  	ListNode node = new ListNode(data);  		if (this.head == null) {  			this.head = node;  		} else {  			node.next = head;  			head = node;  		}  	}//2,������������������������������������������������  public void addLast(int data){  	//���������������  	Py fatt��lo nov�� nodu:  	public void addLast(int data) {  		ListNode node = new ListNode(data);  		if (this.head == null) {  			this.head = node;  		} else {  			ListNode cur = this.head;  			while (cur.next != null) {  				cur = cur.next;  			}\			//���������������������  			cur.next = node;  		}  	}//3,������������������index���������������������������������0���������  //index���������������0 <= index < getLength()  //������������������������������������������index���������  private ListNode searchIndex(int index){  	ListNode prev = this.head;  	int count = 0;  	while (count < index) {  		prev = prev.next;  		count++;  	}  	return prev;  }  //���������������������������������Remove node from current next and insert new node  private void addIndex(int index, int data){  	if (index < 0 || index >= this.getLength()) {  			return; //���������������������������  	} else if (index == 0) {  			addFirst(data);  			return true;  	} else {  			ListNode prev = searchIndex(index);  			ListNode node = new ListNode(data);  			//������������������  		 nods relative pu prevudand next������:  			public boolean addindex(int index, int data) {  				if (index < 0 || index >= this.getLength()) {  					return false; //���������������������  				} else if (index == 0) {  					addFirst(data);  					return true;  				} else {  					ListNode prev = searchIndex(index);  					ListNode node = new ListNode(data);  					node.next = prev.next;  					prev.next = node;  					return true; //������������  				}  			}  //���������������������������������������  public boolean contains1(int key){  	ListNode cur = this.head;  	while (cur != null) {  		if (cur.data == key) {  			return true;  		}  		cur = cur.next;  	}  	return false;  }  //���������������������������������������������  public ListNode contains2(int key){  	ListNode cur = this.head;  	while (cur != null) {  		if (cur.data == key) {  			return cur;  		}  		cur = cur.next;  	}  	return null;  }  //������������������������  private ListNode searchPrev(int key){  	ListNode prev = this.head;  	while (prev.next != null) {  		if (prev.next.data == key) {  			return prev;  		}  		prev = prev.next;  	}  	return null;  }  //������������������������������������������������������  public void remove(int key){  	//������������������������  	if (this.head.data == key) {  		this.head = this.head.next;  		return;  	}  	//������������������������������������������  	ListNode prev = searchPrev(key);  	if (prev == null) {  		System.out.println("���������������������������");  		return;  	}  	//���������������  	ListNode del = prev.next;  	prev.next = del.next;  }  //���������������������������������key���������  public void removeAll(int key){  	ListNode prev = head;  	ListNode cur = head == null ? null : head.next;  	while (cur != null) {  		if (prev.next.data == key) {  			//������������������  			prev.next = cur.next;  			cur = cur.next;  			//���������������������������  			if (prev.data == key) {  				head = cur;  			}  		} else {  			prev = cur;  			cur = cur.next;  		}  	}  }  //������������  public void clear(){  	//������������������������  	if (this.head == null) {  		return;  	}  	//������������������������������  	ListNode cur = head;  	while (cur.next != null) {  		cur = cur.next;  	}  	//���������������  	head = null;  }  //������������������  public int getLength(){  	int count = 0;  	ListNode cur = this.head;  	while (cur != null) {  		count++;  		cur = cur.next;  	}  	return count;  }  //���������������������  public void display(){  	if (this.head == null) {  		return; //���������������������������  	}  	ListNode cur = head;  	while (cur != null) {  		System.out.println(cur.data + " ");  		cur = cur.next;  	}  	System.out.println(); //������  }  //���������������MySingleList���������������������  public static void main(String[] args){  	//���������������  	MySingleList ls = new MySingleList();  	//1,���������������  	ls.addFirst(2); //���������������������  	ls.addFirst(5); //������������������������  	ls.display(); //������������������  	//2,���������������  	ls.addLast(1);  	ls.addLast(2);  	ls.addLast(3);  	ls.addLast(4);  	ls.addLast(5);  	ls.display();  	//3,������������������������������  	ls.addIndex(0, 99); //������������0������������������������  	ls.display();  	ls.addIndex(7, 999); //���������������������9���������2,5-->1,2,3,4,5���������������������7������������������  	ls.display();  	//4,������������������  	System.out.println("������������������99������������" + ls.contains1(99));  	ListNode node = ls.contains2(999);  	if (node != null) {  		System.out.printf("������������������%d\n", node.data);  	} else {  		System.out.println("���������999������");  	}  	System.out.println(" ");  	//5,������������  	ls.remove(99); //������������������������99������  	ls.display();  	ls.remove(999); //������������������������999������  	ls.display();  	//6,������������������������������  	ls.removeAll(2); //������������������2���������  	ls.display();  	//7,������������  	ls.clear();  	System.out.println("���������������");  	//������������  	System.out.println("������������");  }

}

codepen-link


���������������������������������Java������������������������������������ ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������


���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
下一篇:一次对跨境赌博类APP的渗透实战(getshell并获得全部数据)

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2025年04月04日 14时15分11秒