gpt4 book ai didi

c - 尝试用 C 语言创建堆栈 ADT。无法从中获得我想要的输出

转载 作者:行者123 更新时间:2023-11-30 17:26:25 24 4
gpt4 key购买 nike

因此,我们被要求为 C 语言制作自己的堆栈 ADT,用于存储 double float 。我用基本的堆栈函数(isEmpty、push、pop)制作了一个非常基本的 ADT。不管怎样,我决定通过创建 3 个变量并将它们压入堆栈来主要测试我的 ADT。然后我想看看顶部元素是什么,但我得到的输出非常奇怪。

这是我收到的所有输出:

(lldb) 

顶部应该指向变量结果,并打印该值,但以上就是我得到的全部内容。

#include <stdio.h>
#include <stdlib.h>

// create a new structure for stack ADT.

struct double_stack{
double * array;
int stackCapacity;
int numOfObjects;
int top;
};

//creating a new empty stack.

struct double_stack *newStack(){

struct double_stack* new_Stack= malloc(sizeof(struct double_stack));

//stack has a capactiy to store one element as default, and has 0 number of objects.

new_Stack->stackCapacity = 1;
new_Stack->numOfObjects = 0;
new_Stack->array = malloc(sizeof(new_Stack->stackCapacity));

//top points to -1 to show the stack is empty. When its not empty,it will point to 0,so an element can be placed at that index etc.

new_Stack->top = -1;

return new_Stack;
}


//check to see if stack is empty. Returns 1 if true. 0 if false.
int isEmptyStack(struct double_stack *this){

//if the attribute pointed in the condition below is true,then stack is empty.
if(this->numOfObjects==0 && this->top==-1){
return 1;
}
else{
return 0;
}

}

//push an element onto the stack.

void push(struct double_stack * this, double element){

this->stackCapacity++; //stack capacity is increased by 1 to make space for next element to be pushed on.

this->numOfObjects++; //number of elements increased by 1.

this->array[++this->top]=element; // the prefix ++ operator increments the top index before it is used as an index in the array (i.e., where to place the new element).

}

//this method pops an element off the stack. If stack is empty,the exit command quits the function. It returns the element to be popped ,because usually we need to perform some operation on the element.

double pop(struct double_stack*this){
if(isEmptyStack(this)){
printf("%s","Error:Cannot pop element from empty stack!");
return -1;
}

return this->array[this->top--];
}


int main() {
struct double_stack * s = newStack();
double a = 5;
double b = 10;
double result=a+b;

push(s,a);
push(s,b);
push(s,result);

printf("%d",s->top);
}

最佳答案

首先,你不能动态增加堆栈的容量,如果你想这样做,你需要使用 realloc。堆栈的容量应该是预定义的,否则你需要使用链表而不是使用链表大批。这是您的工作代码。

#include <stdio.h>
#include <stdlib.h>

// create a new structure for stack ADT.

struct double_stack{
double * array;
int stackCapacity;
int numOfObjects;
int top;
};

//creating a new empty stack.

struct double_stack *newStack(int size){

struct double_stack* new_Stack= malloc(sizeof(struct double_stack));

//stack has a capactiy to store one element as default, and has 0 number of objects.

new_Stack->stackCapacity = size;//this is better way
new_Stack->numOfObjects = 0;
new_Stack->array = malloc(sizeof(double)*new_Stack->stackCapacity);//size of the //array must be declared here that is the capacity of the stack

//top points to -1 to show the stack is empty. When its not empty,it will point //to 0,so an element can be placed at that index etc.

new_Stack->top = -1;

return new_Stack;
}


//check to see if stack is empty. Returns 1 if true. 0 if false.
int isEmptyStack(struct double_stack *this){

//if the attribute pointed in the condition below is true,then stack is empty.
if(this->numOfObjects==0 && this->top==-1){
return 1;
}
else{
return 0;
}

}

//push an element onto the stack.

void push(struct double_stack * this, double element){

//this->stackCapacity++; //you cant increase the size of the array
//put a check here if the stack is full
if(this->top==(this->stackCapacity-1))
printf("Stack is full\n");
else{
this->numOfObjects++; //number of elements increased by 1.

this->array[++this->top]=element; // the prefix ++ operator increments the top index before it is used as an index in the array (i.e., where to place the new element).
}
}

//this method pops an element off the stack. If stack is empty,the exit command quits the function. It returns the element to be popped ,because usually we need to perform some operation on the element.

double pop(struct double_stack*this){
if(isEmptyStack(this)){
printf("%s","Error:Cannot pop element from empty stack!");
return -1;
}

return this->array[this->top--];
}


int main() {
struct double_stack * s = newStack(5);
double a = 5;
double b = 10;
double result=a+b;

push(s,a);
push(s,b);
push(s,result);

printf("%f\n",s->array[s->top]);//this will print the last element on the stack
}

关于c - 尝试用 C 语言创建堆栈 ADT。无法从中获得我想要的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26806149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com