導航:首頁 > 編程語言 > javastackpoptop

javastackpoptop

發布時間:2022-01-20 07:52:50

① 僅使用Stack類中的構造函數、push()、peek()、pop()、以及empty()等方法實現任意長度數組的逆序

// stack.java
// demonstrates stacks
// to run this program: C>java StackApp
////////////////////////////////////////////////////////////////
class StackX
{
private int maxSize; // size of stack array
private long[] stackArray;
private int top; // top of stack
//--------------------------------------------------------------
public StackX(int s) // constructor
{
maxSize = s; // set array size
stackArray = new long[maxSize]; // create array
top = -1; // no items yet
}
//--------------------------------------------------------------
public void push(long j) // put item on top of stack
{
stackArray[++top] = j; // increment top, insert item
}
//--------------------------------------------------------------
public long pop() // take item from top of stack
{
return stackArray[top--]; // access item, decrement top
}
//--------------------------------------------------------------
public long peek() // peek at top of stack
{
return stackArray[top];
}
//--------------------------------------------------------------
public boolean isEmpty() // true if stack is empty
{
return (top == -1);
}
//--------------------------------------------------------------
public boolean isFull() // true if stack is full
{
return (top == maxSize-1);
}
public void tofun(long a,long b,long c,long d,long e,long f,long g,long h,long i,long k)
{
push(a);
push(b);
push(c);
push(d);
push(e);
push(f);
push(g);
push(h);
push(i);
push(k);
}
}
class StackApp
{
public static void main(String[] args)
{
StackX stackX=new StackX(10);
stackX.tofun(1,2,3,4,5,6,7,8,9,10);
while( !stackX.isEmpty() ) // until it's empty,
{ // delete item from stack
long value = stackX.pop();
System.out.print(value); // display it
System.out.print(" ");
} // end while
System.out.println("");
}
}

② 小菜鳥求教Java stack的實現問題

stack類修改一下就行了,修改處見注釋

staticclassstack
{

Nodetop;

Objectpop()
{

if(top!=null)
{

Objecttopdata=top.data;

top=top.next;

returntopdata;

}

returnnull;

}

voidpush(Objecttopdata)
{
//Nodet=newNode((Integer)topdata);你push的是node,這邊當int處理,自然錯了
Nodet=(Node)topdata;

t.next=top;

top=t;

}


Objectpeek()
{
if(top!=null)
{
returntop.data;
}
//這邊再加個判斷,不過對於運行期異常,不處理也是可以的
else
{
returnnull;
}

}

}

③ 用JAVA編寫一個程序,希望有注釋,但不要太簡單,不要在別的地方復制,急用!

/**
* GenericLinkedStack.java
*/
package fix;

import java.util.EmptyStackException;

/**
*泛型的鏈式棧數據結構
*/
public class GenericLinkedStack<E> {
// 棧頂元素
private Item top = null;
// 返回棧頂元素,並彈出
public E pop() throws EmptyStackException {
if (isEmpty()) {
throw new EmptyStackException();
}
E e = top.value;
top = top.next;
return e;
}
/**
* 棧頂壓入一個元素
* @param e 被壓入的元素
*/
public void push(E e) {
Item curr = new Item(e);
curr.next = top;
top = curr;
}
/**
* 返回棧頂元素,但不出棧
* @return 棧頂元素
*/
public E peek() {
if (isEmpty()) {
throw new EmptyStackException();
}
return top.value;
}
/**
* 判斷棧是否為空
* @return 判斷結果
*/
public boolean isEmpty() {
return top == null;
}
/**
* 棧中元素
* @author jilen
*
*/
class Item {
//元素
private E value;
//下一個
private Item next;
public Item(E e) {
this.value = e;
}
public E getValue() {
return value;
}
public void setValue(E value) {
this.value = value;
}
public Item getNext() {
return next;
}
public void setNext(Item next) {
this.next = next;
}
}
}

/**
* InfixToPostfixConverter.java
*/
package fix;

import java.util.Hashtable;

/**
* @author jilen
*
*/
public class InfixToPostfixConverter {
// 操作符及其優先順序組成的鍵值對
private static final Hashtable<Character, Integer> operators;
private StringBuffer infix;
private StringBuffer postfix;
GenericLinkedStack<Character> stack = new GenericLinkedStack<Character>();
// 初始化操作符列表,static語句塊會在載入類時自動執行
static {
operators = new Hashtable<Character, Integer>();
operators.put('^', 4);
operators.put('*', 3);
operators.put('/', 3);
operators.put('%', 3);
operators.put('+', 2);
operators.put('-', 2);
operators.put('(', -1);
operators.put(')', 5);

}
/**
*
*/
public InfixToPostfixConverter(StringBuffer infix, StringBuffer postfix) {
this.infix = infix;
this.postfix = postfix;
}
/**
* 轉換函數
*/
public void convertToPostfix() {
// 對輸入字元串中字元遍歷
for (int i = 0, n = infix.length(); i < n; i++) {
char c = infix.charAt(i);
// 是數字之間添加到轉換後字元串
if (isNumber(c)) {
postfix.append(c);
} else if (isOperator(c)) {
switch (c) {
// '(' 直接入棧
case '(':
stack.push(c);
break;
// ')' 彈出元素直到碰到'('
case ')':
while (!stack.isEmpty() && stack.peek() != '(') {
postfix.append(stack.pop());
}
stack.pop();
break;
// 其他操作符
default:
// 當前操作符比棧頂操作符優先順序高,直接入棧
if (stack.isEmpty() || precedence(c, stack.peek())) {
stack.push(c);
}
// 當前操作符比棧頂操作符優先順序低,出棧直到為空或棧頂優先順序低於當前操作符
else if (!precedence(c, stack.peek())) {
while (!stack.isEmpty() && !precedence(c, stack.peek())) {
postfix.append(stack.pop());
}
stack.push(c);
}
break;
}
}
}
// 若棧中還有操作符,所以元素出棧
while (!stack.isEmpty()) {
postfix.append(stack.pop());
}
}
/**
* 判斷是否為操作符
* @param c
* @return
*/
public static boolean isOperator(char c) {
return operators.containsKey(c);
}
/**
* 優先順序大小關系operator1 > operator2 則返回true,否則false
* @param operator1
* @param operator2
* @return 判斷結果
*/
public static boolean precedence(char operator1, char operator2) {
return operators.get(operator1) > operators.get(operator2);
}
/**
* 是否數字
* @param c 要判斷的字元
* @return 判斷結果
*/
public static boolean isNumber(char c) {
return c >= '0' && c <= '9';
}
}

/**
*Main.java測試類
*/
package fix;

/**
* @author Administrator
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
StringBuffer infix = new StringBuffer("(1+2)*3/4");
StringBuffer postfix = new StringBuffer();
InfixToPostfixConverter converter = new InfixToPostfixConverter(infix,
postfix);
converter.convertToPostfix();
System.out.println(postfix.toString());
}
}
中綴轉後綴的程序,有GenericLinkedStack.java,InfixToPostfix.java,Main.java三個源文件需要放在fix包下

④ 用java編寫出來:用數組實現一個棧

} } 使用java中Stack類.跟數據結構中的棧是一個意思。 不用Stack,直接new一個長度為10的數組就可以了

⑤ java Collection ArrayStack 的問題...

粗略的看了一下,
for(int i=0; i<6; i++)

stack.push(i*2);
這一步會調用
if(!isFull()) ---> if((top+1) == stackSize)----->elements= new Object[top];數組會被創建5次(01234)
數組第一次被創建是emements=new Object[0];這樣會拋出異常。假如沒拋出異常:
只有第五次的elements[4]=8,會被存入數組,其他elements[0]elements[1]elements[2]elements[3]沒有值,默認為0(你的是Object,不一定為零,先看成0)。所以elements={0,0,0,0,8}
之後System.out.print(stack.pop() + " "); 應該會輸出0 (elements[3])

錯了兩點不該每次都創建elements數組;不該在pop中將top--後輸出var,要反過來。
最好將Object數組改成Integer數組,這樣測試。

⑥ c++堆棧中 top() pop()的具體作用是什麼

top()是將指針置於堆棧頂部pop()是從堆棧中提取數據

⑦ 關於數據結構(java)的一個代碼

描述棧抽象數據類型的SStack介面的聲明
public interfaceSStack<E> //棧介面
{
boolean isEmpty(); //判斷是否空棧,若空棧返回true
boolean push(E element); //元素element入棧,若操作成功返回true
E pop(); //出棧,返回當前棧頂元素,若棧空返回null
E get(); //取棧頂元素值,未出棧,若棧空返回null
}
順序棧類具體操作方法的聲明:

importdataStructure.linearList.SStack;

public classSeqStack<E> implements SStack<E>
//順序棧類

{
private Object value[]; //存儲棧的數據元素
private int top; //top為棧頂元素下標

public SeqStack(int capacity) //構造指定容量的空棧
{
this.value = newObject[Math.abs(capacity)];
this.top=-1;
}
public SeqStack() //構造默認容量的空棧
{
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) //若棧滿,則擴充容量
{
Object[] temp = this.value;
this.value = newObject[temp.length*2];
for (int i=0; i<temp.length;i++)
this.value[i] = temp[i];
}
this.top++;
this.value[this.top] = element;
return true;
}

public E pop() //出棧,返回當前棧頂元素,若棧空返回null
{
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+"} ";
}
實例引用public static void main(String args[])
{
SeqStack<String> stack = newSeqStack<String>(20);
System.out.print("Push: ");
char ch='a';
for(int i=0;i<5;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();
}

⑧ 求java 裡面「堆棧」的簡單解釋,通俗例子!

java堆棧類源程序import java.util.LinkedList;import java.io.*;import java.util.NoSuchElementException;/** * * @version 1.00 06/12/19 */public class MStack { LinkedList list; public MStack() { list=new LinkedList(); } public static void main(String[] args) { MStack MStack=new MStack(); MStack.push("ok"); MStack.push("i am ok"); System.out.println(MStack.pop()); System.out.println(MStack.pop()); System.out.println(MStack.pop()); } //彈出元素 public Object pop() { try { Object o=list.getFirst(); list.removeFirst(); return o; } catch(NoSuchElementException e) { //System.out.println(e); return null; } } //壓入元素 public void push(Object o) { list.addFirst(o); } //得到棧頂元素 public Object getTop() { if(list.size()!=0) { return list.getFirst(); } else { return null; } } }Junit測試程序import junit.framework.*;public class TestMStack extends TestCase{ MStack s=new MStack(); public TestMStack(String name) { super(name); } public void setUp() { } public void testCase1() { MStack MStack=new MStack(); s.push("ok"); MStack.push("ok"); Assert.assertEquals(s.getTop(),MStack.getTop()); } public static void main(String[] args) { junit.textui.TestRunner.run(TestMStack.class); } }// http://blog.csdn.net/wfisone/archive/2009/05/09/4163778.aspx

閱讀全文

與javastackpoptop相關的資料

熱點內容
歐美動作愛情 瀏覽:915
word2013更改圖片 瀏覽:980
win10plsql 瀏覽:819
香港電影開頭一個女的在床上自慰 瀏覽:512
win10cdromsys下載 瀏覽:30
桌面程序hibernate 瀏覽:14
如何建蔬菜網站 瀏覽:579
android網路通信聊天 瀏覽:1
電影頭上裹著布還有紐扣 瀏覽:246
iphone6nfc充電 瀏覽:422
鐵銹戰爭的文件夾是哪個 瀏覽:184
大數據業務描述 瀏覽:162
古惑仔粵語版歌詞 瀏覽:897
韓國劇情片網站 瀏覽:759
自學滅火器編程該如何入手 瀏覽:817
網站ip地址怎麼防禦 瀏覽:572
大數據自動化部署 瀏覽:368
自動編程軟體有哪些有什麼特色 瀏覽:140
韓國污片網站 瀏覽:758

友情鏈接