Bala Ganesh
Bala Ganesh

Reputation: 1135

Linked List delete operation not working in visual studio using C

when I tried to implement a linked list in visual studio 2019 using c it produces heap error. It was due to the free function. However, the code works fine on online compilers which use the GCC compiler. https://www.jdoodle.com/c-online-compiler/

I can't able to figure it out..........................

enter image description here

here is the code:

#include <stdio.h>
#include <stdlib.h>

struct Node
{
    int data;
    struct Node* next;
};

struct Node* head = NULL;

void append(int data)
{

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node*));
    (*newNode).data = data;
    (*newNode).next = NULL;

    if (head == NULL)
    {
        head = newNode;
        return;
    }

    struct Node* temp = head;

    while (temp->next != NULL)
    {
        temp = temp->next;
    }

    temp->next = newNode;
}

void insertAt(int position, int data)
{
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node*));
    newNode->data = data;
    newNode->next = NULL;



    if (position == 1)
    {
        newNode->next = head;
        head = newNode;
        return;
    }

    struct Node* temp = head;

    for (int i = 1; i < position - 1; i++)
    {
        temp = temp->next;
    }

    newNode->next = temp->next;
    temp->next = newNode;
}

void deleteAt(int position)
{
    struct Node* temp = NULL;

    if (position == 1)
    {
        temp = head;
        head = temp->next;
        free(temp);
        return;
    }

    struct Node* tempHead = head;

    for (int i = 1; i < position - 1; i++)
    {
        tempHead = tempHead->next;
    }

    temp = tempHead->next;
    tempHead->next = temp->next;
    free(temp);


}

void print()
{
    struct Node* temp = head;

    while (temp != NULL)
    {
        printf("%d\n", temp->data);
        temp = temp->next;
    }

}

void main()
{
    append(3);
    append(4);
    append(5);
    append(6);

    insertAt(3, 20);
    insertAt(4, 50);
    insertAt(2, 70);

    deleteAt(4);
    deleteAt(3);


    print();
}

Upvotes: 0

Views: 234

Answers (3)

Aditya Shende
Aditya Shende

Reputation: 107

Just try this code once down there .This code is written by me according to my understanding and still if u have any issue with the code you can further ask me .You can try this code out or just cross check it with your's.

Code:

Linked List:

#include <stdio.h>
#include <stdlib.h>
struct Node
{
 int data;
 struct Node *next;
}*first=NULL;
void create(int A[],int n)
{
 int i;
 struct Node *t,*last;
 first=(struct Node *)malloc(sizeof(struct Node));
 first->data=A[0];
 first->next=NULL;
 last=first;

 for(i=1;i<n;i++)
 {
 t=(struct Node*)malloc(sizeof(struct Node));
 t->data=A[i];
 t->next=NULL;
 last->next=t;
 last=t;
 }
}
void Display(struct Node *p)
{
 while(p!=NULL)
 {
 printf("%d ",p->data);
 p=p->next;
 }
}
void RDisplay(struct Node *p)
{
 if(p!=NULL)
 {
 RDisplay(p->next);
 printf("%d ",p->data);

 }
}
int Delete(struct Node *p,int index)
{
 struct Node *q=NULL;
 int x=-1,i;

 if(index < 1 || index > count(p))
 return -1;
 if(index==1)
 {
 q=first;
 x=first->data;
 first=first->next;
 free(q);
 return x;
 }
 else
 {
 for(i=0;i<index-1;i++)
 {
 q=p;
 p=p->next;
 }
 q->next=p->next;
 x=p->data;
 free(p);
 return x;

 }


}
int main()
{

 int A[]={10,20,30,40,50};
 create(A,5);

 printf(“%d\n",Delete(first),2);
 Display(first);

 return 0;
} 

In main function you can pass the function created int the program and also pass the argument according to you.

Upvotes: 1

chux
chux

Reputation: 154087

As @1201ProgramAlarm answered, the allocation size is wrong. sizeof(struct Node*) is the size of a pointer, not the size of the struct.

Instead of trying to match the type, use the size of the referenced data. Easy to code right, review and maintian.

Cast not needed in C.

// struct Node* newNode = (struct Node*)malloc(sizeof(struct Node*));
// instead... 
//  ptr = malloc(sizeof *ptr * N);
struct Node* newNode = malloc(sizeof *newNode);

Upvotes: 2

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32717

The sizes you're passing to malloc are wrong. You should pass sizeof(struct Node).

If you're compiling this as C++ you shouldn't be using malloc at all.

Upvotes: 2

Related Questions