美文网首页程序员技术
普林斯顿Algorithms-1.3.2-包、队列、栈的链表实现

普林斯顿Algorithms-1.3.2-包、队列、栈的链表实现

作者: 蛋黄也可以很有派 | 来源:发表于2019-03-13 14:17 被阅读0次


Linked lists

 A linked list is a recursive data structure that is either empty (null) or a reference to a node having a generic item and a reference to a linked list. To implement a linked list, we start with a nested class that defines the node abstraction

Linked-list implementations of collections.

Linked list implementation of a stack. Stack.java implements a generic stack using a linked list. It maintains the stack as a linked list, with the top of the stack at the beginning, referenced by an instance variable first. To push() an item, we add it to the beginning of the list; to pop() an item, we remove it from the beginning of the list.

定义栈顶节点和n两个成员变量,创建Node类 push/pop:将栈顶节点更新,改变n

Linked list implementation of a queue. Program Queue.java implements a generic FIFO queue using a linked list. It maintains the queue as a linked list in order from least recently to most recently added items, with the beginning of the queue referenced by an instance variable first and the end of the queue referenced by an instance variable last. To enqueue() an item, we add it to the end of the list; to dequeue() an item, we remove it from the beginning of the list.

Linked list implementation of a bag. Program Bag.java implements a generic bag using a linked list. The implementation is the same as Stack.java except for changing the name of push() to add() and removing pop().

如何实现迭代写法???

To implement iteration in a collection:

Include the following import statement so that our code can refer to Java's java.util.Iterator interface:

import java.util.Iterator;

Add the following to the class declaration, a promise to provide an iterator() method, as specified in the java.lang.Iterableinterface:

implements Iterable<Item>

Implement a method iterator() that returns an object from a class that implements the Iterator interface:

public Iterator<Item> iterator() {

    return new ListIterator();

}

课后问答

Autoboxing Q + A 泛型Q + A

课后练习

Write a stack client Parentheses.java that reads in sequence of left and right parentheses, braces, and brackets from standard input and uses a stack to determine whether the sequence is properly balanced. 

For example, your program should print true for [()]{}{[()()]()} and false for [(]).

相关文章

  • 普林斯顿Algorithms-1.3.2-包、队列、栈的链表实现

    Linked lists A linked list is a recursive data structure ...

  • 数据结构java描述

    接口 栈 队列 集合 并查集 映射 数组 链表 栈 数组实现 链表实现 队列 数组实现 链表实现 二分搜索树 集合...

  • C语言第七次作业:链表

    707. 设计链表 空指针 空节点 225. 用队列实现栈 链式存储栈 双队列实现栈 232. 用栈实现队列 链式...

  • 基础算法学习与实践

    数组&链表 1. 快慢指针的方式实现判断链表是否有环 栈和队列 1. 栈实现队列(负负得正) ...

  • 数据结构与算法之数组与链表

    线性表包括数组,链表(单链表,双向链表,循环链表,双向循环链表,静态链表),栈(顺序栈,链式栈),队列(普通队列,...

  • 数据结构与算法之栈与队列

    线性表包括数组,链表(单链表,双向链表,循环链表,双向循环链表,静态链表),栈(顺序栈,链式栈),队列(普通队列,...

  • 第四章_栈和队列_2019-03-20

    基本知识点 栈:先进后出,队列:先进先出 栈和队列都既能用数组实现,又能用链表实现 栈和队列的基本操作:pop()...

  • 常见的数据结构

    常见的数据结构有: 数组 链表单链表、双向链表、循环链表、双向循环链表、静态链表 栈顺序栈、链式栈 队列普通队列、...

  • 用数组实现栈、队列

    用数组实现一个栈 用数组实现一个队列 用单链表实现给队列

  • 数据结构-系列文章

    线性表 单链表 单链表-OC实现 双链表 循环链表 栈 栈 队列 待完善 数组 待完善 树 待完善 图 待完善 哈...

网友评论

    本文标题:普林斯顿Algorithms-1.3.2-包、队列、栈的链表实现

    本文链接:https://www.haomeiwen.com/subject/jnvzpqtx.html