📄 linkedlistclass.java
字号:
package 音像店;
public abstract class LinkedListClass {
protected class LinkedListNode//相当于链表结构体
{
DataElement info;
LinkedListNode link;
}
protected LinkedListNode first;
protected LinkedListNode last;
protected int count;
public LinkedListClass()//构造函数
{
first=null;
last=null;
count=0;
}
public boolean isEmptyList()//判断链表是否为空
{
return(first==null);
}
public void initializeList()//初始化链表
{
first=null;
last=null;
count=0;
}
public void print()//打印链表的全体数据
{
LinkedListNode current;
current=first;
while(current!=null)
{
System.out.print(current.info + "");
current=current.link;
}
}
public int length()//求链表的长度
{
return count;
}
public DataElement front()//检索链表的最后一个节点
{
DataElement temp=first.info.getCopy();
return temp;
}
public DataElement back()//检索链表的第一个节点
{
DataElement temp=last.info.getCopy();
return temp;
}
public void insertFirst(DataElement newItem)//在链表头插入节点
{
LinkedListNode newNode;
newNode=new LinkedListNode();
newNode.info=newItem.getCopy();
newNode.link=first;
first=newNode;
if(last==null) last=newNode;
count++;
}
public void insertLast(DataElement newItem)//在链表尾插入节点
{
LinkedListNode newNode;
newNode=new LinkedListNode();
newNode.info=newItem.getCopy();
newNode.link=null;
if(first==null)
{
first=newNode;
last=newNode;
}
else
{
last.link=newNode;
last=newNode;
}
count++;
}
private void copy(LinkedListClass otherList)//复制链表
{
LinkedListNode newNode;
LinkedListNode current;
first=null;
if(otherList.first==null)
{
first=null;
last=null;
count=0;
}
else
{
count=otherList.count;
current=otherList.first;
first=new LinkedListNode();
first.info=current.info.getCopy();
first.link=null;
current=current.link;
while(current!=null)
{
newNode=new LinkedListNode();
newNode.info=current.info.getCopy();
newNode.link=null;
last.link=newNode;
last=newNode;
current=current.link;
}
}
}
public LinkedListClass(LinkedListClass otherList)//拷贝构造函数
{
copy(otherList);
}
public void copyList(LinkedListClass otherList)//复制链表
{
if(this !=otherList) copy(otherList);
}
public abstract boolean search(DataElement sreachItem);
public abstract void deleteNode(DataElement deleteItem);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -