下压堆栈(链表实现)

import java.util.Iterator;
public class Stack<Item>
{
    private Node first;   //栈顶
    private int N;        //元素数量
    private class Node
    {   //定义节点的嵌套类
        Item item;
        Node next;
    }
    public boolean isEmpty() {  return first == null; } // 或: N == 0
	public int size() {  return N; }
	public void push(Item item)
	{	// 向栈顶添加元素
		Node oldfirst = first;
		first = new Node();
		first.item = item;
		first.next = oldfirst;
		N++;
	}
	public Item pop()
	{	//从栈顶删除元素
		Item item = first.item;
		first = first.next;
		N--;
		return item;
	}
	public Iterator<Item> iterator()
	{	return new ListIterator();	}
	private class ListIterator implements Iterator<Item>
	{
		private Node current = first;
		public boolean hasNext()
		{	return current != null;	}
		public void remove() { }
		public Item next()
		{
			Item item = current.item;
			current = current.next;
			return item;
		}
	}
}

  

原文地址:https://www.cnblogs.com/auhz/p/8964570.html

时间: 2024-08-04 19:46:20

下压堆栈(链表实现)的相关文章

下压堆栈(链表实现)

import java.util.Iterator; import java.util.Scanner; public class Stack<Item> implements Iterable<Item> { private Node first;// 栈顶 private int N;// 元素数量 // 定义结点的嵌套类 private class Node{ Item item; Node next; } public boolean isEmpty() { return

堆栈链表储存

//堆栈,链表实现#include<iostream>using namespace std;class stack{public: int data; stack*next; };stack*Linkstack(){ stack*s = new stack; s->next = NULL; //生成一个头结点,但不表示任何数 return s;}int pop(stack*head); //出栈void push(stack*head,int x);//进栈int main(){ st

最全C 语言常用算法详解-排序-队列-堆栈-链表-递归-树

具体 源代码 案例查看github,持续更新中............ github地址:https://github.com/Master-fd/C-Algorithm 1. 二分法查找 2. 冒泡排序 3. 插入排序 4. 希尔排序 5. 选择排序 6. 快速排序 7. 单链表实现堆栈 8. 单链表实现队列 9. 普通单链表 10. 递归实现斐波拉契数列 11. 递归实现strlen 12. 循环链表 13. 求素数 14. 双向链表 15. 顺序表实现队列 16. 顺序表实现栈 17. 顺

堆栈——链表实现

#include <iostream> #include <cstdio> #include <cstdlib> #include <ctime> using namespace std; using ElemType = int; // 堆栈结构 class Node { public: ElemType data; Node *next; }; // 初始化栈 Node* initStack(Node *head, int n) { srand(time

算法(第四版)学习笔记之java实现栈和队列(链表实现)

下压堆栈(链表实现): import java.util.Iterator; public class LinkedStack<Item> implements Iterable<Item> { public class Node { Item item; Node next; } private Node frist; private int N = 0; public boolean isEmpty() { return N == 0; } public int size()

栈的链式结构表示与实现——自己写数据结构

今天给大家介绍栈的链式结构,用dev-c++4.9.9.2调试通过,少废话直接上代码: 数据结构体存放文件stacklist.h文件如下 #ifndef _STACKLIST_H_ #define _STACKLIST_H_ typedef struct _Node { int data; struct _Node *pre; struct _Node *next; }Node,*pNode; typedef struct _Stack_Header { struct _Node *botton

数据存储的常用结构 堆栈、队列、数组、链表

数据存储的常用结构有:堆栈.队列.数组.链表.我们分别来了解一下: 堆栈,采用该结构的集合,对元素的存取有如下的特点: 先进后出(即,存进去的元素,要在后它后面的元素依次取出后,才能取出该元素).例如,子弹压进弹夹,先压进去的子弹在下面,后压进去的子弹在上面,当开枪时,先弹出上面的子弹,然后才能弹出下面的子弹. 栈的入口.出口的都是栈的顶端位置 压栈:就是存元素.即,把元素存储到栈的顶端位置,栈中已有元素依次向栈底方向移动一个位置. 弹栈:就是取元素.即,把栈的顶端位置元素取出,栈中已有元素依次

java:堆栈,队列,枚举,链表

Stack类 栈:(水杯喝水,先进后出) 栈是一种数据结构,是只能在某一端插入和删除的特殊线性表.他按照先进后出的原则存储数据 Enumeration(枚举) 1,hasMoreElements()    测试此枚举是否包含更多的元素 2,nextElements()    如果此枚举对象至少还有一个可提供的元素,则返回此枚举的下一个元素 ----------------------------- 代码演示: package day07; import java.util.Date; impor

《Java数据结构与算法》笔记-CH5-链表-4用链表实现堆栈

1 //用链表实现堆栈 2 /** 3 * 节点类 4 */ 5 class LinkS { 6 private long data; 7 public LinkS next; 8 9 public LinkS(long d) { 10 this.data = d; 11 } 12 13 public String toString() { 14 return String.valueOf(data); 15 } 16 } 17 18 /** 19 * 链表类 20 */ 21 class Li