简体   繁体   English

具有静态数组C的动态堆栈

[英]dynamic stack with static array C

I have been doing excercises from a book. 我一直在练习一本书。 And I am stuck on the meaning of this qustion. 我坚持这个问题的意思。 Assuming that you store integer values on the stac and that using a static array to store data provide a createStack() deleteStack(stack) methods. 假设您在stac上存储整数值,并使用静态数组存储数据,请提供createStack()deleteStack(stack)方法。

My interpretation is 我的解释是

typedef struct {
  int values;
  char data[50];

} StackData;

typedef struct n {
  StackData d; // store some data in node
  struct n *successor; // store successor of node
  // as typedef is not yet completed
  // name StackNode cannot be used
} SatckNode;


typedef struct {
  StackNode *head;
  StackNode *current;
} Stacklist;

I know these arent the methods. 我知道这些方法。 But i want to know if I am going about it the right way 但是我想知道我是否正在正确地做这件事

I think you're on the right way - just a couple of comments. 我认为您的方法正确-仅有几点评论。

In Stacklist , I don't get why you have pointers to two of the nodes in the stack. Stacklist ,我不明白为什么您有指向堆栈中两个节点的指针。 Usually, stacks only keep a reference to the item on the top of the stack. 通常,堆栈仅在堆栈顶部保留对该项目的引用。 In addition, they either keep a counter of how big is the stack, or a pointer to the node on the bottom of the stack (which is what you probably mean by head , and reference the head node by current ?). 另外,它们要么保留一个堆栈大小的计数器,要么指向堆栈底部的节点的指针(这可能是head意思,并通过current引用head节点)。

And don't forget to initialize everything whenever you create any of those structures :P usually ends up in endless hours of headache. 而且,无论何时创建任何这些结构,都不要忘记初始化所有内容:P通常会陷入无尽的头痛之中。

Keep up the good work. 保持良好的工作。

If you're using a static array for the values, then you don't technically need createStack() and deleteStack() functions, because you can just create a struct stack or whatever on the stack (pun intended) and you're done. 如果您使用静态数组作为值,那么从技术上讲,您不需要createStack()deleteStack()函数,因为您可以只创建一个struct stack或堆栈上的任何内容(双关语)即可。

If you do want to, though, (and you might legitimately want to, eg to avoid having to explicitly initialize top , or to hide the implementation behind an opaque type, or to be able to return one from a function without copying a potentially large array) this'll do it: 但是,如果您确实想这样做(并且您可能合法地希望避免例如必须显式初始化top ,或者将实现隐藏在不透明类型的后面,或者能够从函数中返回一个而不复制可能的大对象数组)即可:

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

#define STACKSIZE 50

typedef struct stack * Stack;

struct stack {
    size_t top;
    int values[STACKSIZE];
};

Stack createStack(void)
{
    Stack new_stack = malloc(sizeof *new_stack);
    if ( !new_stack ) {
        perror("couldn't allocate memory");
        exit(EXIT_FAILURE);
    }
    new_stack->top = 0;
    return new_stack;
}

void deleteStack(Stack stack)
{
    free(stack);
}

void push(Stack stack, const int n)
{
    if ( stack->top < STACKSIZE ) {
        stack->values[stack->top++] = n;
    }
    else {
        fprintf(stderr, "Stack full - exiting.\n");
        exit(EXIT_FAILURE);
    }
}

int pop(Stack stack)
{
    if ( stack->top > 0 ) {
        return stack->values[--stack->top];
    }
    else {
        fprintf(stderr, "Stack empty - exiting.\n");
        exit(EXIT_FAILURE);
    }
}

int main(void)
{
    Stack mystack = createStack();

    push(mystack, 3);
    push(mystack, 1);
    push(mystack, 4);
    push(mystack, 1);
    push(mystack, 5);

    for ( size_t i = 0; i < 5; ++i ) {
        printf("Popped %d from stack.\n", pop(mystack));
    }

    deleteStack(mystack);

    return 0;
}

Right now you seem to want a stack with values in a static array, but then you start defining structs for nodes and lists, as if you want a linked list implementation. 现在,您似乎想要一个带有静态数组中的值的堆栈,但是随后您开始为节点和列表定义结构,就好像您想要一个链表实现一样。 The two implementations are obviously pretty different. 两种实现方式明显不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM