⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mylinklisttest.java

📁 对已建立的单链表实现插人、删除、查找等基本操作
💻 JAVA
字号:
public class MyLinkListTest {
private MyLinkListTest first;
private MyLinkListTest next;
private Object data;
private int count;

public MyLinkListTest()
{

}


public MyLinkListTest(Object data)
{
this.data = data;
}


	public int getLength()  //求表长
	{
		System.out.println(count);
		return count;	
	}
	

public void insert(Object data)
{
MyLinkListTest newLink = new MyLinkListTest(data);
newLink.next = first;
first = newLink;
count++;
}


public void reverse()
{
MyLinkListTest current = first;
while(current != null)
{
System.out.println(current.data);
current = current.next;
}
}


public boolean find(Object key)
{
MyLinkListTest current = first;
while(current != null)
{
if(current.data.equals(key))
return true;
current = current.next;
}

return false;
}

/*public Object getEntry(int i)
{
MyLinkListTest current = first;
while(current != null)
{
if(current.data.equals(key))
return true;
current = current.next;
}

return false;
}
*/

/*public boolean add(Object data)
{
	MyLinkListTest newLink = new MyLinkListTest(data);
	if(isEmpty())
		firstNode = newNode;
	else
		lastNode.next = newNode;
	lastNode.next = newNode;
	length++;
	return true;
}
*/

public void remove(Object key)
{
MyLinkListTest prev = first;
MyLinkListTest current = first;
while(!current.data.equals(key))
{
if(current.next == null)
return;
else
{
prev = current;
current = current.next;
}
}
if(first == current)
first = first.next;
else
prev.next = current.next;
}
public boolean isEmpty()
{
return (first == null);
}


public static void main(String[] args)
{
MyLinkListTest link = new MyLinkListTest();
//insert data
link.insert(1); 
link.insert(2);
link.insert(3);
link.insert("4");
link.insert("5");
link.insert("6");
System.out.println("The link's length : ");
link.getLength();

System.out.println("the reverse link : ");
link.reverse();
//delete data
System.out.println("After remove 4 and 2,the link is :");
link.remove("4");
link.remove(2);
link.reverse();


System.out.println("-----------------------");
//search data
Object key = "3"; 

if(link.find(key))
System.out.println("find: "+key);
else
System.out.println("not find: "+key);
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -