More

    [ALGORITHM] SINGLY LINKED LIST – INSERTION AND DELETION

    This is just one way to do insertion and deletion, you may create a better algorithm.

    Insertion algorithm for inserting at any arbitrary location

    public void insert (location,data)

    {

             If(location>size) throw exception

          Node n=new node(data);

          If(location==0)

          {

                  n.next=head;

                  head=n;

                  return;

          }

          Node before=head;

          For(int i=0;i<location-1;i++)

                     before=before.next;

          n.next=before.next;

          before.next=n;

          return;

    }

    Note: This algorithm assume no node marked as Tail. We will have to adjust tail (if available) accordingly.

    Deletion algorithm (without using previous)

    Public void delete(data)

    {

          If(head.data==data)

          {

                  Head=head.next;

                  Return;

          }

    Node current=head;

          While(current.next!=null)

          {

                  If(current.next.data==data)

                  {

                           Current.next=current.next.next;

                           Return;

                  }

          }

    }

    Note: You may simplify this using few more variables.

    Recent Articles

    OAUTH – FREQUENTLY ASKED QUESTIONS FOR INTERVIEWS AND SELF EVALUATION

    Why is refresh token needed when you have access token? Access tokens are usually short-lived and refresh tokens are...

    SUMO LOGIC VIDEOS AND TUTORIALS

    Sumo Logic Basics - Part 1 of 2 (link is external) (Sep 29, 2016)Sumo Logic Basics - Part 2 of 2...

    GIT – USEFUL COMMANDS

    Discard all local changes, but save them for possible re-use later:  git stash Discarding local changes...

    DISTRIBUTED COMPUTING – RECORDED LECTURES (BITS)

    Module 1 - INTRODUCTION Recorded Lecture - 1.1 Introduction Part I – Definition

    BOOK REVIEW GUIDELINES FOR COOKBOOKS

    Whenever you add reviews for the book, please follow below rules. Write issues in an excel.Create an excel...

    Related Stories

    Leave A Reply

    Please enter your comment!
    Please enter your name here

    Stay on op - Ge the daily news in your inbox