Stack in Pop不会弹出?

时间:2017-06-23 20:06:23

标签: c stack

我的流行音乐没有弹出老师提供此代码的骨架,我把printf语句和推送似乎正在工作它似乎停止当它到达单词pop。我知道我可能没有释放正确的记忆,所以我想我以后会解决这个问题(或许这就是问题?)char **真的把我扔掉了。 这与此问题Conflicting types for enum bool?相同 我猜我班上的人越来越多了。

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

typedef struct{
char** data;    /* Array of strings representing the stack */
int top;        /* Index of the top of the stack.  top is -1 if the stack is 
empty. */
int size;       /* Number of elements that the stack can currently hold */
} Stack;

typedef enum { FALSE, TRUE } bool;

/* function prototypes */
Stack* create();
void deleteStack( Stack* ps );
void push( Stack* ps, char* str );
char* pop( Stack* s);
bool empty( Stack *s );
bool full( Stack *s );

int main(int argc, char *argv[])
{
FILE *in_file = fopen("data_a2.txt", "r");
Stack *s;

printf("CS 2123 Assignment 2\n");

if (in_file == NULL)
{
    printf("File %s not found.\n", "data_a2.txt");
    return -1;
}
    char strBuffer[256];

    s = create();

    while(fscanf(in_file, "%s", strBuffer)!=EOF) {
        printf("%s\n", strBuffer);

        if(strcmp(strBuffer, "pop") == 0) {
            pop(s);
            } else {
                push(s, strBuffer);
            }
        }

   // free(s);



/* Uncomment next line once you get create working */
   // deleteStack( &s );
fclose(in_file);

}

/* create: returns a new empty stack. */
Stack* create(){
    Stack *s;
s = (Stack*)malloc(sizeof(Stack));
s->top = -1;
s->size = 10;
s->data = (char**)malloc(s->size * sizeof(char*));
return s;
}

/* deleteStack: deletes the memory associated with the given stack. */
void deleteStack( Stack* ps ){
while( ps->top>=0 ){
    free( ps->data[ps->top] );
    ps->top--;

}

free( ps->data );
}

/*
 *  push: takes a string parameter which is the value it pushes onto the 
 stack.
 *  It may also need to call realloc to expand the size of the stack before 
completing the push.
*/
void push( Stack* s, char* str ){
   char *newStr;
   newStr = (char*)malloc(256);
   strcpy(str, newStr);
if(full(s)){
    s->size = 10;
    s->data = (char**)realloc(s->data, s->size);
}
s->data[++s->top] = str;
free(newStr);
free(s->data);
//make new str malloc strcpy buffer
//realloc memory here s.data
}

/* pop: returns the string that was removed from the stack. */
char* pop( Stack* s){
    if(empty(s)) {
        return NULL;
    }
printf("# of elements after popping: %d, string popped: %s\n",s->top + 1, s-
>data[s->top]);
return s->data[s->top--];
}

/* empty: returns TRUE if the stack has no elements, otherwise FALSE. */
bool empty( Stack *s ){
if(s->top == -1) {
    return TRUE;
} 
return FALSE;
}

/* full returns TRUE if the stack does not have any room left, otherwise 
FALSE. */
bool full( Stack *s ){
    if (s->top == s->size - 1) {
        return TRUE;
}
return FALSE;
}

3 个答案:

答案 0 :(得分:0)

我认为你的练习比现在更复杂。

你的推动:

push

它比询问的要多得多。 main()应接受来电者定义的任何字符串(您的Stack* create() { Stack* stack = malloc(sizeof(Stack)); if (!stack) return 0; stack->size = 128; // or any size you like... stack->top = -1; stack->data = malloc(stack->size * sizeof(char*)); if (!stack->data) { free(stack); return 0; } return stack; } bool push( Stack* ps, char* str ) // I would consider returning an error code { char* str2; char** tmp; str2 = strdup(str); // caller of pop() should call free() if (!str2) return FALSE; // error condition if (full(ps)) { tmp = (char**)realloc(ps->data, (ps->size + 10) * sizeof(char*)); if (!tmp) return FALSE; // this is an error condition. // use return value to inform the caller ps->size += 10; ps->data = tmp; } ps->data[++(ps->top)] = str2; return TRUE; }

比较:

val array = Array(5, { i -> i })

答案 1 :(得分:0)

我只看了push

问题:

  • 可怕的缩进。
  • 没有理由相信256对你的字符串来说已经足够了。使用strlen(str)+1
  • strcpy(str, newStr)应为strcpy(newStr, str)
  • strdup更方便。
  • s->size = 10;应为s->size += 10;
  • 重新分配时,会传递指针数而不是它们所采用的大小。
  • 投下realloc的结果毫无意义。 (同样适用于malloc。)
  • 您已将str而不是newStr添加到堆栈。
  • 不要释放你添加到堆栈中的字符串!
  • 不要释放堆叠!
  • 不检查内存分配错误。 (这不会在下面修复。)

修正:

void push( Stack* s, const char* str ) {
    if (full(s)) {
        s->size += 10;
        s->data = realloc(s->data, s->size * sizeof(char*));
    }

    s->data[++(s->top)] = strdup(str);
}

清理了剩余的堆栈代码(没有解释):

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

typedef struct {
    char** data;  /* Array of strings representing the stack */
    int size;     /* Number of elements that the stack can currently hold */
    int top;      /* Index of the top of the stack (-1 if empty). */
} Stack;

Stack* Stack_new();
void   Stack_delete( Stack* ps );
void   Stack_push( Stack* ps, const char* str );
char*  Stack_pop( Stack* s );
int    Stack_empty( const Stack* s );
int    Stack_full( const Stack* s );

int main(int argc, char *argv[]) {
    Stack *s = Stack_new();

    ...
    Stack_push(s, str);
    ...

    ...
    char* str = Stack_pop(s);
    free(str);
    ...

    Stack_delete(s);
    return 0;
}

/* Create a new empty stack. */
Stack* Stack_new() {
    Stack* s = malloc(sizeof(Stack));
    s->top = -1;
    s->size = 10;
    s->data = malloc(s->size * sizeof(char*));
    return s;
}

/* Deletes the memory associated with the given stack. */
void Stack_delete( Stack* s ) {
    while ( s->top >= 0 ){
        free( s->data[(s->top)--] );
    }

    free( s->data );
    free( s );
}

/* Pushes a copy of the provided NUL-terminated string unto the stack. */
void Stack_push( Stack* s, const char* str ){
    if (full(s)) {
        s->size += 10;
        s->data = realloc(s->data, s->size * sizeof(char*));
    }

    s->data[++(s->top)] = strdup(str);
}

/* Returns the string that was removed from the stack. */
/* The returned string needs to be freed. */
/* Returns NULL if the stack is empty. */
char* Stack_pop( Stack* s ) {
    if (empty(s)) {
        return NULL;
    }

    return s->data[(s->top)--];
}

/* Returns true if the stack is empty. */
int Stack_empty( const Stack* s ) {
    return s->top == -1;
}

/* Returns true if the stack is full. */
int Stack_full( const Stack* s ) {
    return s->top == s->size - 1;
}

请注意,我没有添加内存分配错误检查。

答案 2 :(得分:-1)

哇,这段代码有很多问题,我不知道从哪里开始。

push方法对我没有意义,你传递一个字符串(可能指向一个只读位置),你保留(而不是任意)256字节的内存,将这个新的未初始化的数据复制到src然后再次释放记忆......

您正在使用mallocrealloc非常糟糕

int size = ???;
int *array = malloc(size * sizeof *array);

int new_size = ???;
int *tmp = realloc(array, new_size * sizeof *array);
if(tmp == NULL)
    // no more memory, array is still alive
    // return

array = tmp;

我不喜欢堆栈的整体设计,你应该设计数据结构,以便在不必查看代码的情况下清楚地了解你正在做什么。给我几分钟,我会发布类似的东西。

编辑:

我撤回了我的例子,这是非常糟糕的设计。