文章目录
线性数据结构:
定义一个链表:
e
:存储的数据
next
:指向下一个节点
数据和数据之间连接,由next完成
next指向null时,说明是最后一个节点
链表优缺点:
随机访问能力
:不能像数组那样,给出一个索引就能马上获取数据
数组和链表的对比:
在链表头添加元素
node.next = head: 这段代码主要是将当前头节点的指针域指向这个新结点,所以就将当前结点加入到了链表的最前面
head = node: 个人认为这段代码最核心的就在这里,在执行完上述两句代码时,其实已经将新的结点加入到链表的头部,那么新的结点如何再加到链表的头部?
就应该还向上面那样,令head
指向最前一个结点,所以,head = node
(这句话实际是将head = node的地址
赋给head
)。所以,当前head
仍旧指向了链表头结点
,
再来新的结点时,仍然会像上面那样执行
在链表中间添加元素
为链表设立虚拟头结点
插入,修改
:想要找index
之前的那个节点,就要从dummyHead
开始遍历
查询,删除
:想要找index
节点,就要从dummyHead.next
开始遍历
链表元素的删除
改进链表,添加尾指针
从head
端删除元素,从tail
端插入元素
链表的时间复杂度分析
栈来自于线性表
,因此栈可以使用顺序表的方式
来实现也可以使用链表的方式
来实现。
本文是根据单向链表
来实现栈的使用的语言是Java,上一篇文章中使用顺序存储(数组)来实现栈
Stack.class
package code.ArrayCode;
public interface Stack<E> {
int getSize();
boolean isEmpty();
void push(E e);
E pop();
E peek();
}
LinkedList.class
package code.LinkedListCode;
public class LinkedList<E> {
private class Node{
public E e;
public Node next;
public Node(E e,Node next){
this.e = e;
this.next = next;
}
public Node(E e){
this.e = e;
this.next = null;
}
public Node(){
this(null,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 ;
}
//在链表index(0-based)位置添加新的元素e
//在链表中不是一个常用的操作,练习用:
public void add(int index , E e){
if(index < 0 || index > size){
throw new IllegalArgumentException("Add failed. Illegal index.");
}
//在头部插入数据
Node prev = dummyHead;
for(int i = 0 ; i < index; i ++){
prev = prev.next;
}
Node node = new Node(e);
node.next = prev.next;
prev.next = node;
size ++;
}
//在链表头添加新的元素e
public void addFirst(E e){
add(0,e);
}
//在链表末尾添加新的元素e
public void addLast(E e){
add(size,e);
}
//获得链表的第index(0-based)个位置的元素
//在链表中不是一个常用的操作,练习用:
public E get(int index){
if(index < 0 || index >= size){
throw new IllegalArgumentException("Get failed. Illegal index.");
}
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 -1);
}
//修改链表的第index(0-based)个位置的元素为e
//在链表中不是一个常用的操作,练习用:
public void set(int index , E e){
if(index < 0 || index >= size){
throw new IllegalArgumentException("Set failed. Illegal index.");
}
Node cur = dummyHead.next;
for(int i = 0 ; i < index ; i ++){
cur = cur.next;
}
cur.e = e ;
}
//查找链表中是否有元素e
public boolean contains(E e){
Node cur = dummyHead.next;
while (cur != null){
if(cur.e.equals(e)){
return true;
}
cur = cur.next;
}
return false;
}
//删除链表的第index(0-based)个位置的元素
//在链表中不是一个常用的操作,练习用:
public E remove(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Remove failed. Illegal index.");
}
Node prev = dummyHead;
for(int i = 0 ; i < index ; i ++){
prev = prev.next;
}
Node retNode = prev.next;
prev.next = retNode.next;
retNode.next = null;
size--;
return retNode.e;
}
//从链表中删除第一个元素,返回删除的元素
public E removeFirst(){
return remove(0);
}
//从链表中删除最后一个元素,返回删除的元素
public E removeLast(){
return 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();
}
}
LinkedListStack.class
package code.LinkedListCode;
import code.ArrayCode.Stack;
public class LinkedListStack<E> implements Stack<E> {
private LinkedList<E> list;
public LinkedListStack() {
list = new LinkedList<E>();
}
public int getSize() {
return list.getSize();
}
public boolean isEmpty() {
return list.isEmpty();
}
public void push(E e) {
list.addFirst(e);
}
public E pop() {
return list.removeFirst();
}
public E peek() {
return list.getFirst();
}
@Override
public String toString(){
StringBuilder res = new StringBuilder();
res.append("Stack: top");
res.append(list);
return res.toString();
}
public static void main(String[] args) {
LinkedListStack<Integer> stack = new LinkedListStack<Integer>();
for(int i =0 ; i < 5 ; i++){
stack.push(i);
System.out.println(stack);
}
stack.pop();
System.out.println(stack);
stack.push(stack.pop());
System.out.println(stack);
}
}
测试结果:
Stack: top0->NULL
Stack: top1->0->NULL
Stack: top2->1->0->NULL
Stack: top3->2->1->0->NULL
Stack: top4->3->2->1->0->NULL
Stack: top3->2->1->0->NULL
Stack: top3->2->1->0->NULL