DEV Community

Nahyun Lee
Nahyun Lee

Posted on

Help me I'm new to c programming: double linked list

Hi I am new to c programming and I am studying data structures rn.
I am making a double linked list and I am keep getting core dumped error :(

I am getting error from insertNode(the one after ---insert 1---)

So this is my main code:

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

typedef enum{false, true} bool;
typedef int Data;


typedef struct _Node
{
    Data item;
    struct _Node* prev;
    struct _Node* next;
} Node;

typedef struct
{
    Node* head;
    int len;
} DoubleLinkedList;

//Function Prototype
void InitList(DoubleLinkedList* plist);
bool IsEmpty(DoubleLinkedList* plist);
Node* makeNode(Data item);
void insertNode(DoubleLinkedList* plist, Data item, int cur);
void deleteNode(DoubleLinkedList* plist, int cur);
void PrintList(DoubleLinkedList* plist);
void ClearList(DoubleLinkedList* plist);


int main()
{
    printf("Data Structure: double linked list practice\n\n");
    printf("\n\n");

    DoubleLinkedList DoubleLink;
    DoubleLinkedList* DLink = &DoubleLink;

    printf("---Initialize the list.---\n");
    InitList(DLink);
    printf("\n\n");

    printf("------Insert 1------\n");
    insertNode(DLink, 1, 1);
    printf("Current status: ");
    PrintList(DLink);
    printf("\n\n");

    printf("------Insert 2------\n");
    insertNode(DLink, 2, 2);
    printf("Current status: ");
    PrintList(DLink);
    printf("\n\n");

    printf("------Insert 3------\n");
    insertNode(DLink, 3, 3);
    printf("Current status: ");
    PrintList(DLink);
    printf("\n\n");

    printf("------Insert 4------\n");
    insertNode(DLink, 4, 4);
    printf("Current status: ");
    PrintList(DLink);
    printf("\n\n");

    printf("------Insert 5------\n");
    insertNode(DLink, 5, 5);
    printf("Current status: ");
    PrintList(DLink);
    printf("\n\n");

    printf("------Delete 2------\n");
    deleteNode(DLink, 2);
    printf("Current status: ");
    PrintList(DLink);
    printf("\n\n");

    printf("---Clear List---\n");
    ClearList(DLink);
}

Enter fullscreen mode Exit fullscreen mode

and this is my code file for double linked list functions:

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

typedef enum{false, true} bool;
typedef int Data;


typedef struct _Node
{
    Data item;
    struct _Node* prev;
    struct _Node* next;
} Node;

typedef struct
{
    Node* head;
    int len;
} DoubleLinkedList;

//Initialize a Double Linked list
void InitList(DoubleLinkedList* plist)
{
    Node* dummy1, * dummy2;
    dummy1 = (Node*)malloc(sizeof(Node));
    dummy2 = (Node*)malloc(sizeof(Node));

    dummy1->prev = NULL;
    dummy1->next = dummy2;
    dummy2->prev = dummy1;
    dummy2->next = NULL;

    plist->head = dummy1;
    plist->len = 0;
}

//Check whether the list is empty
bool IsEmpty(DoubleLinkedList* plist)
{
    return plist->len == 0;
}

//a function that creates new node
Node* makeNode(Data item)
{
    Node* newNode;
    newNode = (Node*)malloc(sizeof(Node));
    newNode->prev = NULL;
    newNode->item = item;
    newNode->next = NULL;

    return newNode;
}

void insertNode(DoubleLinkedList* plist, Data item, int cur) //insert item in (cur) position
{
    Node* newNode = makeNode(item);
    Node* cursor;

    cursor = plist->head;

     //Move the cur pointer to the (k-1)th position
    for(int i = 0;i<cur;i++)
        cursor = cursor->next;

    cursor->next->prev = newNode;
    newNode->next = cursor->next;
    cursor->next = newNode;
    newNode->prev = cursor;
    plist->len++;
}

void deleteNode(DoubleLinkedList* plist, int cur)
{
    Node* cursor, *temp;
    if(IsEmpty(plist)||cur < 0||cur >= plist->len)
    {
        printf("Error: Cannot delete! \n");
        return;
    }

    //Move the cur pointer to the (k-1)th position
    cursor = plist->head;
    for(int i =0; i<cur; i++)
        cursor = cursor->next;

    // Connect adjacent nodes to remove the kth node
    temp = cursor->next;
    temp->next->prev = cursor;
    cursor->next = temp->next;

    //Remove the node to the kth position.
    plist->len--;
    free(temp);
    return;
}

void PrintList(DoubleLinkedList* plist)
{
    Node* cursor;
    cursor = plist->head->next; //start from dummy1

    for (int i = 0; i<plist->len; i++)
    {
        printf("%d\n", cursor->item);
        cursor = cursor->next;
        printf("Finished printing the list\n");
    }
}

void ClearList(DoubleLinkedList* plist)
{
    Node* cursor, *temp;
    if(IsEmpty(plist)){
        printf("List already empty! \n");
        return;
    }
    cursor = plist->head;
    for(int i =0; i<plist->len; i++)
    {
        cursor = cursor->next; //start from dummy 1
        temp = cursor->next;
        temp->next->prev = cursor;
        cursor->next = temp->next;
        free(temp);
    }
    free(plist->head);
    printf("List Cleared! \n");
}

Enter fullscreen mode Exit fullscreen mode

Thank you in advance..!

Top comments (1)

Collapse
 
pauljlucas profile image
Paul J. Lucas

Without saying on what line of code the core dump is happening, nobody can help you.