>我正在编写这个程序,我想了解为什么我得到错误的打印(应该 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: %dn", 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 popn");
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;
}
您创建的堆栈指针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: %dn", 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 popn");
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;
}
我认为您需要阅读有关通过复制传递值的内容:通过引用传递与按值传递有什么区别?
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: %dn", 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 popn");
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