概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的引用链
接次序实现的 。
// 1、无头单向非循环链表实现
public interface ILinked {
//头插法
void addFirst(int data);
//尾插法
void addLast(int data);
//任意位置插入,第一个数据节点为0号下标
boolean addindex(int index,int data);
//查找是否包含关键字key是否在单链表当中
boolean contains(int key);
//删除第一次出现关键字为key的节点
3.3 链表面试题
int remove(int key);
//删除所有值为key的节点
void removeAllKey(int key);
//得到单链表的长度
int getLength();
void display();
void clear();
}
public class MySingleLinkedlmpl implements ILinked {
class Node{
public int getData() {
return data;
}
private int data;
public Node next;
public Node(int data){
this.data=data;
this.next=null;
}
}//节点类
private Node head;
public MySingleLinkedlmpl(){
this.head=null;
}
@Override
public void addFirst(int data) {
Node node=new Node(data);
if(this.head==null){
this.head=node;
}else{
node.next=this.head;
this.head=node;
}
}
@Override
public void addLast(int data) {
Node node=new Node(data);
Node cur=this.head;
if(cur==null){
this.head=node;
}else {
//找尾巴
while(cur.next!=null){
cur=cur.next;
}
//退出上面的循环,cur所指向的位置就是尾节点
cur.next=node;
}
}
private void checkIndex(int index) {
if(index < 0 || index > getLength()){
throw new UnsupportedOperationException("index位置不合法");
}
}
private Node searchIndex(int index){
checkIndex(index);
int count=0;
Node cur=this.head;
while (count<index-1){
cur=cur.next;
count++;
}
return cur;
}
@Override
public boolean addindex(int index, int data) {
Node node=new Node(data);
if(index==0){
addFirst(data);
return true;
}
Node cur=searchIndex(index);
node.next=cur.next;
cur.next=node;
return true;
}
@Override
public boolean contains(int key) {
Node cur=this.head;
while(cur.next!=null){
if(cur.data==key){
return true;
}
cur=cur.next;
}
return false;
}
//寻找前驱节点
private Node searchPrev(int key) {
Node cur = this.head;
while (cur.next != null) {
if(cur.next.data== key){
return cur;
}
cur = cur.next;
}
return null;
}
@Override
public int remove(int key) {
Node cur=this.head;
if(cur==null){
throw new UnsupportedOperationException("单链表为空");
}
int oldDate=0;
//删除节点为头节点
if (this.head.data==key){
oldDate=this.head.data;
this.head=this.head.next;
return oldDate;
}
Node prev = searchPrev(key);
if(prev == null){
throw new UnsupportedOperationException("没有前驱");
}
Node del = prev.next;
oldDate = del.data;
prev.next = del.next;
//del = null;
return oldDate;
}
@Override
public void removeAllKey(int key) {
if(this.head == null) {
return;
}
Node prev = this.head;
Node cur = this.head.next;
while (cur != null) {
if(cur.data == key){
prev.next = cur.next;
cur = prev.next;
}else {
prev = cur;
cur = cur.next;
}
}
if(this.head.data == key){
this.head = this.head.next;
}
}
@Override
public int getLength() {
Node cur=this.head;
if(cur==null){
return 0;
}
int count=0;
while(cur.next!=null){
count++;
cur=cur.next;
}
return count;
}
@Override
public void display() {
Node cur=this.head;
while (cur.next!=null){
System.out.print(cur.data+" ");
cur=cur.next;
}
System.out.println();
}
@Override
public void clear() {
Node cur=this.head;
while (cur.next!=null){
Node del = cur.next;
cur.next = del.next;
}
this.head=null;
}
}