English 中文(简体)
利用一个阵列来执行三个圣克
原标题:Use an array to implement three Stack
  • 时间:2010-11-21 17:50:32
  •  标签:
  • java

我看到以下的《 Java法》用于在单一阵列上三个阵列。

1 int stackSize = 300;
2 int indexUsed = 0;
3 int[] stackPointer = {-1,-1,-1};
4 StackNode[] buffer = new StackNode[stackSize * 3];
5 void push(int stackNum, int value) {
6   int lastIndex = stackPointer[stackNum];
7   stackPointer[stackNum] = indexUsed;
8   indexUsed++;
9   buffer[stackPointer[stackNum]]=new StackNode(lastIndex,value);
10 }
11 int pop(int stackNum) {
12  int value = buffer[stackPointer[stackNum]].value;
13  int lastIndex = stackPointer[stackNum];
14  stackPointer[stackNum] = buffer[stackPointer[stackNum]].previous;
15  buffer[lastIndex] = null;
16  indexUsed--;
17  return value;
18 }
19 int peek(int stack) { return buffer[stackPointer[stack]].value; }
20 boolean isEmpty(int stackNum) { return stackPointer[stackNum] == -1; }
21
22 class StackNode {
23  public int previous;
24  public int value;
25  public StackNode(int p, int v){
26   value = v;
27   previous = p;
28  }
29 }

我的问题1是,第4行为变数的缓冲分配了记忆。 为什么在第9行,我们仍然需要分配新的StackNode。

我的问题2是:这项职能能否帮助收回旧的记忆?

例如,

Stack1_E1 => Stack1_E2 => Stack2_E1 => Stack2_E2 => Stack3_E1

When we call pop(0) // pop the Stack1 Based on my understanding, the free space used by Stack1_E2 will not be reused next time when we call push.

Is the function pop designed correctly? Thank you

注: 这个问题已经修改,包括民俗功能。

问题回答

问题1:第4行为变数缓冲分配了记忆。 为什么在第9行,我们仍然需要分配新的StackNode。

第4行设置了一系列<>参照>至Stack Node。 实际<代码>StackNode 然后按行文制造物体 9.9

Question 2: Can the function pop help recollect the used memory?

<代码>pop功能从中分到下一个价值物体(StackNode. Value,并相应在“代码>>>>中注明。 <代码>Stack Node使用的记忆将收集垃圾,因为StackNode不再提及。 当物体不再使用时(即不再由电传者或其他物体提及)将收集价值物体本身的记忆。

你似乎熟悉C++。 在 Java,宣布一个可变的初始空间;“新”建筑仅需要称作建筑商。 (这就是为什么没有“三角”。)

因此,第4行的通话创造了一系列的斯塔克诺德物体形空间,第9行则产生了实际物体。

//Array to implement 3 Stacks

#include<stdio.h>
#include<stdlib.h>
#define size 13

struct Stack{
    int arr[size];
    int top;
}st[3];


void push(int item, int sno){
    if(isFull(sno)){
        printf("Overflow....!!
");

    }
    else{
       st[sno].top++;

        st[sno].arr[st[sno].top]=item;
      }
}

int isFull(int sno){
    if(sno==1&&st[sno].top==(size/3)-1){
        return 1;
    }
    else if(sno==2&& st[sno].top==(size/3)-1){
        return 1;
    }
    else if(sno==3 && st[sno].top==(size-1 -2*(size/3))){
        return 1;
    }
    else{
        return 0;
    }
}

int isEmpty(int sno){
    if(st[sno].top==-1){
        return 1;
    }
    else{
        return 0;
    }
}


int pop(int sno){
int item;
  if(isEmpty(sno)){
    printf("Underflow....!!
");
  }
  else{

    item = st[sno].arr[st[sno].top];
    st[sno].top--;
    return item;
}
}

void display(int sno){
    int i;

    for(i=st[sno].top;i>=0;i--){
        printf("%d->",st[sno].arr[i]);
    }
}

int main(){

    st[1].top=-1;
    st[2].top=-1;
    st[3].top=-1;

    push(10,1);
    push(20,1);
    push(30,1);
    push(40,1);

    display(1);

    printf("
");
    push(50,2);
    display(2);
    printf("
");
    pop(2);
    pop(2);
    //display(2);

    push(60,3);
    push(70,3);
    push(80,3);
    push(90,3);
    push(100,3);
    printf("
");
    display(3);
    push(110,3);





    return 0;
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签