【数算-3】链表结构(实现栈和队列)

这篇博客探讨了链表数据结构的操作,包括单链表和双链表的反转算法,以及如何在链表中删除特定值的节点。此外,还介绍了Java内存泄漏的概念,并提供了基于双向链表实现的栈和队列。最后,讨论了使用数组动态实现栈和队列的方法。
摘要由CSDN通过智能技术生成

面试题目的最优解,确定最优解
有区分度的题目 》 考的万无一失的题目;高频题目
在痛苦就听下去,
【面试】锻炼一种能力,絮絮叨叨的把想法想出来,完成思路的碰撞。从人性出发,絮絮叨叨弄出来,面试官:“我帮你一下”,絮絮叨叨又不要招人烦。牛逼啊,太牛逼了。

链表结构:

给定一个 Node 结构:

public static class Node {
    public int value;
    public Node next;

    public Node(int data) {
        this.value = data;
    }
}

1) 单链表和双链表如何反转

1.1 单向链表

这是一个单链表结构, 假设初始时,a --> b --> c --> null :

那么如何实现指针的反转,最终 c --> b --> a --> null ?

代码实现:

public static Node reverseLinkedList(Node head) {
    Node pre = null;
    Node next = null;
    while (head != null) {
        // 保存好下一个结点要用的head 的指针,不然就不知道一会应该操作谁了。
        next = head.next;
        // 改变当前 head 的指针,使其指向 pre,指回去
        head.next = pre;
        // 把pre存好给下一个用,不然下一个就不知道指向哪里了。
        pre = head;
        // 把 next 下一次循环的 head
        head = next;
    }
    // 返回pre 元素,就是返回头节点。
    return pre;
}

pic1

1.2 双向链表

public class LinkedListTest2 {

    public static Node[] nodes = new Node[3];

    static {
        Node a = new Node(1);
        Node b = new Node(2);
        Node c = new Node(3);
        a.prev = null;
        a.next = b;
        b.prev = a;
        b.next = c;
        c.prev = b;
        c.next = null;
        nodes[0] = a;
        nodes[1] = b;
        nodes[2] = c;
    }

    public static void main(String[] args) {
        Node head = nodes[0];
        printNodes(head);
        Node newHead = reverseLinkedList(head);
        System.out.println("\n反转后:");
        printNodes(newHead);
    }

    public static void printNodes(Node head) {
        while (head != null) {
            System.out.println(head.prev + " <-- " + head.value + " --> " + head.next);
            head = head.next;
        }
    }

    public static class Node {
        public Node prev;
        public int value;
        public Node next;

        public Node(int value) {
            this.value = value;
        }
        public String toString() {
            return Integer.valueOf(value).toString();
        }
    }

    /**
     * prev --> head --> next
     */
    public static Node reverseLinkedList(Node head) {
        Node prev = null;
        Node next = null;
        while (head != null) {
            next = head.next;
            prev = head.prev;
            head.prev = next;
            head.next = prev;
            prev = head;
            head = next;
        }
        return prev;
    }
}

pic1

2) 把给定值都删除

    /**
     * 2 -> 2-> 3-> 2-> 3-> 4
     * 若:删除 num = 3,得到:
     * 2 -> 2-> 2-> 4,return head = 2
     * 若:删除 num = 2 得到:
     * 3-> 3-> 4 ,return head= 3
     * @param head
     * @param val
     * @return
     */
    public static ListNode removeValue(ListNode head, int val) {
        // head 来到第一个不需要删除的位置
        while(head != null) {
            if (head.val != val) {
                break;
            }
            head = head.next;
        }
        // 1、head == null 表示所有 node 都是 num,return null
        // 2、head != null
        ListNode prev = head;
        ListNode curr = head;
        while (curr != null) {
            if (curr.val == val)
                prev.next = curr.next;
            else
                prev = curr;
            curr = curr.next;
        }
        return head;
    }

1.3 java内存泄漏:

Java使用的是可达性算法,对于不可达对象会随时释放(自动释放内存

2. 栈和队列的实际实现:

栈:数据先进后出,犹如弹匣

队列:数据先进先出,好似排队

2.1 双向链表 (对栈和队列)的实现:

保留头进和头出

保留头进和尾出

双向链表

	public static class Node&lt;T&gt; {<!-- -->
		public T value;
		public Node&lt;T&gt; last;
		public Node&lt;T&gt; next;

		public Node(T data) {<!-- -->
			value = data;
		}
	}

	//双向链表
	public static class DoubleEndsQueue&lt;T&gt; {<!-- -->
		public Node&lt;T&gt; head;
		public Node&lt;T&gt; tail;

		public void addFromHead(T value) {<!-- -->
			Node&lt;T&gt; cur = new Node&lt;T&gt;(value);
			if (head == null) {<!-- -->
				head = cur;
				tail = cur;
			} else {<!-- -->
				cur.next = head;
				head.last = cur;
				head = cur;
			}
		}

		public void addFromBottom(T value) {<!-- -->
			Node&lt;T&gt; cur = new Node&lt;T&gt;(value);
			if (head == null) {<!-- -->
				head = cur;
				tail = cur;
			} else {<!-- -->
				cur.last = tail;
				tail.next = cur;
				tail = cur;
			}
		}

		public T popFromHead() {<!-- -->
			if (head == null) {<!-- -->
				return null;
			}
			Node&lt;T&gt; cur = head;
			if (head == tail) {<!-- -->
				head = null;
				tail = null;
			} else {<!-- -->
				head = head.next;
				cur.next = null;
				head.last = null;
			}
			return cur.value;
		}

		public T popFromBottom() {<!-- -->
			if (head == null) {<!-- -->
				return null;
			}
			Node&lt;T&gt; cur = tail;
			if (head == tail) {<!-- -->
				head = null;
				tail = null;
			} else {<!-- -->
				tail = tail.last;
				tail.next = null;
				cur.last = null;
			}
			return cur.value;
		}

		public boolean isEmpty() {<!-- -->
			return head == null;
		}

	}

public static class MyStack {  
	
	private DoubleEndsQueue&lt;
	public MyStack() {
		queue = new DoubleEndsQueue();
	}

	public void push(T value) {
		queue.addFromHead(value);
	}

	public T pop() {
		return queue.popFromHead();
	}

	public boolean isEmpty() {
		return queue.isEmpty();
	}

}

队列

public static class MyQueue {
		private DoubleEndsQueue queue;

    public MyQueue() {
			queue = new DoubleEndsQueue();
		}

		public void push(T value) {
			queue.addFromHead(value);
		}

		public T poll() {
			return queue.popFromBottom();
		}

		public boolean isEmpty() {
			return queue.isEmpty();
		}

	}

2.2 数组实现

面试中是动态实现的

语言的api的有限的,手动改写是不确定的。

两个栈拼队列来搞、两个队列拼栈来搞。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

willorn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值