
本文共 5743 字,大约阅读时间需要 19 分钟。
������������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������������������
������������������������������������������������������������������������FILO���First In, Last Out������������������������������������������������������������������������������������������push������������������������������������������������pop���������������������������������������������������������������������������������������������������������������������������������������������
���������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������
���������������������������������������������������������������������������������������������
public class ArrayStack { private static int[] stackArray; private static int top = -1; public ArrayStack(int stackArraySize) { stackArray = new int[stackArraySize]; } public boolean isEmpty() { return top == -1; } public void push(int value) { if (top == stackArray.length - 1) { System.out.println("������������������������"); return; } top++; stackArray[top] = value; } public int pop() { if (top == -1) { throw new RuntimeException("������������������������"); } int value = stackArray[top]; top--; return value; } public static void main(String[] args) { ArrayStack stack = new ArrayStack(4); Scanner scanner = new Scanner(System.in); boolean loop = true; while (loop) { System.out.println("��������������� show(���������)���top(������������)���push(������)���pop(������)���exit(������)"); String key = scanner.next(); switch (key) { case "show": stack.show(); break; case "top": stack.showTop(); break; case "push": System.out.println("���������������"); int value = scanner.nextInt(); stack.push(value); break; case "pop": try { int res = stack.pop(); System.out.println("���������������" + res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case "exit": scanner.close(); loop = false; break; } } } public void show() { System.out.println("������������"); for (int i = top; i >= 0; i--) { System.out.println("���[" + i + "] = " + stackArray[i]); } } public void showTop() { if (top == -1) { System.out.println("������������top���������������"); return; } System.out.println("���������������" + stackArray[top]); }}
���������������
������������������������������������������������������������������������������������������������������������������������������������������������
public class LinkedListStack { private static Noded header = new Noded(0); private static Noded twinNode = header; public void push(Noded node) { twinNode.next = node; node.pre = twinNode; twinNode = node; } public void pop() { if (twinNode == header) { System.out.println("������������������������"); return; } twinNode = twinNode.pre; } public void show() { Noded tempTwin = twinNode; Noded tempHeader = header.next; while (tempHeader != tempTwin.next) { System.out.println(tempHeader); tempHeader = tempHeader.next; } System.out.println("������������"); } public static void main(String[] args) { LinkedListStack linkedListStack = new LinkedListStack(); linkedListStack.push(new Noded(1)); linkedListStack.show(); linkedListStack.push(new Noded(3)); linkedListStack.show(); linkedListStack.push(new Noded(2)); linkedListStack.show(); linkedListStack.pop(); linkedListStack.show(); linkedListStack.pop(); linkedListStack.show(); linkedListStack.pop(); linkedListStack.show(); linkedListStack.pop(); linkedListStack.show(); } static class Noded { public int data; public Noded next; public Noded pre; public Noded(int value) { this.data = value; } @Override public String toString() { return "Noded{" + "data=" + data + '}'; } }}
������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
