InsertAt() in linked list
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: InsertAt() in linked list

  1. #1
    Join Date
    Mar 2007
    Posts
    149

    InsertAt() in linked list

    Hi,

    Can Anyone help me in pseudo code or algorithms to write InsertAt() method for LinkedList. Basically when this method will be called it will ask user where he want to put that data somewhere in the middle of nodes and then that data will be inserted in between the nodes.

    I am new to C# programming and learning different things everyday. Even if anybody can help me in defining structure for "InsertAt()" method.

    Any ideas.

    Please help..
    Thanks

  2. #2
    Join Date
    Nov 2006
    Location
    North Bend, WA
    Posts
    487

    Re: InsertAt() in linked list

    The general way to handle an insert in a singly-linked list is to use a "follower". Basically:

    Code:
    ListItemPtr = head of list;
    FollowPtr = null;
    while (ListItemPtr is not null and ListItemPtr is not the item to insert before)
    {
       FollowPtr = ListItemPtr;
       ListItemPtr = ListItemPtr.Next;
    }
    
    if (FollowPtr == null)
    {
       // insert item at the beginning of the list
    }
    else
    {
       FollowPtr.Next = NewItem;
      NewItem.Next = ListItemPtr;
    }

  3. #3
    Join Date
    Mar 2007
    Posts
    149

    Re: InsertAt() in linked list

    I have main method as:

    Code:
    Console.WriteLine("Please Enter the data to be Inserted in the LinkList...");
    
    link lnk = new link();
    
    String str = "";
    
    while (true)
    
    {
    
    str = Console.ReadLine();
    
    if(str.Equals("e"))
    
    break;
    
    add(str);
    
    }
    
     Console.WriteLine("The data in nodes are : ");
    
    display();
    
    Console.WriteLine("Do you want to put the data at some specific position?");
    
    String myString = Console.ReadLine();
    
    
    Console.WriteLine("Enter element to put at specified position :");
    
    String s = Console.ReadLine();
    
    Int32 ind = Int32.Parse(s);
    
    insertAt(str, ind);

    When i try to insert the data at some specific position it inserts the element "e" at some random position and also it puts any required element on that position. e.g.

    Console: Do you want to put the data at some specific position?

    pos: 1

    Console.WriteLine("Enter element to put at specified position :");

    3

    So when it display the list it shows like as :

    3

    5

    6

    7

    e

    1

    8

    Why it is inserting "e" in the middle?

    Please advise. Thanks

  4. #4
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: InsertAt() in linked list

    Quote Originally Posted by shah123
    Hi,

    Can Anyone help me in pseudo code or algorithms to write InsertAt() method for LinkedList. Basically when this method will be called it will ask user where he want to put that data somewhere in the middle of nodes and then that data will be inserted in between the nodes.

    I am new to C# programming and learning different things everyday. Even if anybody can help me in defining structure for "InsertAt()" method.

    Any ideas.

    Please help..
    Thanks
    You need an existing GetItemAt() method.. then just do this:

    NewItem.Next = GetItemAt(x)
    GetItemAt(x-1).Next = NewItem

    where X is the location to insert.. REMEMBER TO DO CHECKS FOR THE START AND END OF THE LIST
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center