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

目录

用链表实现栈和队列

一、链表实现栈

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.测试结果:
相关推荐
rannn_1112 小时前
【力扣hot100】链表专题|160、206、234、141、142
java·算法·leetcode·链表·面试·开发
LuminousCPP6 小时前
数据结构-双向循环链表
c语言·数据结构·笔记·链表
白狐_7987 小时前
408数据结构第5章:树与二叉树②——遍历、线索树、森林与哈夫曼
数据结构·算法·深度优先
Jasmine_llq9 小时前
《P15800 [GESP202603 六级] 选数》
数据结构·long long·逆序线性 dp·从后往前推导·转移严格遵循题意·防止总和溢出·复杂度on
疯狂打码的少年9 小时前
【数据结构】串(字符串):基本概念与存储
数据结构·笔记
纵有疾風起11 小时前
线性表的定义与基本操作 — 从逻辑结构到 ADT 接口
数据结构·算法·408·线性表·adt
wuyk55511 小时前
1.栈:后进先出的线性数据结构
c语言·数据结构·stm32·单片机
LuminousCPP12 小时前
数据结构-时间与复杂度|时间/空间复杂度 + 两道力扣练习 + 二分查找复盘
c语言·数据结构·经验分享·算法·leetcode
Nil20812 小时前
leetcode 三数之和
数据结构·算法·leetcode
noipp12 小时前
推荐题目:洛谷 P16689 出征
java·开发语言·数据结构·c++·算法·洛谷·luogu