和C++里面一樣,有入棧,彈棧,查找函數(shù)

10年積累的網(wǎng)站建設(shè)、做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先制作網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有右江免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
import java.util.*;(引入包含棧類的頭文件)
相關(guān)函數(shù)介紹
boolean empty()
測(cè)試堆棧是否為空。
E peek()
查看堆棧頂部的對(duì)象,但不從堆棧中移除它。
E pop()
移除堆棧頂部的對(duì)象,并作為此函數(shù)的值返回該對(duì)象。
E push(E item)
把項(xiàng)壓入堆棧頂部。
int search(Object o)
返回對(duì)象在堆棧中的位置,以 1 為基數(shù)。
這是我寫的棧,你看看
#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("請(qǐng)輸入要入棧的元素\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("棧現(xiàn)在是空了");
system("pause");
return?0;
}
//你明確說一下哪里不明白?注釋還可以啊?
#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;????????????//判斷是否棧空
};
class?SeqStack:?public?IntStack{
int?data[100];???//?存放棧元素的數(shù)組
int?top;?????????//?棧頂元素的下標(biāo)
public:
//**********found**********
SeqStack():top(-1){}????????????//?把top初始化為-1表示棧空
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表示棧空
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è)測(cè)試函數(shù)?把數(shù)據(jù)壓棧?然后出棧。。
coutendl;
LinkStack?st2;?pushData(st2);?popData(st2);
coutendl;
return?0;
}
//這是JDK提供的棧
import java.util.Stack;
public class UsingStack {
public static void main(String[] args) {
//構(gòu)造棧對(duì)象,使用類型限制,只能存儲(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)造棧對(duì)象,使用類型限制,只能存儲(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;
}
你得明白棧的定義。代碼執(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í)行這條語(yǔ)句的時(shí)候是不是執(zhí)行了一個(gè)方法? //那么語(yǔ)句執(zhí)行的時(shí)候是要從左往右執(zhí)行的對(duì)吧,但是事實(shí)的邏輯卻是先算出來(lái)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對(duì)接上了,就把值賦給c了。這只是一個(gè)小例子。道理是這樣,但是具體的存取可不是這樣的哦。具體的存取應(yīng)該分的非常細(xì)膩,應(yīng)該是按照java語(yǔ)法的最小單位來(lái)往棧里存取的。說白了一句話,程序運(yùn)行的時(shí)候的先后順序是跟人大腦想問題的順序一樣的,但是代碼不是按照這樣的順序?qū)懙模◤淖蟮接遥谑蔷陀脳=Y(jié)構(gòu)來(lái)達(dá)到這樣的效果。這么說,明白了嗎?
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)題:java進(jìn)出棧代碼,c語(yǔ)言進(jìn)棧出棧完整代碼
文章起源:http://chinadenli.net/article33/dsgjiss.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、關(guān)鍵詞優(yōu)化、App開發(fā)、自適應(yīng)網(wǎng)站、網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)