(java版)
1. 顺序栈的实现
顺序栈实现1
顺序栈实现2
- package lang;
-
- import java.util.ArrayList;
- import java.util.EmptyStackException;
-
- public class ArrayStack2<E> extends ArrayList<E> {
-
-
- private static final long serialVersionUID = -4396620094929287983L;
-
- public ArrayStack2() {
- super();
- }
-
- public ArrayStack2(final int initialSize) {
- super(initialSize);
- }
-
- public boolean empty() {
- return isEmpty();
- }
-
- public E peek() throws EmptyStackException {
- final int n = size();
- if (n <= 0) {
- throw new EmptyStackException();
- } else {
- return get(n - 1);
- }
- }
-
- public E peek(final int n) throws EmptyStackException {
- final int m = (size() - n) - 1;
- if (m < 0) {
- throw new EmptyStackException();
- } else {
- return get(m);
- }
- }
-
- public E pop() throws EmptyStackException {
- final int n = size();
- if (n <= 0) {
- throw new EmptyStackException();
- } else {
- return remove(n - 1);
- }
- }
-
- public E push(final E item) {
- add(item);
- return item;
- }
-
- public int search(final Object object) {
- int i = size() - 1;
- int n = 1;
- while (i >= 0) {
- final Object current = get(i);
- if ((object == null && current == null)
- || (object != null && object.equals(current))) {
- return n;
- }
- i--;
- n++;
- }
- return -1;
- }
- }
2. 链式栈的实现:
3. 栈的应用
3.1 将10进制正整数num转换为n进制
- package stack.apply;
-
- import org.junit.Test;
-
- import lang.ArrayStack;
-
- public class Conversion {
-
-
- private String conversion(int num, int n) {
- ArrayStack<Integer> myStack = new ArrayStack<Integer>();
- Integer result = num;
- while (true) {
-
- myStack.push(result % n);
- result = result / n;
- if (result == 0) {
- break;
- }
- }
- StringBuilder sb = new StringBuilder();
-
- while ((result = myStack.pop()) != null) {
- sb.append(result);
- }
- return sb.toString();
- }
-
- @Test
- public void testConversion(){
- String s = conversion(13,2);
- System.out.println(s);
- }
- }
3.2 行编辑:
输入行中字符‘#‘表示退格, ‘@‘表示之前的输入全都无效.
- package stack.apply;
-
- import org.junit.Test;
-
- import lang.ArrayStack;
-
- public class LineEdit {
-
- private String lineEdit(String input) {
- ArrayStack<Character> myStack = new ArrayStack<Character>();
- char[] arr = input.toCharArray();
- for (char c : arr) {
- if (c == ‘#‘) {
- myStack.pop();
- } else if (c == ‘@‘) {
- myStack.clear();
- } else {
- myStack.push(c);
- }
- }
-
- StringBuilder sb = new StringBuilder();
- Character temp = null;
- while ((temp = myStack.pop()) != null) {
- sb.append(temp);
- }
-
- sb.reverse();
- return sb.toString();
- }
-
- @Test
- public void testLineEdit(){
- String s = lineEdit("abcd#dsa@#usera#22#8");
- System.out.println(s);
- }
- }
3.3 检验符号是否匹配.
‘[‘和‘]‘, ‘(‘和‘)‘成对出现时字符串合法.
例如"[][]()", "[[([]([])()[])]]"是合法的; "([(])", "[())"是不合法的
- package stack.apply;
-
- import org.junit.Test;
-
- import lang.ArrayStack;
-
- public class Match {
-
- public boolean isMatch(String str) {
- ArrayStack<Character> myStack = new ArrayStack<Character>();
- char[] arr = str.toCharArray();
- for (char c : arr) {
- Character temp = myStack.pop();
-
- if (temp == null) {
- myStack.push(c);
- }
-
- else if (temp == ‘[‘ && c == ‘]‘) {
- }
-
- else if (temp == ‘(‘ && c == ‘)‘) {
- }
-
- else {
- myStack.push(temp);
- myStack.push(c);
- }
- }
- return myStack.isEmpty();
- }
-
- @Test
- public void testMatch(){
- boolean b = isMatch("[[([]([])()[])]]");
- System.out.println(b);
- }
- }
-
- (c版)
-
判断一行字符串输入"各种括号"是否是合法的-----------------------栈用数组实现
如:[()]是合法的(balance)
[(])是不合法的(imbalance)
测试:
- shang@shang:~/C$ ./a.out
- Please input the symbol that you want to balance:
- {[]}
- The symbol that you input is balance!
- shang@shang:~/C$ ./a.out
- Please input the symbol that you want to balance:
- [(])
- The symbol that you input is imbalance!
- shang@shang:~/C$ ./a.out
- Please input the symbol that you want to balance:
- ([{}])
- The symbol that you input is balance!
- shang@shang:~/C$
- 用栈计算逆波兰式
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- typedef struct Mystack *Stack;
-
- struct Mystack {
- int Capacity;
- int Top_of_stack;
- int *Array;
- };
-
- Stack CreateStack(int Max)
- {
- Stack S;
- S = malloc(sizeof(struct Mystack));
- if (S == NULL)
- printf("Create stack error!\n");
-
- S->Array = malloc(sizeof(char) * Max);
- if (S->Array == NULL)
- printf("Create stack error!\n");
-
- S->Capacity = Max;
- S->Top_of_stack = 0;
- return S;
- }
-
- void DisposeStack(Stack S)
- {
- if (S != NULL)
- {
- free(S->Array);
- free(S);
- }
- }
-
- int IsEmpty(Stack S)
- {
- return !S->Top_of_stack;
- }
-
- int IsFull(Stack S)
- {
- if (S->Top_of_stack == S->Capacity - 1)
- return 1;
- else
- return 0;
- }
-
-
- int Push(int x, Stack S)
- {
- if (IsFull(S))
- printf("The Stack is full!\n");
- else
- S->Array[S->Top_of_stack++] = x;
- }
-
- int Pop(Stack S)
- {
- if (IsEmpty(S))
- printf("The Stack is empty!\n");
- else
- S->Top_of_stack--;
- }
-
- int Top(Stack S)
- {
- if (!IsEmpty(S))
- return S->Array[S->Top_of_stack-1];
- printf("The Stack is empty!\n");
- return 0;
- }
-
- int main()
- {
- int i, len, result;
- char str[100];
- printf("Please input the postfix that you want to compute: \n");
- scanf("%s", str);
- len = strlen(str);
-
- struct Mystack *my_stack = CreateStack(len+1);
- for (i = 0; i < len; i++)
- {
- if (str[i] >= ‘0‘ && str[i] <= ‘9‘)
- Push((int)str[i]-48, my_stack);
- else if (str[i] == ‘+‘)
- {
- int x = Top(my_stack);
- Pop(my_stack);
- int y =Top(my_stack);
- Pop(my_stack);
- Push(x+y, my_stack);
-
- }
- else if (str[i] == ‘-‘)
- {
- int x = Top(my_stack);
- Pop(my_stack);
- int y = Top(my_stack);
- Pop(my_stack);
- Push(x-y, my_stack);
-
- }
-
- else if (str[i] == ‘*‘)
- {
- int x = Top(my_stack);
- Pop(my_stack);
- int y = Top(my_stack);
- Pop(my_stack);
- Push(x*y, my_stack);
-
- }
- else if (str[i] == ‘/‘)
- {
- int x = Top(my_stack);
- Pop(my_stack);
- int y = Top(my_stack);
- Pop(my_stack);
- Push(x/y, my_stack);
-
- }
- }
- printf("The result is: %d\n", Top(my_stack));
- Pop(my_stack);
-
-
- return 0;
-
- }
stack(顺序、链式及应用)
原文:http://www.cnblogs.com/tangtang-123/p/4436761.html