題目:以array實作Stack,包含下列函數
Push,Pop,Top,Print_Stack會以Pop方式將stack清空,並依序印出stack中的資料
測試資料:
Push(23)Push(24)Push(25)Pop()Top()Push(26)Print_Stack();
public class Stack_Function {
//The size of the stack
int MaxSize = 10;
//Practice the stack via array
int stack[] = new int[ MaxSize ];
//The pointer of the stack
int top = -1;
//Push function
void push (int value)
{
//Test whether the stack is full or not
if(top >= MaxSize-1)
{
System.out.println("The stack is full!!! Cannot push any item into the stack!!!");
}
//If not, we can push the item into the stack
else
{
//Push the item into the stack
top++;
stack[top] = value;
//Print out all of the items in the stack
System.out.printf("After pushing %d, the items in stack are ( top -> bottom ): ", value);
for(int i=top; i>=0; i--)
System.out.printf("%d ", stack[i]);
System.out.println();
}
}
//Pop function
void pop ()
{
//Test whether the stack is empty or not
if (top < 0)
System.out.println("The Stack is empty!!! Cannot pop out any item!!!");
//If not, thus we can pop out the item from stack
else
{
//Pop out the item from stack
System.out.printf("After poping %d, the items in stack are ( top -> bottom ): ", stack[top]);
top--;
//Print out all of the item in the stack
for(int i=top; i>=0; i--)
System.out.printf("%d ", stack[i]);
System.out.println();
}
}
//Get the value of the top item of the stack
void top ()
{
//Test whether the stack is empty or not
if (top < 0)
System.out.println("The Stack is empty!!! Cannot get the top item!!!");
else
System.out.printf("Get the value of the top item from stack: %d\n", stack[top]);
}
//Take out and print out all of the items in the stack
void Print_Stack ()
{
System.out.print("Take out all of the items from stack: ");
//Take and print out all of the items
for(; top>=0; top--)
System.out.printf("%d ", stack[top]);
System.out.println();
}
}
以上是我寫出來的,但是我希望能夠再簡單一點容易懂一點.
2007-03-21 20:30:43 · 2 個解答 · 發問者 Anonymous in 電腦與網際網路 ➔ 程式設計
1. push() 和 pop() 在功能定義上來說,應該不需要將 Stack 的內容印出,而且如果真的有必要的話,可以用 PrinkStack() 以避免程式碼重覆。
請參考我的做法
public class Stack_Function {
int MaxSize = 10;
int stack[] = new int[ MaxSize ];
int top = -1;
void push (int value)
{
if(top >= MaxSize-1)
System.out.println("The stack is full!!! Cannot push any item into the stack!!!");
else
{
stack[++top] = value;
System.out.println("Push " + value);
// System.out.printf("After pushing %d, the items in stack are ( top -> bottom ): ", value);
// Print_Stack();
}
}
void pop ()
{
if (top < 0)
System.out.println("The Stack is empty!!! Cannot pop out any item!!!");
else
{
int value = stack[top--];
System.out.println("Pop " + value);
// System.out.printf("After poping %d, the items in stack are ( top -> bottom ): ", value);
// Print_Stack();
}
}
void top ()
{
if (top < 0)
System.out.println("The Stack is empty!!! Cannot get the top item!!!");
else
System.out.printf("Get the value of the top item from stack: %d\n", stack[top]);
}
void Print_Stack()
{
System.out.println("Pop out and print out all of the items from stack: ");
while (top >= 0)
pop();
}
public static void main(String[] args) {
Stack_Function sf = new Stack_Function();
sf.push(23);
sf.push(24);
sf.push(25);
sf.pop();
sf.top();
sf.push(26);
sf.Print_Stack();
}
}
2007-03-22 08:10:41 · answer #1 · answered by ? 7 · 0⤊ 0⤋
你寫的很好啊.只不過有2個奇怪的地方.
1. 你的top()函式是要回傳在stack最上面的值.為何其回傳型態是void?
2. 你的Print_Stack()應是用pop()和top()而不是直接處理top和stack[].
2007-03-22 12:29:23 補充:
以我個人的意見.
1. push()和pop()的回傳值應都是int.如果push或pop成功的話那就回傳0.不然就回傳1.
2. top()應回傳stack最上面的值.
3. Print_Stack()應用pop()和top()來處理.而不是直接更改top和stack[].
因函式裡應不需顯示(除非是在除錯).
2007-03-22 02:10:35 · answer #2 · answered by 龍 7 · 0⤊ 0⤋