C 프로그래밍을 처음 접하는 사람을 도와주세요: 이중 연결 목록

안녕하세요 저는 C 프로그래밍을 처음 접했고 데이터 구조 rn을 공부하고 있습니다.
이중 연결 목록을 만들고 있는데 코어 덤프 오류가 계속 발생합니다 :(

insertNode(---삽입 1--- 다음)에서 오류가 발생합니다.

이것이 내 주요 코드입니다.

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



그리고 이것은 이중 연결 목록 기능에 대한 내 코드 파일입니다.

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



미리 감사드립니다..!

좋은 웹페이지 즐겨찾기