使用java.util包中的Stack類創(chuàng)建一個棧對象
建網(wǎng)站原本是網(wǎng)站策劃師、網(wǎng)絡(luò)程序員、網(wǎng)頁設(shè)計師等,應(yīng)用各種網(wǎng)絡(luò)程序開發(fā)技術(shù)和網(wǎng)頁設(shè)計技術(shù)配合操作的協(xié)同工作。創(chuàng)新互聯(lián)專業(yè)提供做網(wǎng)站、成都做網(wǎng)站,網(wǎng)頁設(shè)計,網(wǎng)站制作(企業(yè)站、響應(yīng)式網(wǎng)站開發(fā)、電商門戶網(wǎng)站)等服務(wù),從網(wǎng)站深度策劃、搜索引擎友好度優(yōu)化到用戶體驗的提升,我們力求做到極致!
public Object push(Object data);輸入數(shù)據(jù),實現(xiàn)壓棧
public Object pop();輸出數(shù)據(jù),實現(xiàn)彈棧
public boolean empty()判空
public Object peek();查看棧頂元素
可以去查查API嘛
我也是學(xué)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("?,F(xiàn)在是空了");
system("pause");
return?0;
}
實現(xiàn)了棧的基本功能.寫的比較倉促,沒加安全性的檢查.
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());
}
}