📄 list.java
字号:
import java.awt.*;
import java.awt.event.*;
class ListNode {
String data;
ListNode next;
ListNode( String idata , ListNode nextNode )
{
data = idata;
next = nextNode;
}
}
public class List {
private ListNode head;
private ListNode tail;
private String name;
private int size;
public List( String s )
{
name = s;
head = tail = null;
size = 0;
}
public List()
{
this( "List" );
}
public void insertAtFront( String insertItem ) //在链表头插入结点
{
if ( isEmpty() )
head = tail = new ListNode( insertItem , null );
else
head = new ListNode( insertItem ,head );
size++;
}
public void insertAtBack ( String insertItem ) //在链表尾插入结点
{
if ( isEmpty() )
head = tail = new ListNode( insertItem , null );
else
tail = tail.next = new ListNode( insertItem ,null );
size++;
}
public String removeFromFront() //从链表头删除并返回结点值
{
String removeItem = "";
removeItem = head.data;
if ( head.equals(tail) )
head = tail = null;
else
head = head.next;
size--;
return removeItem;
}
public String remove ( String item )//删除结点值与参数相等的结点
{
String removeItem = "";
if( head.equals(tail ) )
head = tail = null;
else{
ListNode current = head;
while (current.data != item )
current = current.next;
removeItem = current.data;
}
size--;
return removeItem;
}
public boolean isEmpty()//判断链表是否为空
{ return head == null;}
public int getSize(){ return this.size;}//取得链表长度
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -