因此我们被要求为存储双精度浮点数的C制作我们自己的堆栈ADT。我用基本的堆栈函数(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);
}
答案 0 :(得分:0)
首先,你不能动态增加堆栈的容量,如果你想这样做,你需要使用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
}