<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nahyun Lee</title>
    <description>The latest articles on DEV Community by Nahyun Lee (@nahyun_lee_299f72b3cb3742).</description>
    <link>https://dev.to/nahyun_lee_299f72b3cb3742</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F851715%2Ff61bb404-ad9c-4c2c-8acb-1607280f9540.png</url>
      <title>DEV Community: Nahyun Lee</title>
      <link>https://dev.to/nahyun_lee_299f72b3cb3742</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nahyun_lee_299f72b3cb3742"/>
    <language>en</language>
    <item>
      <title>Help me I'm new to c programming: double linked list</title>
      <dc:creator>Nahyun Lee</dc:creator>
      <pubDate>Fri, 22 Apr 2022 09:08:08 +0000</pubDate>
      <link>https://dev.to/nahyun_lee_299f72b3cb3742/help-me-im-new-to-c-programming-double-linked-list-4lhg</link>
      <guid>https://dev.to/nahyun_lee_299f72b3cb3742/help-me-im-new-to-c-programming-double-linked-list-4lhg</guid>
      <description>&lt;p&gt;Hi I am new to c programming and I am studying data structures rn. &lt;br&gt;
I am making a double linked list and I am keep getting core dumped error :(&lt;/p&gt;

&lt;p&gt;I am getting error from insertNode(the one after ---insert 1---)&lt;/p&gt;

&lt;p&gt;So this is my main code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;string.h&amp;gt;

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 = &amp;amp;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);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and this is my code file for double linked list functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;string.h&amp;gt;

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-&amp;gt;prev = NULL;
    dummy1-&amp;gt;next = dummy2;
    dummy2-&amp;gt;prev = dummy1;
    dummy2-&amp;gt;next = NULL;

    plist-&amp;gt;head = dummy1;
    plist-&amp;gt;len = 0;
}

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

//a function that creates new node
Node* makeNode(Data item)
{
    Node* newNode;
    newNode = (Node*)malloc(sizeof(Node));
    newNode-&amp;gt;prev = NULL;
    newNode-&amp;gt;item = item;
    newNode-&amp;gt;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-&amp;gt;head;

     //Move the cur pointer to the (k-1)th position
    for(int i = 0;i&amp;lt;cur;i++)
        cursor = cursor-&amp;gt;next;

    cursor-&amp;gt;next-&amp;gt;prev = newNode;
    newNode-&amp;gt;next = cursor-&amp;gt;next;
    cursor-&amp;gt;next = newNode;
    newNode-&amp;gt;prev = cursor;
    plist-&amp;gt;len++;
}

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

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

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

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

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

    for (int i = 0; i&amp;lt;plist-&amp;gt;len; i++)
    {
        printf("%d\n", cursor-&amp;gt;item);
        cursor = cursor-&amp;gt;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-&amp;gt;head;
    for(int i =0; i&amp;lt;plist-&amp;gt;len; i++)
    {
        cursor = cursor-&amp;gt;next; //start from dummy 1
        temp = cursor-&amp;gt;next;
        temp-&amp;gt;next-&amp;gt;prev = cursor;
        cursor-&amp;gt;next = temp-&amp;gt;next;
        free(temp);
    }
    free(plist-&amp;gt;head);
    printf("List Cleared! \n");
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thank you in advance..! &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>c</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
