试图在C中生成堆栈ADT。无法从中获得所需的输出

试图在C中生成堆栈ADT。无法从中获得所需的输出,c,stack,adt,C,Stack,Adt,因此,我们被要求为C制作自己的堆栈ADT,用于存储双精度浮点数。我制作了一个非常基本的ADT,它有基本的堆栈函数isEmpty、push、pop。无论如何,我决定主要测试我的ADT,创建3个变量,并将它们推到堆栈上。然后我想看看顶层元素是什么,但是我得到的输出非常奇怪 这是我收到的所有输出: (lldb) 顶部应该指向变量结果,并打印该值,但以上就是我得到的全部内容 #include <stdio.h> #include <stdlib.h> // create a

因此,我们被要求为C制作自己的堆栈ADT,用于存储双精度浮点数。我制作了一个非常基本的ADT,它有基本的堆栈函数isEmpty、push、pop。无论如何,我决定主要测试我的ADT,创建3个变量,并将它们推到堆栈上。然后我想看看顶层元素是什么,但是我得到的输出非常奇怪

这是我收到的所有输出:

(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
}

mallocsizeofnew_Stack->stackCapacity;你少了一个双倍的。数组的初始分配大小正好是一个int的大小。因为我看不到任何扩展算法的证据,即使你解决了这个问题,任何推送多个元素的尝试也不会起作用。例如:mallocnew_Stack->stackCapacity*sizeofdouble它不会给你带来实际问题,但是,请考虑顶部和NoFoObjes字段看起来是多余的。最好只使用一个,这样既可以减少所需的记账量,又可以避免出现内部不一致的可能性。