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

目录

用链表实现栈和队列

一、链表实现栈

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.测试结果:
相关推荐
TL滕6 分钟前
从0开始学算法——第四天(题目参考答案)
数据结构·笔记·python·学习·算法
Liangwei Lin18 分钟前
洛谷 P1443 马的遍历
数据结构·算法
老鱼说AI20 分钟前
算法基础教学第二步:数组(超级详细原理级别讲解)
数据结构·神经网络·算法·链表
TL滕1 小时前
从0开始学算法——第四天(练点题吧)
数据结构·笔记·学习·算法
[J] 一坚1 小时前
华为OD、微软、Google、神州数码、腾讯、中兴、网易有道C/C++字符串、数组、链表、树等笔试真题精粹
c语言·数据结构·c++·算法·链表
多则惑少则明1 小时前
【算法题4】找出字符串中的最长回文子串(Java版)
java·开发语言·数据结构·算法
迷途之人不知返1 小时前
二叉树题目
数据结构·算法
迈巴赫车主3 小时前
蓝桥杯20534爆破 java
java·数据结构·算法·职场和发展·蓝桥杯
坚持就完事了3 小时前
数据结构之链表
数据结构·python·算法·链表
小李小李快乐不已4 小时前
图论理论基础(3)
数据结构·c++·算法·图论