📄 list.java
字号:
import java.awt.*;
import java.awt.event.*;
class ListNode {
int data;
ListNode next;
ListNode( int 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( int insertItem ) //在链表头部插入节点
{
if ( isEmpty() )
head = tail = new ListNode( insertItem , null );
else
head = new ListNode( insertItem ,head );
size++;
}
public void insertAtBack ( int insertItem ) //在链表尾部插入
{
if ( isEmpty() )
head = tail = new ListNode( insertItem , null );
else
tail = tail.next = new ListNode( insertItem ,null );
size++;
}
public int removeFromFront() //删除链表头部节点,并返回结点数据
{
int removeItem = 0;
removeItem = head.data;
if ( head.equals(tail) )
head = tail = null;
else
head = head.next;
size--;
return removeItem;
}
public int remove ( int item ) //删除链表中第一个结点数据等于参数的那个结点
{
int removeItem = 0;
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 lrucompare( int k ) // 比较在链表中结点中数据是否有与参数相等的,又返回true
{
ListNode temp = new ListNode( 0 , null );
temp = head;
while( temp != tail )
{
if( temp.data == k )
return true;
temp = temp.next;
}
return false;
}
public ListNode lrureplace( int k ) //返回比较在链表中结点中数据与参数相等的结点位置
{
ListNode temp = new ListNode( 0 , null );
temp = head;
while( temp != tail )
{
if( temp.data == k )
return temp;
temp = temp.next;
}
return null;
}
public boolean isEmpty() //判断是链表是否为空
{ return head == null;}
public int getSize(){ return this.size;}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -