【问题标题】:Unexpected results implementing stacks with linked lists使用链表实现堆栈的意外结果
【发布时间】:2016-01-18 10:28:15
【问题描述】:

我正在编写这个程序,我想了解为什么我得到了错误的打印(应该是 1、2、3、4、5)给出了一些地址。我的stackIsEmpty() 甚至没有像它的意思那样在堆栈为空时停止打印值。这是我的代码:

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

//Stacks using Structures and linked list and pointerws.

typedef struct Stack{
    //Type of the elements of the stack. Here integers
    int elem;
    //To of the stack
    struct Stack* previous;
} Stack;

void initialize(Stack *st);
void push(Stack *st, int element);
int pop(Stack *st);
int stackIsEmpty(Stack *st);

int main(){
    int i;
    Stack st;
    Stack *pt_st = &st;
    initialize(pt_st);

    //Test phase...
    for(i=0; i<5; i++)
        push(pt_st, i+1);

    for(i=0; i<5; i++)
        printf("here is one element: %d\n", pop(pt_st));
    //End of test with error!
    return 0;
}

void initialize(Stack *st){
    st = NULL;
}

void push(Stack *st, int element){
    Stack *sta = (Stack*)malloc(sizeof(Stack));
    if (sta != NULL)
    {
        sta->previous = st;
        sta->elem = element;
        st = sta;
        free(sta);
    }
    else
        exit(0);
}

int pop(Stack *st){
    if(!stackIsEmpty(st))
    {
        printf("stack is empty cannot pop\n");
        return 0;
    }
    else
    {
        int number = st->elem;
        Stack* copy = st->previous;
        free(st);
        st = copy;
        return number;
    }
}

int stackIsEmpty(Stack *st){
    if(st == NULL)
        return 0;
    else
        return 1;
}

【问题讨论】:

  • st = sta; free(sta);??这是什么?
  • 我想我可以将sta 的地址放入st。我将分配的空间释放给变量sta
  • 您是否使用调试器单步执行了代码

标签: c pointers linked-list


【解决方案1】:

您创建的堆栈指针pt_st 是按值传递给函数的,因此函数只修改它的本地副本。

您可以创建指向指针的指针,或将 pt_st 设为全局变量。

你的push函数也有问题。

我已经修改了你的代码,使 pt_st 成为全局的,所以它现在可以工作了:

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

//Stacks using Structures and linked list and pointerws.

typedef struct Stack{
    //Type of the elements of the stack. Here integers
    int elem;
    //To of the stack
    struct Stack* previous;
} Stack;

void initialize();
void push(int element);
int pop();
int stackIsEmpty();

Stack *pt_st; // made pt_st global, so functions can modify it

int main(){
    int i;
    initialize();

    //Test phase...
    for(i=0; i<5; i++)
        push(i+1);

    for(i=0; i<5; i++)
        printf("here is one element: %d\n", pop(pt_st));
    //End of test with error!
    return 0;
}

void initialize(){
    pt_st = NULL; // set it to NULL
}

void push(int element){
    Stack *sta = malloc(sizeof(Stack)); // don't need to cast malloc
    sta->elem = element;

    sta->previous = pt_st; // set sta's previous

    pt_st = sta; // point to the pushed node
}

int pop(){
    if(stackIsEmpty())
    {
        printf("pt_stack is empty cannot pop\n");
        return 0;
    }
    else
    {
        int number = pt_st->elem;
        Stack *temp = pt_st; // create a temp copy of the first node's address
        pt_st = pt_st->previous;
        free(temp);
        return number;
    }
}

int stackIsEmpty(){
    if(pt_st == NULL)
        return 1; // if pt_st is NULL, then the stack is empty
    else
        return 0;
}

【讨论】:

    【解决方案2】:

    我认为您需要阅读有关通过副本传递值的内容:What's the difference between passing by reference vs. passing by value?

    initialize 什么都不做,你传递一个地址(按值),所以任何东西都会在你main 中报告。这对于每个功能几乎都是一样的。您还可以在分配结构后立即free。 您必须通过地址将指针传递给堆栈,因为堆栈的顶部由您的函数修改:

    #include <stdlib.h>
    #include <stdio.h>
    
    //Stacks using Structures and linked list and pointerws.
    
    typedef struct Stack{
        //Type of the elements of the stack. Here integers
        int elem;
        //To of the stack
        struct Stack* previous;
    } Stack;
    
    void initialize(Stack **st);
    void push(Stack **st, int element);
    int pop(Stack **st);
    int stackIsEmpty(Stack *st);
    
    int main(){
        int i;
        Stack *pt_st;
        initialize(pt_st);
    
        //Test phase...
        for(i=0; i<5; i++)
            push(&pt_st, i+1);
    
        for(i=0; i<5; i++)
            printf("here is one element: %d\n", pop(&pt_st));
        //End of test with error!
        return 0;
    }
    
    void initialize(Stack **st){
        *st = NULL;
    }
    
    void push(Stack **st, int element){
        Stack *sta = (Stack*)malloc(sizeof(Stack));
        if (sta != NULL)
        {
            sta->previous = *st;
            sta->elem = element;
            *st = sta; // new top of stack is sta            
        }
        else
            exit(0);
    }
    
    int pop(Stack **st){
        if(!stackIsEmpty(*st))
        {
            printf("stack is empty cannot pop\n");
            return 0;
        }
        else
        {
            int number = (*st)->elem;
            Stack* copy = (*st)->previous;
            free(*st);
            *st = copy;
            return number;
        }
    }
    
    int stackIsEmpty(Stack *st){
        if(st == NULL)
            return 0;
        else
            return 1;
    }
    

    也尽量避免在你的函数中过早地使用exit,最好返回一个错误代码并让调用者做出决定,比如:

    //pseudo code
    int push(...) {
       if alloc fails return -1
       else { do the trick; return 0;
    }
    
    ...
    if (push(...)==-1) { print "something's wrong"; exit 0; }
    

    它应该与pop 略有不同,因为您需要在成功时返回一个值,在失败时返回一个错误代码:

    int pop(Stack **st,int *pop) {
        if (staksIsEmpty(st)) return -1;
        *pop = *(st.elem);
        ...
        return 0;
    }
    
    ...
    int value;
    if (pop(&pt_st,&value)==-1) //error
    

    【讨论】:

    • 我很欣赏这句话,我只是在问自己是否碰巧我返回的值包含在用户提供的值中。我应该如何区分return integerreturn error
    • 你说得对,我犯了一个错误,我说的是 push,需要对 pop 进行编辑...
    猜你喜欢
    • 2011-07-29
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 2019-03-04
    • 2020-01-16
    相关资源
    最近更新 更多