链表的实现和数组的实现最大的不同在于链表的插入操作代价要低于数组,不过总体代价还是数组更低,因为链表的构造和连接部分代价其实很高。
基本结构
private Node head = null;
push操作
public void push(String str) { // create a new node and put the str into item Node newNode = new Node(str); // insert before the new node newNode.next = head; head = newNode; }
pop操作
public String pop() { String popItem; // store the pop String // if stack is not empty if (!isEmpty()) { popItem = head.item; head = head.next; return popItem; } // empty can not delete else { System.err.println("Stack is empty"); return ""; } }
判空
public boolean isEmpty() { return head == null; }
求size的操作
public int size() { int size = 0; while (head != null) { head = head.next; size++; } return size; }
原文地址:https://www.jb51.cc/datastructure/382860.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。