描述棧抽象數(shù)據(jù)類型的SStack接口的聲明
創(chuàng)新互聯(lián)公司主打移動(dòng)網(wǎng)站、成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、網(wǎng)站改版、網(wǎng)絡(luò)推廣、網(wǎng)站維護(hù)、主機(jī)域名、等互聯(lián)網(wǎng)信息服務(wù),為各行業(yè)提供服務(wù)。在技術(shù)實(shí)力的保障下,我們?yōu)榭蛻舫兄Z穩(wěn)定,放心的服務(wù),根據(jù)網(wǎng)站的內(nèi)容與功能再?zèng)Q定采用什么樣的設(shè)計(jì)。最后,要實(shí)現(xiàn)符合網(wǎng)站需求的內(nèi)容、功能與設(shè)計(jì),我們還會(huì)規(guī)劃穩(wěn)定安全的技術(shù)方案做保障。
public interfaceSStackE //棧接口
{
boolean isEmpty(); //判斷是否空棧,若空棧返回true
boolean push(E element); //元素element入棧,若操作成功返回true
E pop(); //出棧,返回當(dāng)前棧頂元素,若??辗祷豱ull
E get(); //取棧頂元素值,未出棧,若??辗祷豱ull
}
順序棧類具體操作方法的聲明:
importdataStructure.linearList.SStack;
public classSeqStackE implements SStackE
//順序棧類
{
private Object value[]; //存儲(chǔ)棧的數(shù)據(jù)元素
private int top; //top為棧頂元素下標(biāo)
public SeqStack(int capacity) //構(gòu)造指定容量的空棧
{
this.value = newObject[Math.abs(capacity)];
this.top=-1;
}
public SeqStack() //構(gòu)造默認(rèn)容量的空棧
{
this(10);
}
public boolean isEmpty() //判斷是否空棧,若空棧返回true
{
return this.top==-1;
}
public boolean push(E element) //元素element入棧,若操作成功返回true
{
if (element==null)
return false; //空對象(null)不能入棧
if (this.top==value.length-1) //若棧滿,則擴(kuò)充容量
{
Object[] temp = this.value;
this.value = newObject[temp.length*2];
for (int i=0; itemp.length;i++)
this.value[i] = temp[i];
}
this.top++;
this.value[this.top] = element;
return true;
}
public E pop() //出棧,返回當(dāng)前棧頂元素,若??辗祷豱ull
{
if (!isEmpty())
return (E)this.value[this.top--];
else
return null;
}
public E get() //取棧頂元素值,未出棧,棧頂元素未改變
{
if (!isEmpty())
return (E)this.value[this.top];
else
return null;
}
public String toString() //返回棧中各元素的字符串描述
{
String str="{";
if (this.top!=-1)
str +=this.value[this.top].toString();
for (int i=this.top-1; i=0; i--)
str += ","+this.value[i].toString();
return str+"} ";
}
實(shí)例引用public static void main(String args[])
{
SeqStackString stack = newSeqStackString(20);
System.out.print("Push: ");
char ch='a';
for(int i=0;i5;i++)
{
String str =(char)(ch+i)+"";
stack.push(str);
System.out.print(str+" ");
}
System.out.println("\n"+stack.toString());
System.out.print("Pop : ");
while(!stack.isEmpty()) //全部出棧
System.out.print(stack.pop().toString()+" ");
System.out.println();
}
//你明確說一下哪里不明白?注釋還可以啊?
#includeiostream
using?namespace?std;
class?IntStack{?????????????????????????????//整數(shù)棧類
public:
virtual?void?push(int)=0;?????????????????//入棧
virtual?int?pop()=0;??????????????????????//出棧并返回出棧元素
virtual?int?topElement()const=0;??????????//返回棧頂元素,但不出棧
virtual?bool?isEmpty()const=0;????????????//判斷是否???/p>
};
class?SeqStack:?public?IntStack{
int?data[100];???//?存放棧元素的數(shù)組
int?top;?????????//?棧頂元素的下標(biāo)
public:
//**********found**********
SeqStack():top(-1){}????????????//?把top初始化為-1表示???/p>
void?push(int?n){?data[++top]=n;?}?//下標(biāo)+1?壓入棧?這里沒什么難得吧。
//**********found**********
int?pop(){?return?data[top--];?}??//同樣?先取棧頂元素,然后下標(biāo)-1
int?topElement()const{?return?data[top];?}?//取棧頂元素
bool?isEmpty()const{?return?top==-1;?}??//判斷是否為空
};
struct?Node{
int?data;
Node?*next;
};
class?LinkStack:?public?IntStack{
Node?*top;
public:
//**********found**********
LinkStack():?top(NULL){}?????//?把top初始化為NULL表示???/p>
void?push(int?n){?
Node?*p=new?Node;??//new一個(gè)新Node
p-data=n;????//將n賦值給值域
//**********found**********
p-next=top;??//將p的指針域指向top
top=p;????//將top指向p?采用的頭插法
}
int?pop(){?
int?d=top-data;;?//這里先取棧頂?shù)脑?/p>
top=top-next;??//top指針略過棧頂?shù)脑?下一個(gè)元素成為棧頂元素?//這里做法不嚴(yán)謹(jǐn)?需要把節(jié)點(diǎn)的內(nèi)存釋放掉
return?d;????
}
int?topElement()const{?return?top-data;?}
bool?isEmpty()const{?return?top==NULL;?}
};
void?pushData(IntStack?st){
st.push(8);
st.push(1);
st.push(3);
st.push(6);
st.push(4);
}
void?popData(IntStack?st){
while(!st.isEmpty())?coutst.pop()'?';?//不為空一直pop
}
int?main(){
SeqStack?st1;?pushData(st1);?popData(st1);?//兩個(gè)測試函數(shù)?把數(shù)據(jù)壓棧?然后出棧。。
coutendl;
LinkStack?st2;?pushData(st2);?popData(st2);
coutendl;
return?0;
}
你得明白棧的定義。代碼執(zhí)行的時(shí)候是執(zhí)行一個(gè)方法,執(zhí)行完,返回方法的上一個(gè)代碼塊繼續(xù)往下執(zhí)行后面的內(nèi)容。這樣的話是不是就是一個(gè)棧結(jié)構(gòu)了?先進(jìn)后出。方法一邊執(zhí)行,一邊往棧里面存數(shù)據(jù),等執(zhí)行完了就取出數(shù)據(jù)(取出的是返回值,是最后一個(gè)存進(jìn)去的 棧結(jié)構(gòu)是后進(jìn)先出),然后執(zhí)行外面的代碼。這么說你可能不明白,我給你舉個(gè)例子。
int sub(int a,int b){
return a+b;
}
int c = sub(2,3);//注意執(zhí)行這條語句的時(shí)候是不是執(zhí)行了一個(gè)方法?
//那么語句執(zhí)行的時(shí)候是要從左往右執(zhí)行的對吧,但是事實(shí)的邏輯卻是先算出來sub(2,3)這個(gè)方
//法的返回值,然后再把返回值(5)賦值給 c ,那么這個(gè)怎么實(shí)現(xiàn),肯定是一個(gè)棧的數(shù)據(jù)結(jié)構(gòu),編譯的時(shí)候先把”int c = “入棧,然后再把 sub(2,3),入棧,執(zhí)行的時(shí)候,從棧里面取,取的第一個(gè)肯定是sub(2,3)吧?于是就計(jì)算出等于5,繼續(xù)取,取出了int c =,然后就和5對接上了,就把值賦給c了。這只是一個(gè)小例子。
道理是這樣,但是具體的存取可不是這樣的哦。具體的存取應(yīng)該分的非常細(xì)膩,應(yīng)該是按照java語法的最小單位來往棧里存取的。說白了一句話,程序運(yùn)行的時(shí)候的先后順序是跟人大腦想問題的順序一樣的,但是代碼不是按照這樣的順序?qū)懙模◤淖蟮接遥?,于是就用棧結(jié)構(gòu)來達(dá)到這樣的效果。
這么說,明白了嗎?
這是我寫的棧,你看看
#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;
}
//這是JDK提供的棧
import java.util.Stack;
public class UsingStack {
public static void main(String[] args) {
//構(gòu)造棧對象,使用類型限制,只能存儲(chǔ)Integer數(shù)據(jù)
StackInteger s = new StackInteger();
//1、2、3依次入棧
s.push(1);
s.push(2);
s.push(3);
//3、2、1依次出棧
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
}
}
//這是我寫的順序結(jié)構(gòu)的棧
import java.util.EmptyStackException;
import java.util.Vector;
public class UsingStack{
public static void main(String[] args){
//構(gòu)造棧對象,使用類型限制,只能存儲(chǔ)Integer數(shù)據(jù)
MyStackInteger s = new MyStackInteger();
//1、2、3依次入棧
s.push(1);
s.push(2);
s.push(3);
//3、2、1依次出棧
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
}
}
/**
* 棧類
* @author developer_05
* @param T
*/
class MyStackT extends VectorT{
/**
* 構(gòu)造方法
*/
public MyStack(){
}
/**
* 入棧方法
* @param item 待入棧的元素
* @return 返回入棧的元素
*/
public T push(T item) {
addElement(item);
return item;
}
/**
* 出棧方法(同步處理)
* @return 返回出棧元素
*/
public synchronized T pop() {
T obj;
int len = size();
if (len == 0)
throw new EmptyStackException();
obj = elementAt(len - 1);
removeElementAt(len - 1);
return obj;
}
/**
* 判斷棧是否為空的方法
* @return 返回true(???或false(棧非空)
*/
public boolean empty() {
return size() == 0;
}
private static final long serialVersionUID = 1L;
}
public StackX(int maxSize){
maxSize=maxSize;
stackarray=new long[maxSize];
top=-1;
}
不好意思,你犯了一個(gè)很傻的錯(cuò)誤,這里應(yīng)該是this.maxSize = maxSize,否則的話,你的實(shí)例變量maxSize還是沒有被初始化為正確值,而只是默認(rèn)值0
本文標(biāo)題:進(jìn)棧出棧代碼java代碼,java入棧出棧
轉(zhuǎn)載注明:http://chinadenli.net/article36/hohspg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、響應(yīng)式網(wǎng)站、靜態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)