栈的Node实现方式

/**
* Implementation of a stack using nodes.
* Unlimited size, no arraylist.
*
* @author Kyler Smith, 2017
*/

public class NodeStack<Item> {

    /**
    * Entry point for the program.
    */
    public static void main(String[] args) {
        NodeStack<Integer> Stack = new NodeStack<Integer>();

        Stack.push(3);
        Stack.push(4);
        Stack.push(5);
        System.out.println("Testing :");
        Stack.print();              // prints : 5 4 3

        Integer x = Stack.pop();     // x = 5
        Stack.push(1);
        Stack.push(8);
        Integer y = Stack.peek();    // y = 8
        System.out.println("Testing :");
        Stack.print();                // prints : 8 1 4 3

        System.out.println("Testing :");
        System.out.println("x : " + x);
        System.out.println("y : " + y);
    }

    /**
    * Information each node should contain.
    * @value data : information of the value in the node
    * @value head : the head of the stack
    * @value next : the next value from this node
    * @value previous : the last value from this node
    * @value size : size of the stack
    */
    private Item data;
    private static NodeStack<?> head;
    private NodeStack<?> next;
    private NodeStack<?> previous;
    private static int size = 0;

    /**
    * Constructors for the NodeStack.
    */
    public NodeStack() {
    }

    private NodeStack(Item item) {
        this.data = item;
    }

    /**
    * Put a value onto the stack.
    *
    * @param item : value to be put on the stack.
    */
    public void push(Item item) {

        NodeStack<Item> newNs = new NodeStack<Item>(item);

        if(this.isEmpty()) {
            NodeStack.setHead(new NodeStack<>(item));
            newNs.setNext(null);
            newNs.setPrevious(null);
        } else {
            newNs.setPrevious(NodeStack.head);
            NodeStack.head.setNext(newNs);
            NodeStack.head = newNs;
        }

        NodeStack.setSize(NodeStack.getSize() + 1);
    }

    /**
    * Value to be taken off the stack.
    *
    * @return item : value that is returned.
    */
    public Item pop() {

        Item item = (Item) NodeStack.head.getData();

        NodeStack.head = NodeStack.head.getPrevious();
        NodeStack.head.setNext(null);

        NodeStack.setSize(NodeStack.getSize() - 1);

        return item;
    }

    /**
    * Value that is next to be taken off the stack.
    *
    * @return item : the next value that would be popped off the stack.
    */
    public Item peek() {
        return (Item) NodeStack.head.getData();
    }

    /**
    * If the stack is empty or there is a value in.
    *
    * @return boolean : whether or not the stack has anything in it.
    */
    public boolean isEmpty() {
        return NodeStack.getSize() == 0;
    }

    /**
    * Returns the size of the stack.
    *
    * @return int : number of values in the stack.
    */
    public int size() {
        return NodeStack.getSize();
    }

    /**
    * Print the contents of the stack in the following format.
    *
    * x <- head (next out)
    * y
    * z <- tail (first in)
    * .
    * .
    * .
    *
    */
    public void print() {
        for(NodeStack<?> n = NodeStack.head; n != null; n = n.previous) {
            System.out.println(n.getData().toString());
        }
    }

    /** Getters and setters (private) */
    private NodeStack<?> getHead() {
        return NodeStack.head;
    }

    private static void setHead(NodeStack<?> ns) {
        NodeStack.head = ns;
    }

    private NodeStack<?> getNext() {
        return next;
    }

    private void setNext(NodeStack<?> next) {
        this.next = next;
    }

    private NodeStack<?> getPrevious() {
        return previous;
    }

    private void setPrevious(NodeStack<?> previous) {
        this.previous = previous;
    }

    private static int getSize() {
        return size;
    }

    private static void setSize(int size) {
        NodeStack.size = size;
    }

    private Item getData() {
        return this.data;
    }

    private void setData(Item item) {
        this.data = item;
    }
}

栈算是比较简单的,pop push peek ,但是用的地方很多,方法栈,变量名等都是压栈弹栈

原文地址:https://www.cnblogs.com/CherryTab/p/11939117.html

时间: 2024-08-01 04:17:42

栈的Node实现方式的相关文章

打通前后端全栈开发node+vue进阶【课程学习系统项目实战详细讲解】(3):用户添加/修改/删除 vue表格组件 vue分页组件

第三章 建议学习时间8小时      总项目预计10章 学习方式:详细阅读,并手动实现相关代码(如果没有node和vue基础,请学习前面的vue和node基础博客[共10章] 演示地址:后台:demoback.lalalaweb.com  前台:demo.lalalaweb.com 演示过程中可能会发现bug,希望即时留言反馈,谢谢 源码下载:https://github.com/sutianbinde/classweb               //不是全部的代码,每次更新博客才更新代码 学

二叉树的遍历(基于栈的非递归方式实现)

在写二叉树的时候如果用递归实现二叉树的遍历很简单,但是用非递归来实现二叉树的遍历就不那么简单了需要一些技巧. 那为什么还要非递归实现呢?个人理解:如果树的高度很大,超过了允许递归的次数,那么就会出错,比如我记得python只允许递归100次(不知道记错没) 这时候用迭代就要保险的多,不会出错. 下面先来做基本的准备说明: 1 #include<iostream> 2 #include<stack> 3 4 #define null NULL 5 6 template<type

Web全栈-网页的布局方式、浮动流基本概念

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>网页的布局方式</title> <style> /* div,h1,p{ border: 1px solid #000; } span,strong,b{ border: 1px solid #000; } */ *{ margin: 0; pa

九、运用栈的知识对后缀表达式的运算方式进行表达

前言:中缀表达式符合人们的阅读习惯:  如:5+3 后缀表达式符合计算机的运算习惯:如:53+ 现在运用栈的知识对后缀表达式的运算方式进行表达 1.LinkList.h #ifndef _LINKLIST_H_ #define _LINKLIST_H_ typedef void LinkList; typedef struct _tag_LinkListNode LinkListNode; struct _tag_LinkListNode { LinkListNode* next; }; Lin

六、栈的实现两种方式

前言:栈的特点:先进先出,只在栈顶进行操作.栈低密封,不进行操作,栈的实现有两种方式,通过对线性表实现进行复用.安全性高. 实现栈的方式: 第一种:以顺序结构的方式实现:将顺序表的队尾作为栈顶 第二种:以链式结构的方式实现:将链式表的队头作为栈顶 第一种实现方式(顺序结构): 1.SeqList.h #ifndef _SEQLIST_H_ #define _SEQLIST_H_ typedef void SeqList; typedef void SeqListNode; SeqList* Se

顺序栈与链表栈的实现

栈是一种常见的数据结构,它虽然有栈顶和栈底之分,但它只能从一端操作(插入或删除),从而是一种"先进后出"的操作模式.向栈内进数据称为压栈(Push),从栈里取出数据叫出栈(POp).例如压栈顺序为1.2.3.4.5,着出栈的顺序为5.4.3.2.1(只考虑一次性出栈的情况). 栈按照存储的方式,又分为顺序栈和链表栈.顺序栈基于数组实现,所以顺序栈存储数据的内存是连续的,在创建栈时规定好栈的大小,这样对内存的使用效率并不高.而链式栈则是采用了链表来实现,其元素的存储地址是不连续的,而且是

当今最流行的Node.js应用开发框架简介

快速开发而又容易扩展,高性能且鲁棒性强.Node.js的出现让所有网络应用开发者的这些梦想成为现实.但是,有如其他新的开发语言技术一样,从头开始使用Node.js的最基本功能来编写代码构建应用是一个非常划不来的耗时的事情.这个问题的解决方案非常简单且已经经受起时间的考验:使用一个已经提前打造好的开发框架.因此才会有如此多的如Express.js,Koa,Sails.js等框架的概念提出来并加以实现. 这些开发框架的角色非常简单.就是要去为应用开发人员节省时间,让我们不用话费太多精力在一些不必要的

数据结构基础(4)C语言实现栈--链式存储(动态栈)

写程序的时候,我们经常会说基本类型变量存在栈内存,引用类型的变量(对象,数组)存在堆内存.现在我们来看看栈这种数据结构是怎么实现的. 定义 一种可以实现"先进后出" 的存储结构 栈类似于往箱子放衣服,先放的最后拿 栈的分类: 静态栈:以数组方式实现,删除时删除下标最大的,插入时从最大下标+1插入 动态栈:以链表方式实现,删除和插入都是从头部 算法:出栈(POP),入栈(PUSH) 应用: 1.      函数调用 :在函数f中调用另一个g函数,在g函数中调用k函数 执行到要调用g函数位

C++ 栈 (链表实现)

第一.基本概念 栈中的元素遵守“先进后出”的原则(LIFO,Last In First Out) 只能在栈顶进行插入和删除操作 压栈(或推入.进栈)即push,将数据放入栈顶并将栈顶指针加一 出栈(或弹出)即pop,将数据从栈顶删除并将栈顶指针减一 栈的基本操作有:pop,push,判断空,获取栈顶元素,求栈大小 第二.实现方式 栈一般 采用数组或者链表2中方式实现,各有各的特点,我这里采用倒插法链表实现 第三.代码实现 #pragma once #include <iostream> usi