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

目录

用链表实现栈和队列

一、链表实现栈

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.测试结果:
相关推荐
riri191920 分钟前
算法分析:蛮力法
数据结构·算法
摄殓永恒24 分钟前
猫咪几岁
数据结构·c++·算法
似水এ᭄往昔1 小时前
【数据结构】——队列
c语言·数据结构·c++·链表
水水沝淼㵘1 小时前
嵌入式开发学习日志(数据结构--双链表)Day21
c语言·数据结构·学习·算法·排序算法
ElseWhereR2 小时前
反转链表 - 简单
数据结构·链表
共享家95273 小时前
红黑树解析
数据结构·c++·算法
夜晚中的人海4 小时前
【C语言】初阶数据结构相关习题(二)
c语言·开发语言·数据结构
dddaidai1234 小时前
Redis数据结构
数据结构·redis·hash table
cdut_suye4 小时前
【Linux系统】从 C 语言文件操作到系统调用的核心原理
java·linux·数据结构·c++·人工智能·机器学习·云计算
灵典3365 小时前
数据结构入门-二叉树的层序遍历
数据结构·算法