数据结构【4】【高级排序】线性表—链表的操作和栈的实现

目录

1.链表的操作

1.1链表反转

1.2快慢指针

1.2.1 中间值问题

1.2.2单向链路是否有环

1.2.3有环链表入口问题

2.栈

2.1栈的API实现

 2.2API代码实现

2.3队列的API设计 

2.4队列的API实现


1.链表的操作

1.1链表反转

需求:

原链表中数据为:1->2->3>4

反转后链表中数据为:4->3->2->1

 

 

    public void reverse(){
        if (N==0){
            //当前是空链表,不需要反转
            return;
        }
        reverse(head.next);
    }
    /**
     *
     * @param curr 当前遍历的结点
     * @return 反转后当前结点上一个结点
     */
    public Node reverse(Node curr){
        //到达最后一个节点
        if (curr.next == null){
            //反转后,头结点应该指向原链表中的最后一个元素
            head.next = curr;
            return curr;
        }
        //当前结点的上一个结点
        Node pre = reverse(curr.next);
        pre.next = curr;
        //当前结点的下一个结点设为null
        curr.next = null;
        //返回当前结点
        return curr;
    }

1.2快慢指针

1.2.1 中间值问题

  /**
     * @param first 链表的首结点
     * @return 链表的中间结点的值
     */
    public static String getMid(Node<String> first) {
        Node<String> slow = first;
        Node<String> fast = first;

        while (fast!=null && fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
        }

        return slow.item;
    }

1.2.2单向链路是否有环

public static boolean isCircle(Node<String> first) {
    Node<String> slow = first;
    Node<String> fast = first;
    while(fast!=null && fast.next!=null){
        fast = fast.next.next;
        slow = slow.next;
        if (fast.equals(slow)){
            return true;
        }
    }
    return false;
}

1.2.3有环链表入口问题

    public static Node getEntrance(Node<String> first) {
        Node<String> slow = first;
        Node<String> fast = first;
        Node<String> temp = null;
        while(fast!=null && fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
            if (fast.equals(slow)){
                temp = first;
                //return true;
                continue;
            }
            if (temp!=null){
                temp=temp.next;
                if (temp.equals(slow)){
                    return temp;
                }
            }

        }
        return null;
    }

2.栈

2.1栈的API实现

 2.2API代码实现

import java.util.Iterator;


public class Stack<T> implements Iterable<T>{
    //记录首结点
    private Node head;
    //栈中元素的个数
    private int N;
    public Stack() {
        head = new Node(null,null);
        N=0;
    }
    //判断当前栈中元素个数是否为0
    public boolean isEmpty(){
        return N==0;
    }

        //把t元素压入栈
    public void push(T t){
        Node oldNext = head.next;
        Node node = new Node(t, oldNext);
        head.next = node;
        //个数+1
        N++;
    }

    //弹出栈顶元素
    public T pop(){
        Node oldNext = head.next;
        if (oldNext==null){
            return null;
        }
        //删除首个元素
        head.next = head.next.next;
        //个数-1
        N--;
        return oldNext.item;
    }

    //获取栈中元素的个数
    public int size(){
        return N;
    }

    @Override
    public Iterator<T> iterator() {
        return new SIterator();
    }

    private class SIterator implements Iterator<T>{
        private Node n = head;
        @Override
        public boolean hasNext() {
            return n.next!=null;
        }
        @Override
        public T next() {
            Node node = n.next;
            n = n.next;
            return node.item;
        }
    }

    private class Node{
        public T item;
        public Node next;
        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }
}

2.3队列的API设计 

2.4队列的API实现

package linear;

import java.util.Iterator;

public class Queue<T> implements Iterable<T>{
    //记录首结点
    private Node head;
    //记录最后一个结点
    private Node last;
    //记录队列中元素的个数
    private int N;
    public Queue() {
        head = new Node(null,null);
        last=null;
        N=0;
    }
    //判断队列是否为空
    public boolean isEmpty(){
        return N==0;
    }
    //返回队列中元素的个数
    public int size(){
        return N;
    }
    //向队列中插入元素t
    public void enqueue(T t){
        if (last==null){
            last = new Node(t,null);
            head.next=last;
        }else{
            Node oldLast = last;
            last = new Node(t,null);
            oldLast.next=last;
        }
//个数+1
        N++;
    }
    //从队列中拿出一个元素
    public T dequeue(){
        if (isEmpty()){
            return null;
        }
        Node oldFirst = head.next;
        head.next = oldFirst.next;
        N--;
        if (isEmpty()){
            last=null;
        }
        return oldFirst.item;
    }
    @Override
    public Iterator<T> iterator() {
        return new QIterator();
    }
    private class QIterator implements Iterator<T>{
        private Node n = head;
        @Override
        public boolean hasNext() {
            return n.next!=null;
        }
        @Override
        public T next() {
            Node node = n.next;
            n = n.next;
            return node.item;
        }
    }
    private class Node{
        public T item;
        public Node next;
        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值