<?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: Ankit Kumar</title>
    <description>The latest articles on DEV Community by Ankit Kumar (@codeburn12).</description>
    <link>https://dev.to/codeburn12</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%2F951979%2F7bc4fdf2-71d7-4403-adee-f7af6142146a.jpg</url>
      <title>DEV Community: Ankit Kumar</title>
      <link>https://dev.to/codeburn12</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codeburn12"/>
    <language>en</language>
    <item>
      <title>Insertion and Deletion on Linked List</title>
      <dc:creator>Ankit Kumar</dc:creator>
      <pubDate>Sun, 03 Mar 2024 15:11:29 +0000</pubDate>
      <link>https://dev.to/codeburn12/insertion-and-deletion-on-linked-list-1ahn</link>
      <guid>https://dev.to/codeburn12/insertion-and-deletion-on-linked-list-1ahn</guid>
      <description>&lt;p&gt;`&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;p&gt;using namespace std;&lt;/p&gt;

&lt;p&gt;// Linked list definition&lt;br&gt;
struct ListNode&lt;br&gt;
{&lt;br&gt;
    // ListNode comprises data(any) and next(pointer point to next ListNode)&lt;br&gt;
    int data;&lt;br&gt;
    ListNode *next;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Constructor for data initialization for different argument
ListNode() : data(0), next(nullptr) {}
ListNode(int x) : data(x), next(nullptr) {}
ListNode(int x, ListNode *next) : data(x), next(next) {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;class Solution&lt;br&gt;
{&lt;br&gt;
private:&lt;br&gt;
    // Head of ListNode&lt;br&gt;
    ListNode *head;&lt;/p&gt;

&lt;p&gt;public:&lt;br&gt;
    // Constructor to initialize head ListNode as nullptr&lt;br&gt;
    Solution() : head(nullptr) {}&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Insertion at beginning
void InsertAtBegining(int data)
{
    ListNode *newNode = new ListNode(data); // New ListNode creation via ListNode structure constructor(Dynamically)
    newNode-&amp;gt;next = head; // Since head contain the address of first ListNode, put that address in newNode-&amp;gt;next
    head = newNode;       // Now head is pointing to newNode , i.e. newNode is the new head of ListNode
}

// Insertion at end
void InsertionAtEnd(int data)
{
    ListNode *newNode = new ListNode(data);

    // If list is empty, add newNode
    if (head == nullptr)
    {
        head = newNode;
        return;
    }

    // Traverse to the end of list
    ListNode *current = head;
    while (current-&amp;gt;next != nullptr)
    {
        current = current-&amp;gt;next;
    }
    // Now Add to the list 
    current-&amp;gt;next = newNode;
}

// Insertion at any position
void InsertAtPosition(int data, int pos)
{
    // If pos is less than 0 or equals, insert at beginning
    if (pos &amp;lt;= 0)
    {
        InsertAtBegining(data);
        return;
    }
    // Traverse to the ListNode before the specified position
    ListNode *current = head;
    for (int i = 0; i &amp;lt; pos - 1 &amp;amp;&amp;amp; current != nullptr; i++) {
        current = current-&amp;gt;next;
    }
    // If pos is greater than the length of linked list, insert at end
    if(current == nullptr) {
        InsertionAtEnd(data);
        return;
    }
    // Insert newNode at specified position
    ListNode *newNode = new ListNode(data);
    newNode-&amp;gt;next = current-&amp;gt;next;
    current-&amp;gt;next = newNode;
}

// Deletion at beginning
void DeletionAtBeginning() {
    if(head != nullptr) {
        ListNode *temp = head;
        head = temp-&amp;gt;next;
        delete temp;
    }
    else {
        cout &amp;lt;&amp;lt; "List is empty, Can't delete anything";
    }
}

// Deletion at End
void DeletionAtEnd() {
    // List is empty
    if(head == nullptr) {
        cout &amp;lt;&amp;lt; "List is empty, Can't delete anything";
        return;
    }
    // If there is only one ListNode, make head equals nullptr
    if(head-&amp;gt;next == nullptr) {
        delete head;
        head = nullptr;
        return;
    }
    // Traverse from second ListNode to last ListNode
    ListNode *temp = head;
    while(temp-&amp;gt;next-&amp;gt;next != nullptr) {
        temp = temp-&amp;gt;next;
    }
    delete temp-&amp;gt;next;
    temp-&amp;gt;next = nullptr;
}

// Deletion at any position
void DeleteAtPosition(int pos) {
    // Invalid position or list is empty
    if(pos &amp;lt; 0 || head == nullptr) {
        cout &amp;lt;&amp;lt; "List is empty or invalid position";
        return;
    }

    // First position
    if(pos == 0) {
        DeletionAtBeginning();
        return;
    }

    // Traverse till specified position
    ListNode *current = head;
    for (int i = 0; i &amp;lt; pos - 1 &amp;amp;&amp;amp; current != nullptr; i++) {
        current = current-&amp;gt;next;
    }
    // Specified position is beyond the ListNode length
    if(current == nullptr || current-&amp;gt;next == nullptr) {
        cout &amp;lt;&amp;lt; "Given position is beyond ListNode length";
        return;
    }
    // Delition at specified position
    ListNode *temp = current-&amp;gt;next;
    current-&amp;gt;next = current-&amp;gt;next-&amp;gt;next;
    delete temp;
}

void printList()
{
    while (head != nullptr)
    {
        cout &amp;lt;&amp;lt; head-&amp;gt;data &amp;lt;&amp;lt; " ";
        head = head-&amp;gt;next;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;int main()&lt;br&gt;
{&lt;br&gt;
    Solution s;&lt;br&gt;
    s.InsertAtBegining(1);&lt;br&gt;
    s.InsertAtBegining(2);&lt;br&gt;
    s.InsertAtPosition(3,1);&lt;br&gt;
    s.InsertAtPosition(4,2);&lt;br&gt;
    s.InsertionAtEnd(1);&lt;br&gt;
    s.InsertionAtEnd(2);&lt;br&gt;
    s.InsertionAtEnd(3);&lt;br&gt;
    s.InsertionAtEnd(4);&lt;br&gt;
    s.DeletionAtBeginning();&lt;br&gt;&lt;br&gt;
    s.DeletionAtEnd();&lt;br&gt;&lt;br&gt;
    s.DeleteAtPosition(3);&lt;br&gt;&lt;br&gt;
    s.printList();&lt;br&gt;
}`&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>linkedlist</category>
      <category>coding</category>
      <category>programming</category>
    </item>
    <item>
      <title>Insertion and Deletion on Linked List</title>
      <dc:creator>Ankit Kumar</dc:creator>
      <pubDate>Sun, 03 Mar 2024 15:11:25 +0000</pubDate>
      <link>https://dev.to/codeburn12/insertion-and-deletion-on-linked-list-3mma</link>
      <guid>https://dev.to/codeburn12/insertion-and-deletion-on-linked-list-3mma</guid>
      <description>&lt;p&gt;`&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;p&gt;using namespace std;&lt;/p&gt;

&lt;p&gt;// Linked list definition&lt;br&gt;
struct ListNode&lt;br&gt;
{&lt;br&gt;
    // ListNode comprises data(any) and next(pointer point to next ListNode)&lt;br&gt;
    int data;&lt;br&gt;
    ListNode *next;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Constructor for data initialization for different argument
ListNode() : data(0), next(nullptr) {}
ListNode(int x) : data(x), next(nullptr) {}
ListNode(int x, ListNode *next) : data(x), next(next) {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;class Solution&lt;br&gt;
{&lt;br&gt;
private:&lt;br&gt;
    // Head of ListNode&lt;br&gt;
    ListNode *head;&lt;/p&gt;

&lt;p&gt;public:&lt;br&gt;
    // Constructor to initialize head ListNode as nullptr&lt;br&gt;
    Solution() : head(nullptr) {}&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Insertion at beginning
void InsertAtBegining(int data)
{
    ListNode *newNode = new ListNode(data); // New ListNode creation via ListNode structure constructor(Dynamically)
    newNode-&amp;gt;next = head; // Since head contain the address of first ListNode, put that address in newNode-&amp;gt;next
    head = newNode;       // Now head is pointing to newNode , i.e. newNode is the new head of ListNode
}

// Insertion at end
void InsertionAtEnd(int data)
{
    ListNode *newNode = new ListNode(data);

    // If list is empty, add newNode
    if (head == nullptr)
    {
        head = newNode;
        return;
    }

    // Traverse to the end of list
    ListNode *current = head;
    while (current-&amp;gt;next != nullptr)
    {
        current = current-&amp;gt;next;
    }
    // Now Add to the list 
    current-&amp;gt;next = newNode;
}

// Insertion at any position
void InsertAtPosition(int data, int pos)
{
    // If pos is less than 0 or equals, insert at beginning
    if (pos &amp;lt;= 0)
    {
        InsertAtBegining(data);
        return;
    }
    // Traverse to the ListNode before the specified position
    ListNode *current = head;
    for (int i = 0; i &amp;lt; pos - 1 &amp;amp;&amp;amp; current != nullptr; i++) {
        current = current-&amp;gt;next;
    }
    // If pos is greater than the length of linked list, insert at end
    if(current == nullptr) {
        InsertionAtEnd(data);
        return;
    }
    // Insert newNode at specified position
    ListNode *newNode = new ListNode(data);
    newNode-&amp;gt;next = current-&amp;gt;next;
    current-&amp;gt;next = newNode;
}

// Deletion at beginning
void DeletionAtBeginning() {
    if(head != nullptr) {
        ListNode *temp = head;
        head = temp-&amp;gt;next;
        delete temp;
    }
    else {
        cout &amp;lt;&amp;lt; "List is empty, Can't delete anything";
    }
}

// Deletion at End
void DeletionAtEnd() {
    // List is empty
    if(head == nullptr) {
        cout &amp;lt;&amp;lt; "List is empty, Can't delete anything";
        return;
    }
    // If there is only one ListNode, make head equals nullptr
    if(head-&amp;gt;next == nullptr) {
        delete head;
        head = nullptr;
        return;
    }
    // Traverse from second ListNode to last ListNode
    ListNode *temp = head;
    while(temp-&amp;gt;next-&amp;gt;next != nullptr) {
        temp = temp-&amp;gt;next;
    }
    delete temp-&amp;gt;next;
    temp-&amp;gt;next = nullptr;
}

// Deletion at any position
void DeleteAtPosition(int pos) {
    // Invalid position or list is empty
    if(pos &amp;lt; 0 || head == nullptr) {
        cout &amp;lt;&amp;lt; "List is empty or invalid position";
        return;
    }

    // First position
    if(pos == 0) {
        DeletionAtBeginning();
        return;
    }

    // Traverse till specified position
    ListNode *current = head;
    for (int i = 0; i &amp;lt; pos - 1 &amp;amp;&amp;amp; current != nullptr; i++) {
        current = current-&amp;gt;next;
    }
    // Specified position is beyond the ListNode length
    if(current == nullptr || current-&amp;gt;next == nullptr) {
        cout &amp;lt;&amp;lt; "Given position is beyond ListNode length";
        return;
    }
    // Delition at specified position
    ListNode *temp = current-&amp;gt;next;
    current-&amp;gt;next = current-&amp;gt;next-&amp;gt;next;
    delete temp;
}

void printList()
{
    while (head != nullptr)
    {
        cout &amp;lt;&amp;lt; head-&amp;gt;data &amp;lt;&amp;lt; " ";
        head = head-&amp;gt;next;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;int main()&lt;br&gt;
{&lt;br&gt;
    Solution s;&lt;br&gt;
    s.InsertAtBegining(1);&lt;br&gt;
    s.InsertAtBegining(2);&lt;br&gt;
    s.InsertAtPosition(3,1);&lt;br&gt;
    s.InsertAtPosition(4,2);&lt;br&gt;
    s.InsertionAtEnd(1);&lt;br&gt;
    s.InsertionAtEnd(2);&lt;br&gt;
    s.InsertionAtEnd(3);&lt;br&gt;
    s.InsertionAtEnd(4);&lt;br&gt;
    s.DeletionAtBeginning();&lt;br&gt;&lt;br&gt;
    s.DeletionAtEnd();&lt;br&gt;&lt;br&gt;
    s.DeleteAtPosition(3);&lt;br&gt;&lt;br&gt;
    s.printList();&lt;br&gt;
}`&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>linkedlist</category>
      <category>coding</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
