I have made a singly linked list that holds an integer & a string. The creation of the list works fine but deletion of a node is giving an error. Previously I was not adding a string to my list(only adding int)& at that time the code was working. please tell me what is wrong

#include <iostream>
#include <windows.h>
#include <string.h>
#include <stdlib.h>

struct fifo
{
    int result;
    char *abc;
    struct fifo *next;
}*node,*t;

void add(struct fifo **n,int data,char *text)
{
    struct fifo *temp,*node1;
    if (*n==NULL)
    {
        temp=(struct fifo *)malloc(sizeof(struct fifo));
        temp->result=data;
        temp->abc=(char *)malloc(strlen(text)+1);
        strcpy(temp->abc,text);
        temp->next=NULL;
        *n=temp;
        t=temp;
        printf("t=%d\n%s\n",t->result,t->abc);
    }
    else
    {
        temp=*n;
        while(temp->next!=NULL)
            temp=temp->next;
        node1=(struct fifo *)malloc(sizeof(struct fifo));
        node1->result=data;
        node1->abc=(char *)malloc(strlen(text)+1);
        strcpy(node1->abc,text);
        node1->next=NULL;
        temp->next=node1;
    }
}

void del(struct fifo **n)
{
    struct fifo *temp;
    if (*n==NULL)
    {
        printf("Linked list is empty\n");
    }
    else
    {
        temp=*n;
        *n=temp->next;
        free(temp);
        t=t->next;
    }
}

int main()
{
    struct fifo *temp;
    //node=NULL;
    int num;
    char choice;
    char text[20];
    while(1)
    {
start:    printf("Enter your choice:\n1.Add node\n2.Delete Node\n3.Display\n4.Exit\n");
    scanf("%c",&choice);
    switch(choice)
    {
        case '1':
            printf("Enter number\n");
            scanf("%d",&num);
            printf("Enter text\n");
            scanf("%s",text);
            add(&node,num,text);
            break;

        case '2':
            del(&t);
            break;

        case '3':
            if (node==NULL)
            {
                printf("List is empty\n");
                //printf("t=%d\n%s\n",t->result,t->abc);
                goto start;
            }
            else
            {
                temp=node;
                while(temp!=NULL)
                {
                    printf("%d\n",temp->result);
                    char *loc=(char *)malloc(strlen(temp->abc));
                    strcpy(loc,temp->abc);
                    printf("%s\n",loc);
                    free(loc);
                    temp=temp->next;
                }
                printf("t=%d\n%s\n",t->result,t->abc);
            }

            break;

        case '4':
            printf("Exiting program...\n");
            return (0);

        default:
            goto start;
    }
    }

}

champnim, I suspect the problem might be when you free the node which wanted to delete. Perhaps, the memory leak. Do you know the abc char ptr which you have in your node, that needds to be freed as well, before you free temp!!!

ssharish

champnim, I suspect the problem might be when you free the node which wanted to delete. Perhaps, the memory leak. Do you know the abc char ptr which you have in your node, that needds to be freed as well, before you free temp!!!

ssharish

In top of that, and more important, the last statement,

t=t->next;

in the del() function leads to access a null pointer.

This article has been dead for over six months. Start a new discussion instead.