根据代码块进行分段核心转储调试它在第120行,我的链表堆栈实现有问题吗

问题描述 投票:0回答:1

堆栈的链表实现我根据调用 top->data 时的代码块分段转储编写了以下函数,并将其通过返回整数的 ICP 函数我不知道是什么导致了分段转储,就像我使用指针的方式一样或许?

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]!='\0';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);
}
c stack coredump
1个回答
0
投票

看起来您正在尝试使用堆栈将前缀转换为后缀(反之亦然)...只是在代码中需要注意的一些事情已经在注释中提到了。

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*
的大小。我们只需要节点的大小即可为节点动态分配该空间量。

此外,你的 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
,然后再点击
return
pop()
部分,否则程序将不会弹出
temp->data
中保存的内容。请告诉我这是否有帮助,谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.