Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用动态数组和重复加倍实现堆栈_C_Data Structures_Dynamic_Stack_Allocation - Fatal编程技术网

C 使用动态数组和重复加倍实现堆栈

C 使用动态数组和重复加倍实现堆栈,c,data-structures,dynamic,stack,allocation,C,Data Structures,Dynamic,Stack,Allocation,我的代码给出了输出10 30 但它应该给出的是5 10。 请找个人帮忙。 我是数据结构新手,需要一些帮助。 我认为分配有问题。 请有人引导我。 我的代码给出了输出10 30 但它应该给出的是5 10。 请找个人帮忙。 我是数据结构新手,需要一些帮助。 我认为分配有问题。 请有人引导我。 我是数据结构新手,需要一些帮助。 我认为分配有问题。 请有人引导我 #include<stdio.h> #include<stdlib.h> struct DynArrayStac

我的代码给出了输出10 30 但它应该给出的是5 10。 请找个人帮忙。 我是数据结构新手,需要一些帮助。 我认为分配有问题。 请有人引导我。 我的代码给出了输出10 30 但它应该给出的是5 10。 请找个人帮忙。 我是数据结构新手,需要一些帮助。 我认为分配有问题。 请有人引导我。 我是数据结构新手,需要一些帮助。 我认为分配有问题。 请有人引导我

#include<stdio.h>
#include<stdlib.h>
    struct DynArrayStack{
        int top;
        int capacity;
        int *array;
    };
struct DynArrayStack *CreateStack(){
    struct DynArrayStack *S=malloc(sizeof(struct DynArrayStack));
    if(!S){
        return NULL;
    }
        S->capacity=1;
        S->top=-1;
        S->array=malloc(S->capacity*sizeof(int));
    if(!S->array){
        return NULL;
    }
    return S;
}
int isEmptyStack(struct DynArrayStack *S){
    return(S->top==-1);
}
int isFullStack(struct DynArrayStack *S){
    return(S->top==S->capacity-1);
}
void DoubleStack(struct DynArrayStack *S){
    S->capacity*=2;
    S->array=realloc(S->array,S->capacity*sizeof(int));
}
void pushStack(struct DynArrayStack *S, int data){
    if(isFullStack(S)){
        DoubleStack(S);
    }
    else{
        S->array[++S->top]=data;
    }
}
int popStack(struct DynArrayStack *S){
    if(isEmptyStack(S)){
        printf("Stack Underflow.\n");
        return 0;
    }
    else{
        return(S->array[S->top--]);

    }
}
int Top(struct DynArrayStack *S){
    if(isEmptyStack(S)){
        return 0;
    }
    return S->array[S->top];
}
void deleteStack(struct DynArrayStack *S){
    if(S){
        if(S->array){
            free(S->array);
        }
        free(S);
    } 
}
void insertBottom(struct DynArrayStack *S, int data){
    int temp;
    if(isEmptyStack(S)){
        pushStack(S, data);
        return;
    }
    temp=popStack(S);
    insertBottom(S, data);
    pushStack(S, temp);
}
void reverseStack(struct DynArrayStack *S){
    int data;
    if(isEmptyStack(S)){
        return;
    }
    data=popStack(S);
    reverseStack(S);
    insertBottom(S, data);
}
int main(){
    struct DynArrayStack *Stack=CreateStack();
    pushStack(Stack, 30);
    pushStack(Stack, 20);
    pushStack(Stack, 10);
    pushStack(Stack, 05);
    printf("%d\n", Top(Stack));
    printf("%d\n", popStack(Stack));
    printf("%d\n", Top(Stack));
    /*reverseStack(Stack);
    printf("%d\n", Top(Stack));*/
}
#包括
#包括
结构动态堆栈{
int top;
国际能力;
int*数组;
};
结构DynArrayStack*CreateStack(){
struct DynArrayStack*S=malloc(sizeof(struct DynArrayStack));
如果(!S){
返回NULL;
}
S->capacity=1;
S->top=-1;
S->array=malloc(S->capacity*sizeof(int));
如果(!S->数组){
返回NULL;
}
返回S;
}
int isEmptyStack(结构DynArrayStack*S){
返回(S->top==-1);
}
int isFullStack(结构DynArrayStack*S){
返回(S->top==S->capacity-1);
}
void双堆栈(结构DynArrayStack*S){
S->容量*=2;
S->array=realloc(S->array,S->capacity*sizeof(int));
}
void pushStack(结构DynArrayStack*S,int数据){
如果(完整堆栈){
双栈;
}
否则{
S->array[++S->top]=数据;
}
}
int-popStack(结构DynArrayStack*S){
如果(isEmptyStack){
printf(“堆栈下溢。\n”);
返回0;
}
否则{
返回->数组->顶部--];
}
}
int Top(结构动态堆栈*S){
如果(isEmptyStack){
返回0;
}
返回S->array[S->top];
}
void deleteStack(结构DynArrayStack*S){
若有(S){
如果(S->数组){
自由(S->数组);
}
免费的;
} 
}
void insertBottom(结构DynArrayStack*S,int数据){
内部温度;
如果(isEmptyStack){
推堆栈,数据;
返回;
}
temp=popStack(多个);
插入底部(S,数据);
推压堆栈(S,温度);
}
无效反向堆栈(结构动态堆栈*S){
int数据;
如果(isEmptyStack){
返回;
}
数据=流行堆栈;
倒装车厢(S);;
插入底部(S,数据);
}
int main(){
struct DynArrayStack*Stack=CreateStack();
推压堆栈(堆栈,30);
推压堆栈(堆栈,20);
推压堆栈(堆栈,10);
推式堆栈(堆栈,05);
printf(“%d\n”,顶部(堆栈));
printf(“%d\n”,popStack(Stack));
printf(“%d\n”,顶部(堆栈));
/*反向堆栈(堆栈);
printf(“%d\n”,顶部(堆栈))*/
}

这是什么语言?C,C++,还是其他变体?请编辑问题并添加适当的标签,以便人们可以找到它。还有,请帮忙。我哪儿也去不了。当我写容量=8时。然后给出正确答案。因此,我认为在重新分配中存在问题。您能为这个问题添加一些更重要的标记吗?我认为您在
pushStack
中有一个bug。如果
isFullStack
为true,则不会实际推送新值。推送不应在else块中完成。请不要只是复制粘贴文本多次。你必须写更多的文字是有原因的。你需要适当地解释情况,应该发生什么,发生了什么,代码甚至做了什么等等。请阅读更多信息。