我正在编写这个程序,我想知道为什么我得到了错误的打印(应该是de 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;
}
答案 0 :(得分:0)
您创建的堆栈指针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;
}
答案 1 :(得分:0)
我认为您需要阅读有关通过副本传递值的内容: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