package DataStructure;
public class LinkedList<E> {
/**
* 增删改查复杂度:O(n)
* 动态分配空间,不会浪费空间
*/
private class Node{//节点类,一个Node表示一个数据的存储和指向下一个节点
public E e;
public Node next;
public Node(E e,Node next){
this.e = e;
this.next=next;
}
public Node(){
this(null,null);
}
public Node(E e){
this(e,null);
}
@Override
public String toString(){
return e.toString();
}
}
private Node dummyhead;//虚拟头结点,使用内部类
private int size;//节点数量
//构造函数
public LinkedList(){
dummyhead = new Node(null,null);//虚拟头结点
size = 0;
}
public int getSize(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
//在链表的某个位置添加元素,引入索引完全是为了练习使用
public void add(int index,E e){//均摊复杂度O(n)
if(index < 0 || index > size){
throw new IllegalArgumentException("add failed,index is illegal");
}
else{
Node pre = dummyhead;
for(int i = 0;i < index;i++){
pre = pre.next;
}
// Node node = new Node(e);
// node.next = pre.next;
// pre.next = node;
pre.next = new Node(e,pre.next);
size++;
}
}
public void addFirst(E e){
//对链表而言,addFirst是O(1)级别的,对数组而言,addLast是O(1)级别
add(0,e);
}
public void addLast(E e){//O(n)
add(size,e);
}
//获取index位置的元素
public E get(int index){
if(index < 0 || index > size){
throw new IllegalArgumentException("add failed,index is illegal");
}
Node cur = dummyhead.next;
for(int i =0;i < index;i++){
cur = cur.next;
}
return cur.e;
}
public E getFirst(){
return get(0);
}
public E getLast(){
return get(size);
}
//更新
//获取index位置的元素
public void set(int index,E e){
if(index < 0 || index > size){
throw new IllegalArgumentException("add failed,index is illegal");
}
Node cur = dummyhead.next;
for(int i =0;i < index;i++){
cur = cur.next;
}
cur.e = e;
}
public boolean contains(E e){//O(n)
Node cur = dummyhead.next;
while(cur != null){
if(cur.e.equals(e)){
return true;
}
cur = cur.next;
}
return false;
}
//删除元素
public E remove(int index){//O(n)
Node pre = dummyhead;
for(int i = 0; i< index;i++){
pre = pre.next;
}
Node ret = pre.next;
pre.next = ret.next;
ret.next = null;
size--;
return ret.e;
}
public void removeFisr(){//O(1)
remove(0);
}
public void removeLast(){//O(n)
remove(size-1);
}
@Override
public String toString(){
StringBuilder res = new StringBuilder();
Node cur = dummyhead.next;
while (cur != null){
res.append(cur+"->");
cur = cur.next;
}
res.append("null");
return res.toString();
}
}
===================================
package DataStructure;
public class Main {
public static void main(String[] args) {
LinkedList<Integer> linkedList = new LinkedList<>();
for(int i=0;i<10;i++){
linkedList.addLast(i);
System.out.println(linkedList);
}
linkedList.add(3,199);
System.out.println(linkedList);
linkedList.remove(3);
System.out.println(linkedList);
linkedList.removeFisr();
System.out.println(linkedList);
linkedList.removeLast();
System.out.println(linkedList);
}
}
===================================
0->null
0->1->null
0->1->2->null
0->1->2->3->null
0->1->2->3->4->null
0->1->2->3->4->5->null
0->1->2->3->4->5->6->null
0->1->2->3->4->5->6->7->null
0->1->2->3->4->5->6->7->8->null
0->1->2->3->4->5->6->7->8->9->null
0->1->2->199->3->4->5->6->7->8->9->null
0->1->2->3->4->5->6->7->8->9->null
1->2->3->4->5->6->7->8->9->null
1->2->3->4->5->6->7->8->null
Process finished with exit code 0


被折叠的 条评论
为什么被折叠?



