部分代码已省。对C指针的知识不会,希望哪位高手帮帮忙,感激!
typedef struct{
   char *top;
   char *base;
}Vnstack;
Vnstack s;//分析栈void Initstack()//初始化分析栈
{
  s.base=(char*)malloc(20*sizeof(char));
  if(!s.base) exit(1);//
  s.top=s.base ;
  *s.top='#';
  *(++s.top)='E';
}
void Push(char c)
{
  *(++s.top)=c;
  
 }
void Pop()
{
  s.top--;
}
char Gettop()
{
char c;
c=*s.top; 
    return c;
 }
void display()
{
char string[20]={'i','+','i','+','i'};
  int i;
 printf("%d\t",++NUM);
     for(i=0;(s.base+i)<=s.top;i++)
 {printf("%c",*(s.base+i));}
         printf("\t");
      for(i=0;string[i]!='#';i++)
 {printf("%c",string[i]);}
}void main()
{
display();
}

解决方案 »

  1.   


    public class MyStack{
      private int top; 
      private char[] base; 
      private int maxLength = 20;  public void MyStack(){
        base = new char[maxLength];
        top = 0;     
      }  public void push(char c){
        if(top < maxLength){
          base[top++] = c;
        }else{
          char[] newStack = new char[maxLength*2];
          for(int i = 0 ; i < maxLength; i++){
            newStack[i] = base[i];
          }
          base = newStack;
          maxLength *= 2;
          base[top++] = c;
        }    
      }  public char pop(int index){
        if(index < top && index >= 0){
          return base[--top];
        }else{
          return '-1';
        } 
      }  public void display(){
        for(int i = 0; i < top; i++){
          System.out.println(base[i]);
        }
      }  public static void main(String[] args){
        MyStack stack = new MyStack();
        stack.push('a');
        stack.push('b');
        stack.push('c');
        stack.display();
      } 
    }
      

  2.   

    // 这是一个栈结构.. 内部实现可以用 List 也可以用数组... 楼上用的数组.. 我就用list吧..~..
    package stack;import java.util.ArrayList;
    import java.util.List;public class Vnstack { private List<Character> list = new ArrayList<Character>(); private int top = -1; public void push(Character ch) {
    if (ch != null) {
    top++;
    list.add(ch);
    }
    } public Character peek() {
    return list.get(top--);
    } public void pop() {
    top--;
    } public void disPlay() {
    for (int i = 0; i < top; i++) {
    System.out.print("{" + list.get(i) + "}");
    }
    System.out.println();
    }}
      

  3.   

    还是比较认同一楼的观点,java不会还是要学的,直接把代码给你对你没什么用!
      

  4.   

    C的涵数和Java方法其实没多大区别!