链式堆栈(C,JAVA分别实现)
来源:互联网 发布:nokia软件下载 编辑:程序博客网 时间:2024/05/19 07:07
先附上,C语言。
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <malloc.h>struct Node {int date;struct Node *next;};void Init(Node *s){s->date = 0;s->next = NULL;}void Push(Node *s){Node *p = NULL;p = (Node *)malloc(sizeof(Node));scanf("%d", &p->date);p->next = s->next;s->next = p;}void Pop(Node *s){Node *p = NULL;p = s->next;printf("%d ", p->date);s->next = p->next;free(p);}void Destroy(Node *s){Node *p = NULL, *q = NULL;p = s;while (p != NULL){q = p;p = p->next;free(q);}}int main(){int n;Node s;Init(&s);printf("请输入入栈的数据个数:");scanf("%d", &n);for (int i = 0; i < n; i++)Push(&s);for (int i = 0; i < n; i++)Pop(&s);return 0;}
JAVA
package test01;public class StackNode{private int date;private StackNode next;StackNode(){date = 0;next = null;} public void clear(){ StackNode p = null, q = null; p = this.next; while(p.next != null){ q = p; p = p.next; q.date = 0; q.next = null; } } public int length(){ int len = 1; if(this.empty()) return 0; else{ StackNode p; p = this.next; while(p.next != null){ len++; p = p.next; } } return len; }public boolean full(){return false;} public boolean empty(){ if(next == null) return true; else return false; } public void push(int item){ StackNode p = new StackNode(); p.date = item; p.next = this.next; this.next = p; } int pop(){ StackNode p = null, q = null; p = this.next; q = p; this.next = p.next; return q.date; } public int peek(){ return this.next.date; } public static void main(String[] args){StackNode s = new StackNode();for(int i = 0; i < 10; i++)s.push(i + 1);int len = s.length();System.out.println("len = "+len);System.out.print("Output:");for(int i = 0; i < 10; i++){int date = s.pop();System.out.print(date+" ");}System.out.println(); }}
0 0
- 链式堆栈(C,JAVA分别实现)
- C/C++ 堆栈、队列的链式实现
- 堆栈链式实现入栈出栈等操作(C)
- 链式堆栈——C语言实现
- 链式堆栈.c
- 链式实现的堆栈
- 链式堆栈实现
- 链式堆栈的实现
- 数据结构C语言实现系列——链式堆栈
- 分别用数组和链表实现堆栈(C语言版)
- 单链表(C,JAVA分别实现)
- 数据结构Java实现05----栈:顺序栈和链式堆栈
- 数据结构Java实现05----栈:顺序栈和链式堆栈
- 数据结构Java实现05----栈:顺序栈和链式堆栈
- 数据结构Java实现05----栈:顺序栈和链式堆栈
- c 堆栈的链式存储
- C 堆栈的链式表示
- 堆栈的链式储存实现
- JMX监控 Tomcat Glassfish JBoss WebLogic WebSphere
- Idea快捷方式
- 数字电视视音频格式一览表
- 关于Action和Result的说明
- 参考
- 链式堆栈(C,JAVA分别实现)
- 2015-09-21
- 关于jsp内置对象的作业
- 使用Slidmenu
- 批判和质疑,要坚守自我
- 2015 ACM/ICPC 沈阳赛区网络赛 1010.Jesus Is Here (HDOJ5459)
- 排序算法——java实现
- win10使用genymotion出现Unable to start the virtual device(VirtualBox cannot start the virtual device)
- 目中无人是不行的