资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java栈的链序存储代码 栈的链式存储结构代码

怎么用java代码实现栈内存?

使用java.util包中的Stack类创建一个栈对象

建网站原本是网站策划师、网络程序员、网页设计师等,应用各种网络程序开发技术和网页设计技术配合操作的协同工作。创新互联专业提供做网站、成都做网站,网页设计,网站制作(企业站、响应式网站开发、电商门户网站)等服务,从网站深度策划、搜索引擎友好度优化到用户体验的提升,我们力求做到极致!

public Object push(Object data);输入数据,实现压栈

public Object pop();输出数据,实现弹栈

public boolean empty()判空

public Object peek();查看栈顶元素

可以去查查API嘛

我也是学java的,大家一起进步。

链栈的入栈出栈代码

这是我写的栈,你看看

#includestdio.h

#includeiostream

typedef struct node{

int date;

node * next;

}SeqStack ;

SeqStack * init_SeqStack(SeqStack * top){

top=NULL;

return top;

}

int is_Empty(SeqStack * top){

if(top==NULL)return 1;

else return 0;

}

SeqStack * push_Stack(SeqStack * top){

SeqStack * New;

New=(SeqStack *)malloc(sizeof(SeqStack));

printf("请输入要入栈的元素\n");

scanf("%d",New-date);

New-next=top;

top=New;

return top;

}

SeqStack * pop_Stack(SeqStack * top,int m){

SeqStack * p=NULL;

if(!is_Empty(top)){ 

m=top-date;

p=top;

top=top-next;

free(p);

return top; 

}

}

SeqStack * top_Stack(SeqStack * top,int m){

if(!is_Empty(top)){

m= top-date;

return top;

}

}

int main(){

int m=0;

SeqStack * s=NULL;

init_SeqStack(s);

s=push_Stack(s);

s=push_Stack(s);

s=push_Stack(s);

s=push_Stack(s);

s=top_Stack(s,m);

printf("%d\n",m);

s=top_Stack(s,m);

printf("%d\n",m);

s=pop_Stack(s,m);

printf("%d\n",m);

s=top_Stack(s,m);

printf("%d\n",m);

if(is_Empty(s)) printf("栈现在是空了");

system("pause");

return 0;

}

【JAVA】写一个类,实现栈这种数据结构,要求底层数据使用ArrayList存储。

实现了栈的基本功能.写的比较仓促,没加安全性的检查.

import java.util.ArrayList;

public class Stack {

private ArrayList stack;

public Stack()

{

stack = new ArrayList();

}

public void clear()

{

stack.clear();

}

public int getTop()

{

int temp = (Integer)stack.get(stack.size()-1);

return temp;

}

public void push(int ele)

{

stack.add(ele);

}

public int pop()

{

int temp = (Integer)stack.get(stack.size()-1);

stack.remove(stack.size()-1);

return temp;

}

public int getSize()

{

return stack.size();

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Stack test = new Stack();

test.push(5);

test.push(6);

System.out.println(test.pop());

System.out.println(test.getTop());

System.out.println(test.pop());

test.clear();

System.out.println(test.getSize());

}

}


网站标题:java栈的链序存储代码 栈的链式存储结构代码
网页地址:http://cdkjz.cn/article/dopjosp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220