数据结构——用链表实现栈和队列

目录

用链表实现栈和队列

一、链表实现栈

1.ListNodeStack类

2.测试结果:

一、链表实现队列

1.ListNodeQueue类

2.测试结果:


用链表实现栈和队列

首先我们需要定义一个ListNode节点的泛型类, 其中 T 是一个类型参数,意味着这个节点可以存储任何类型的数据。

java 复制代码
package 数据结构;


public class ListNode<T> {
    T data;
    ListNode<T> next;
}
一、链表实现栈
1.ListNodeStack<T>类
java 复制代码
package 数据结构;
/**
 * 用链表实现一个栈结构
 */
public class ListNodeStack<T> {
	//定义一个头节点
    private ListNode<T> pHead;

    public ListNodeStack(ListNode<T> pHead) {
        this.pHead = pHead;
    }
    
    //链表初始化
    public ListNodeStack() {
        pHead = new ListNode<T>();
        pHead.data = null;
        pHead.next = null;
    }

    //判断栈是否为空
    boolean isEmpty(){
        return pHead == null;
    }

    //入栈
    public void add(T e){
        ListNode<T> p = new ListNode<>();
        p.data = e;
        p.next = pHead.next;
        pHead.next = p;
        System.out.println(e+"push in stack");
    }

    //出栈
    public T pop(){
        ListNode<T> tmp = pHead.next;
        if(tmp != null){
            pHead.next = tmp.next;
            return tmp.data;
        }
        System.out.println("stack empty!");
        return null;
    }


    public static void main(String[] args) {
        ListNodeStack<Integer> stack = new ListNodeStack<Integer>();
        stack.add(1);
        stack.add(2);
        stack.add(3);

        System.out.println(stack.pop()+"out of stack");
        System.out.println(stack.pop()+"out of stack");
        System.out.println(stack.pop()+"out of stack");
        System.out.println(stack.pop()+" out of stack");
    }
}
2.测试结果:
一、链表实现队列
1.ListNodeQueue<T>类
java 复制代码
package 数据结构;

/**
 * 链表实现队列
 */
public class ListNodeQueue<T> {
    //队列首元素
    private ListNode<T> pHead;
    //队列尾元素
    private ListNode<T> pEnd;

    //分配头结点
    public ListNodeQueue(){
        pEnd = pHead = null;
    }

    //判断队列是否为空
    boolean isEmpty(){
        if(pHead == null){
            return true;
        }else {
            return false;
        }
    }

    //获取栈中元素个数
    int size(){
        int size = 0;
        ListNode<T> p = pHead;
        while(p != null){
            p = p.next;
            size++;
        }
        return size;
    }

    //入队列
    void inQueue(T e){
        ListNode<T> p = new ListNode<>();
        p.data = e;
        p.next = null;
        if(pHead == null){
            pHead = pEnd = p;
        }else {
            pEnd.next = p;
            pEnd = p;
        }
    }

    //出队列
    public void outQueue(){
        if(pHead == null){
        	System.out.println("queue empty!");
            return;
        }
        System.out.println(pHead.data+" out of queue");
        pHead = pHead.next;
        if(pHead == null){
            pEnd = null;
        }
        
    }


    public static void main(String[] args) {
    	ListNodeQueue<Integer> queue = new ListNodeQueue<>();
        queue.inQueue(1);
        queue.inQueue(2);
        queue.inQueue(3);
        queue.inQueue(4);
       
        
        queue.outQueue();
        queue.outQueue();
        queue.outQueue();
        queue.outQueue();
        queue.outQueue();

    }
}
2.测试结果:
相关推荐
奋发向前wcx4 小时前
y1,y2总复习笔记5 2026.7.19
数据结构·笔记·算法
ChaoZiLL9 小时前
我的数据结构4-栈和队列
数据结构
miller-tsunami9 小时前
顺序表相关知识点
数据结构·顺序表
华玥作者12 小时前
uniapp 万条数据不卡顿:我写了个虚拟列表组件 hy-list,原生支持瀑布流
数据结构·uni-app·list·vue3
2401_8414956412 小时前
【数据结构】B*树
数据结构·c++·b树·算法·删除·插入·三分分裂
晚笙coding12 小时前
LeetCode 108:将有序数组转换为二叉搜索树 —— 从数组到平衡二叉树的递归构造
数据结构·算法·leetcode
不如语冰13 小时前
AI大模型入门-模块导入import
数据结构·人工智能·pytorch·python
岑梓铭13 小时前
《考研408数据结构》第七章(7.1 查找:顺序查找、折半查找、分块查找)复习笔记
数据结构·笔记·考研·408·ds·查找
壹号用户13 小时前
c++入门之list了解及使用
数据结构·list
来一碗刘肉面13 小时前
什么是双端队列
数据结构·链表