Search code examples
cstackcoredump

segmentation core dump according to code blocks debug it's on line 120 is there something wrong with my implementation of linked list stacks


linked list implimentation of a stack I wrote the following functions, according to code blocks segmentation dumps when calling for top->data and putting it through ICP function which returns an integer I dont know what's causing the segmentation dumps the way I used the pointers maybe?

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);
}

Solution

  • It looks like you are trying to convert prefix to postfix using a stack (or vice versa)... Just some thing to note in your code that have already been mentioned in the comments.

    void push(char x)
    {
      struct node* temp = (struct node*)malloc(sizeof(struct node*));
      // Other code here...
    }
    

    Should instead be:

    void push(char x)
    {
      struct node* temp = malloc(sizeof(struct node));
      // Other code here...
    }
    

    We don't need the sizeof(struct node*) as we don't need the size of a node* here. We need just the size of the node to dynamically allocate just that amount of space for our node.

    Also, your pop function needs some rethinking.

    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;
      }
    

    }

    You need to remove data first temp->data before hitting the return portion of pop() or else the program will not pop what is held in temp->data. Let me know if this helps, thanks!