segmentation core dump according to code blocks debug it's on line 120 我的链表栈实现有问题吗
segmentation core dump according to code blocks debug it's on line 120 is there something wrong with my implementation of linked list stacks
堆栈的链表实现我编写了以下函数,根据代码块分段转储调用顶部->数据并将其通过 ICP 函数 returns 一个整数我不知道是什么导致了分段可能会转储我使用指针的方式?
struct node
{
char data;
struct node* link;
};
//create pointer top to indicate top of stack
struct node* top=NULL;
void push(char x)
{
struct node* temp = (struct node*)malloc(sizeof(struct node*));
//requires all of this to be done when inserting a node at the end
//set temp.data to character x
temp->data = x;
//set link of node to address of current top
temp->link = top;
//set top of list to newly created node
top = temp;
}
char pop()
{
struct node *temp;
if(top==NULL)
return;
else
{
//pointer temp node pointing to top node
temp = top;
//set address of top to the next node
top=top->link;
//returns character stored in top
return temp->data;
//frees memory space
free(temp);
}
}
int ICP(char z)
{
/*checks if z is + or -, returns ICP*/
if(z=='+'||z=='-')
{return(1);}
if(z=='*'||z=='/')
/*checks if z is * or /, returns ICP*/
{return(3);}
if(z=='^')
/*checks if z is ^, returns ICP*/
{return(6);}
}
int ISP(char z)
{
if(z=='(')
/*checks if z is "(", returns ISP*/
{return(0);}
if(z=='+'||z=='-')
/*checks if z is + or -, returns ISP*/
{return(2);}
if(z=='*'||z=='/')
/*checks if z is * or /, returns ISP*/
{return(4);}
if(z=='^')
/*checks if z is ^, returns ICP*/
{return(5);}
}
int convert(char input[],char output[],int rank)
{
char x;
char TOKEN;
int a=0;
int m=0;
for(m=0;input[m]!='[=10=]';m++)
{
TOKEN=input[m];
if(isalnum(input[m]))
{output[a]=TOKEN;rank++;a++;}
else
{
if(TOKEN=='(')
{push('(');printf("%d",m);}
else
if (TOKEN==')')
{
while((x=pop())!='(')
{
output[a]=rank;rank=rank-1;a++;
}
}
else
{
while(ICP(TOKEN)<ISP(top->data)) **//seg core dumps here**
{
x=pop();
output[a]=x;
rank=rank-1;
a++;
}
push(TOKEN);
}
}
}
return (rank);
}
看起来您正在尝试使用堆栈将前缀转换为后缀(反之亦然)...只是在您的代码中需要注意的一些事项已在评论中提及。
void push(char x)
{
struct node* temp = (struct node*)malloc(sizeof(struct node*));
// Other code here...
}
应该改为:
void push(char x)
{
struct node* temp = malloc(sizeof(struct node));
// Other code here...
}
我们不需要 sizeof(struct node*)
,因为我们不需要 node*
的大小。我们只需要节点的大小来为我们的节点动态分配 space 的数量。
此外,您的 pop 函数需要重新考虑一下。
char pop()
{
struct node *temp = NULL; // Initialize your temp pointers to null.
// Check if list is empty
if(top==NULL)
return; // Cannot return void here; You will need to return something as you function type is a char.
else
{
//pointer temp node pointing to top node
temp = top;
//set address of top to the next node
top=top->link;
//frees memory space
free(temp);
//returns character stored in top
return temp->data;
}
}
您需要先删除数据 temp->data
,然后再点击 pop()
的 return
部分,否则程序将不会弹出 temp->data
中保存的内容。如果这有帮助,请告诉我,谢谢!
堆栈的链表实现我编写了以下函数,根据代码块分段转储调用顶部->数据并将其通过 ICP 函数 returns 一个整数我不知道是什么导致了分段可能会转储我使用指针的方式?
struct node
{
char data;
struct node* link;
};
//create pointer top to indicate top of stack
struct node* top=NULL;
void push(char x)
{
struct node* temp = (struct node*)malloc(sizeof(struct node*));
//requires all of this to be done when inserting a node at the end
//set temp.data to character x
temp->data = x;
//set link of node to address of current top
temp->link = top;
//set top of list to newly created node
top = temp;
}
char pop()
{
struct node *temp;
if(top==NULL)
return;
else
{
//pointer temp node pointing to top node
temp = top;
//set address of top to the next node
top=top->link;
//returns character stored in top
return temp->data;
//frees memory space
free(temp);
}
}
int ICP(char z)
{
/*checks if z is + or -, returns ICP*/
if(z=='+'||z=='-')
{return(1);}
if(z=='*'||z=='/')
/*checks if z is * or /, returns ICP*/
{return(3);}
if(z=='^')
/*checks if z is ^, returns ICP*/
{return(6);}
}
int ISP(char z)
{
if(z=='(')
/*checks if z is "(", returns ISP*/
{return(0);}
if(z=='+'||z=='-')
/*checks if z is + or -, returns ISP*/
{return(2);}
if(z=='*'||z=='/')
/*checks if z is * or /, returns ISP*/
{return(4);}
if(z=='^')
/*checks if z is ^, returns ICP*/
{return(5);}
}
int convert(char input[],char output[],int rank)
{
char x;
char TOKEN;
int a=0;
int m=0;
for(m=0;input[m]!='[=10=]';m++)
{
TOKEN=input[m];
if(isalnum(input[m]))
{output[a]=TOKEN;rank++;a++;}
else
{
if(TOKEN=='(')
{push('(');printf("%d",m);}
else
if (TOKEN==')')
{
while((x=pop())!='(')
{
output[a]=rank;rank=rank-1;a++;
}
}
else
{
while(ICP(TOKEN)<ISP(top->data)) **//seg core dumps here**
{
x=pop();
output[a]=x;
rank=rank-1;
a++;
}
push(TOKEN);
}
}
}
return (rank);
}
看起来您正在尝试使用堆栈将前缀转换为后缀(反之亦然)...只是在您的代码中需要注意的一些事项已在评论中提及。
void push(char x)
{
struct node* temp = (struct node*)malloc(sizeof(struct node*));
// Other code here...
}
应该改为:
void push(char x)
{
struct node* temp = malloc(sizeof(struct node));
// Other code here...
}
我们不需要 sizeof(struct node*)
,因为我们不需要 node*
的大小。我们只需要节点的大小来为我们的节点动态分配 space 的数量。
此外,您的 pop 函数需要重新考虑一下。
char pop()
{
struct node *temp = NULL; // Initialize your temp pointers to null.
// Check if list is empty
if(top==NULL)
return; // Cannot return void here; You will need to return something as you function type is a char.
else
{
//pointer temp node pointing to top node
temp = top;
//set address of top to the next node
top=top->link;
//frees memory space
free(temp);
//returns character stored in top
return temp->data;
}
}
您需要先删除数据 temp->data
,然后再点击 pop()
的 return
部分,否则程序将不会弹出 temp->data
中保存的内容。如果这有帮助,请告诉我,谢谢!