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

目录

用链表实现栈和队列

一、链表实现栈

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.测试结果:
相关推荐
Sam_Deep_Thinking2 小时前
学数据结构到底有什么用
数据结构
py有趣6 小时前
力扣热门100题之和为K的子数组
数据结构·算法·leetcode
hipolymers6 小时前
C语言怎么样?难学吗?
c语言·数据结构·学习·算法·编程
CS创新实验室6 小时前
从“跑得动”到“跑得稳”:深度剖析数据结构究竟是理论点缀还是核心战力?
数据结构
jllllyuz7 小时前
MATLAB 蒙特卡洛排队等待模拟程序
数据结构·matlab
自我意识的多元宇宙7 小时前
树、森林——树、森林与二叉树的转换(森林转换为二叉树)
数据结构
海清河晏1117 小时前
数据结构 | 双循环链表
数据结构·链表
py有趣8 小时前
力扣热门100题之编辑距离
数据结构·算法·leetcode
努力努力再努力wz9 小时前
【Linux网络系列】万字硬核解析网络层核心:IP协议到IP 分片重组、NAT技术及 RIP/OSPF 动态路由全景
java·linux·运维·服务器·数据结构·c++·python
谭欣辰10 小时前
AC自动机:多模式匹配的高效利器
数据结构·c++·算法